From 8d0dd53d5dd9e8f3664b824675f53c093e168df5 Mon Sep 17 00:00:00 2001 From: Elias N Vasylenko Date: Sun, 29 Nov 2015 22:51:19 +0000 Subject: [PATCH] Much work towards a sensible API for updatable Binding etc. Broken atm.. --- .../strangeskies/modabi/profile/Profiler.java | 28 ++-- .../modabi/bnd/ModabiRegistration.java | 8 +- .../uk/co/strangeskies/modabi/Binding.java | 21 +-- .../co/strangeskies/modabi/SchemaManager.java | 125 +++++++-------- ...terface.java => StructuredDataFormat.java} | 2 +- .../modabi/impl/PartialSchemaProcessor.java | 2 +- .../modabi/impl/ProcessingContextImpl.java | 4 + .../modabi/impl/SchemaManagerImpl.java | 145 +++++++++++++++--- .../modabi/impl/processing/SchemaBinder.java | 39 ++++- .../modabi/io/xml/XmlInterface.java | 4 +- uk.co.strangeskies.modabi.mapping/bnd.bnd | 3 +- .../modabi/mappings/MappingTest.xml | 15 ++ .../{ => modabi}/schemata/MappingSchema.xml | 2 +- .../strangeskies/modabi/mapping/Mapping.java | 7 + .../modabi/mapping/MappingBuilder.java | 15 ++ 15 files changed, 281 insertions(+), 139 deletions(-) rename uk.co.strangeskies.modabi.core.api/src/uk/co/strangeskies/modabi/io/structured/{DataInterface.java => StructuredDataFormat.java} (96%) create mode 100644 uk.co.strangeskies.modabi.mapping/modabi/mappings/MappingTest.xml rename uk.co.strangeskies.modabi.mapping/{ => modabi}/schemata/MappingSchema.xml (91%) create mode 100644 uk.co.strangeskies.modabi.mapping/src/uk/co/strangeskies/modabi/mapping/MappingBuilder.java diff --git a/uk.co.strangeskies.modabi.benchmarks/src/uk/co/strangeskies/modabi/profile/Profiler.java b/uk.co.strangeskies.modabi.benchmarks/src/uk/co/strangeskies/modabi/profile/Profiler.java index 33ac36c7..f0a36720 100644 --- a/uk.co.strangeskies.modabi.benchmarks/src/uk/co/strangeskies/modabi/profile/Profiler.java +++ b/uk.co.strangeskies.modabi.benchmarks/src/uk/co/strangeskies/modabi/profile/Profiler.java @@ -11,7 +11,6 @@ import org.osgi.service.component.annotations.ReferenceCardinality; import uk.co.strangeskies.modabi.SchemaManager; -import uk.co.strangeskies.modabi.io.structured.NavigableStructuredDataSource; import uk.co.strangeskies.modabi.io.structured.StructuredDataBuffer; import uk.co.strangeskies.utilities.ContextClassLoaderRunner; import uk.co.strangeskies.utilities.Log; @@ -71,10 +70,9 @@ public void profile(BundleContext context) throws BundleException { } private long bindingProfile(int profileRounds) { - NavigableStructuredDataSource buffer = manager - .unbind(manager.getMetaSchema().getSchemaModel(), - manager.getMetaSchema()) - .to(StructuredDataBuffer.singleBuffer()).getBuffer(); + StructuredDataBuffer.Navigable buffer = StructuredDataBuffer.singleBuffer(); + manager.unbind(manager.getMetaSchema().getSchemaModel(), + manager.getMetaSchema()).to(buffer); log(Level.INFO, "Binding Profiling"); long startTime = System.currentTimeMillis(); @@ -82,9 +80,9 @@ private long bindingProfile(int profileRounds) { if (i % 10 == 0) log(Level.INFO, " working... (" + i + " / " + profileRounds + ")"); - buffer.reset(); - manager.bind(manager.getMetaSchema().getSchemaModel()).from(buffer) - .resolve(); + buffer.getBuffer().reset(); + manager.bind(manager.getMetaSchema().getSchemaModel()) + .from(buffer.getBuffer()).resolve(); } return System.currentTimeMillis() - startTime; } @@ -108,14 +106,14 @@ private void warmUpProfiling(int warmupRounds) { if (i % 10 == 0) log(Level.INFO, " working... (" + i + " / " + warmupRounds + ")"); - NavigableStructuredDataSource buffer = manager - .unbind(manager.getMetaSchema().getSchemaModel(), - manager.getMetaSchema()) - .to(StructuredDataBuffer.singleBuffer()).getBuffer(); + StructuredDataBuffer.Navigable buffer = StructuredDataBuffer + .singleBuffer(); + manager.unbind(manager.getMetaSchema().getSchemaModel(), + manager.getMetaSchema()).to(buffer); - buffer.reset(); - manager.bind(manager.getMetaSchema().getSchemaModel()).from(buffer) - .resolve(); + buffer.getBuffer().reset(); + manager.bind(manager.getMetaSchema().getSchemaModel()) + .from(buffer.getBuffer()).resolve(); } } } diff --git a/uk.co.strangeskies.modabi.bnd/src/uk/co/strangeskies/modabi/bnd/ModabiRegistration.java b/uk.co.strangeskies.modabi.bnd/src/uk/co/strangeskies/modabi/bnd/ModabiRegistration.java index 9e508aed..7d74a38c 100644 --- a/uk.co.strangeskies.modabi.bnd/src/uk/co/strangeskies/modabi/bnd/ModabiRegistration.java +++ b/uk.co.strangeskies.modabi.bnd/src/uk/co/strangeskies/modabi/bnd/ModabiRegistration.java @@ -29,14 +29,14 @@ import uk.co.strangeskies.modabi.Schema; import uk.co.strangeskies.modabi.SchemaException; import uk.co.strangeskies.modabi.SchemaManager; -import uk.co.strangeskies.modabi.io.structured.DataInterface; +import uk.co.strangeskies.modabi.io.structured.StructuredDataFormat; import uk.co.strangeskies.utilities.ContextClassLoaderRunner; public abstract class ModabiRegistration implements AnalyzerPlugin { - private final DataInterface handler; + private final StructuredDataFormat handler; private final SchemaManager manager; - public ModabiRegistration(SchemaManager manager, DataInterface handler) { + public ModabiRegistration(SchemaManager manager, StructuredDataFormat handler) { this.handler = handler; this.manager = manager; manager.registerDataInterface(handler); @@ -87,7 +87,7 @@ private void registerSchemata(Jar jar, Analyzer analyzer) { if (newCapabilities != null) { appendProperties(analyzer, Constants.REQUIRE_CAPABILITY, "osgi.service;" + "filter:=\"(&(objectClass=" - + DataInterface.class.getTypeName() + ")(formatId=" + + StructuredDataFormat.class.getTypeName() + ")(formatId=" + handler.getFormatId() + "))\";resolution:=mandatory;effective:=active"); diff --git a/uk.co.strangeskies.modabi.core.api/src/uk/co/strangeskies/modabi/Binding.java b/uk.co.strangeskies.modabi.core.api/src/uk/co/strangeskies/modabi/Binding.java index 6343f6ba..e853429f 100644 --- a/uk.co.strangeskies.modabi.core.api/src/uk/co/strangeskies/modabi/Binding.java +++ b/uk.co.strangeskies.modabi.core.api/src/uk/co/strangeskies/modabi/Binding.java @@ -18,22 +18,17 @@ */ package uk.co.strangeskies.modabi; +import uk.co.strangeskies.modabi.io.structured.NavigableStructuredDataSource; import uk.co.strangeskies.modabi.schema.Model; -public class Binding { - private final Model model; - private final T data; +public interface Binding { + public Model getModel(); - public Binding(Model model, T data) { - this.model = model; - this.data = data; - } + public T getData(); - public Model getModel() { - return model; - } + public void updateBinding(); - public T getData() { - return data; - } + public NavigableStructuredDataSource getSource(); + + public void updateSource(); } diff --git a/uk.co.strangeskies.modabi.core.api/src/uk/co/strangeskies/modabi/SchemaManager.java b/uk.co.strangeskies.modabi.core.api/src/uk/co/strangeskies/modabi/SchemaManager.java index 90723cdb..b4703e94 100644 --- a/uk.co.strangeskies.modabi.core.api/src/uk/co/strangeskies/modabi/SchemaManager.java +++ b/uk.co.strangeskies.modabi.core.api/src/uk/co/strangeskies/modabi/SchemaManager.java @@ -29,25 +29,21 @@ import java.util.Collection; import java.util.Collections; import java.util.Set; -import java.util.function.BiConsumer; import java.util.function.BiPredicate; import java.util.function.Consumer; import java.util.function.Function; import java.util.function.Predicate; import java.util.function.Supplier; -import uk.co.strangeskies.modabi.io.DataTarget; -import uk.co.strangeskies.modabi.io.structured.DataInterface; +import uk.co.strangeskies.modabi.io.structured.DiscardingStructuredDataTarget; +import uk.co.strangeskies.modabi.io.structured.StructuredDataFormat; import uk.co.strangeskies.modabi.io.structured.StructuredDataSource; import uk.co.strangeskies.modabi.io.structured.StructuredDataTarget; -import uk.co.strangeskies.modabi.io.structured.StructuredDataTargetImpl; import uk.co.strangeskies.modabi.processing.BindingFuture; import uk.co.strangeskies.modabi.processing.BindingState; import uk.co.strangeskies.modabi.processing.UnbindingState; -import uk.co.strangeskies.modabi.schema.ComplexNode; -import uk.co.strangeskies.modabi.schema.DataNode; +import uk.co.strangeskies.modabi.schema.BindingNode; import uk.co.strangeskies.modabi.schema.Model; -import uk.co.strangeskies.modabi.schema.SchemaNode; import uk.co.strangeskies.reflection.Reified; import uk.co.strangeskies.reflection.TypeToken; import uk.co.strangeskies.reflection.TypedObject; @@ -99,11 +95,21 @@ default BindingFuture from(URL input) { BindingFuture from(String extension, InputStream input); - Binder supply(TypeToken type, - Supplier> action); + default Binder supply(TypeToken type, + Supplier> action) { + return supply(type, s -> action.get()); + } + + default Binder supply(TypeToken type, + Function> action) { + return supply(s -> { + return (s instanceof BindingNode && type.isAssignableFrom( + ((BindingNode.Effective) s.bindingNode()).getDataType())) + ? action.apply(s) : null; + }); + } - Binder supply(TypeToken type, - Function> action); + Binder supply(Function> action); /* * Errors which are rethrown will be passed to the next error handler if @@ -113,22 +119,22 @@ Binder supply(TypeToken type, Binder with(Consumer errorHandler); } - interface Unbinder { - U to(U output); + interface Unbinder { + BindingFuture to(StructuredDataTarget output); - default void to(File output) { - to(output.toURI()); + default BindingFuture to(File output) { + return to(output.toURI()); } - default void to(URI output) { + default BindingFuture to(URI output) { try { - to(output.toURL()); + return to(output.toURL()); } catch (MalformedURLException e) { throw new IllegalArgumentException(e); } } - default void to(URL output) { + default BindingFuture to(URL output) { String extension = output.getQuery(); int lastDot = extension.lastIndexOf('.'); if (lastDot > 0) { @@ -140,35 +146,43 @@ default void to(URL output) { try ( OutputStream fileStream = output.openConnection().getOutputStream()) { - to(extension, fileStream).flush(); + BindingFuture binding = to(extension, fileStream); + fileStream.flush(); + return binding; } catch (IOException e) { throw new IllegalArgumentException(e); } } - U to(String extension, U output); + BindingFuture to(String extension, OutputStream output); - Unbinder consume(Predicate filter); + Unbinder consume(Predicate filter); + + default Unbinder consume(TypeToken type, + Predicate> filter) { + return consume(type, (s, o) -> filter.test(o)); + } - Unbinder consume(BiPredicate> filter); + Unbinder consume(TypeToken type, + BiPredicate> filter); /* * Errors which are rethrown will be passed to the next error handler if * present, or dealt with as normal. Otherwise, a best effort is made at - * unbinding. + * unbinding, and the exception information will be serialised as a comment. */ - Unbinder with(Consumer errorHandler); + Unbinder with(Consumer errorHandler); } - void registerDataInterface(DataInterface handler); + void registerDataInterface(StructuredDataFormat handler); - void unregisterDataInterface(DataInterface handler); + void unregisterDataInterface(StructuredDataFormat handler); - Set getRegisteredDataInterfaces(); + Set getRegisteredDataInterfaces(); - DataInterface getDataInterface(String id); + StructuredDataFormat getDataInterface(String id); - Set getDataInterfaces(String extension); + Set getDataInterfaces(String extension); default GeneratedSchema generateSchema(QualifiedName name) { return generateSchema(name, Collections.emptySet()); @@ -191,13 +205,10 @@ default void registerProvider(Class providedClass, registerProvider(TypeToken.over(providedClass), provider); } - void registerSchema(Schema schema); + boolean registerSchema(Schema schema); - // So we can import from manually added data. - void registerBinding(Binding binding); - - default void registerBinding(Model model, T data) { - registerBinding(new Binding(model, data)); + default BindingFuture registerBinding(Model model, T data) { + return unbind(model, data).to(new DiscardingStructuredDataTarget()); } // Blocks until all possible processing is done other than waiting imports: @@ -214,53 +225,21 @@ default Binder bind(Class dataClass) { Set> bindingFutures(Model model); - default Binder bindSchema() { - Binder binder = bind(getMetaSchema().getSchemaModel()); - - return new Binder() { - @Override - public BindingFuture from(StructuredDataSource input) { - return registerFuture(binder.from(input)); - } - - @Override - public BindingFuture from(URL input) { - return registerFuture(binder.from(input)); - } - - @Override - public BindingFuture from(InputStream input) { - return registerFuture(binder.from(input)); - } - - @Override - public BindingFuture from(String extension, InputStream input) { - return registerFuture(binder.from(extension, input)); - } - - private BindingFuture registerFuture(BindingFuture from) { - new Thread(() -> { - registerSchema(from.resolve()); - }).start(); - - return from; - } - }; - } + Binder bindSchema(); - Unbinder unbind(Model model, T data); + Unbinder unbind(Model model, T data); - Unbinder unbind(TypeToken dataClass, T data); + Unbinder unbind(TypeToken dataClass, T data); - default Unbinder unbind(Class dataClass, T data) { + default Unbinder unbind(Class dataClass, T data) { return unbind(TypeToken.over(dataClass), data); } - default > Unbinder unbind(T data) { + default > Unbinder unbind(T data) { return unbind(data.getThisType(), data); } - Unbinder unbind(Object data); + Unbinder unbind(T data); MetaSchema getMetaSchema(); diff --git a/uk.co.strangeskies.modabi.core.api/src/uk/co/strangeskies/modabi/io/structured/DataInterface.java b/uk.co.strangeskies.modabi.core.api/src/uk/co/strangeskies/modabi/io/structured/StructuredDataFormat.java similarity index 96% rename from uk.co.strangeskies.modabi.core.api/src/uk/co/strangeskies/modabi/io/structured/DataInterface.java rename to uk.co.strangeskies.modabi.core.api/src/uk/co/strangeskies/modabi/io/structured/StructuredDataFormat.java index 4e07e616..74402270 100644 --- a/uk.co.strangeskies.modabi.core.api/src/uk/co/strangeskies/modabi/io/structured/DataInterface.java +++ b/uk.co.strangeskies.modabi.core.api/src/uk/co/strangeskies/modabi/io/structured/StructuredDataFormat.java @@ -22,7 +22,7 @@ import java.io.OutputStream; import java.util.Set; -public interface DataInterface { +public interface StructuredDataFormat { String getFormatId(); Set getFileExtensions(); diff --git a/uk.co.strangeskies.modabi.core.provider/src/uk/co/strangeskies/modabi/impl/PartialSchemaProcessor.java b/uk.co.strangeskies.modabi.core.provider/src/uk/co/strangeskies/modabi/impl/PartialSchemaProcessor.java index 84127f1a..02c01d67 100644 --- a/uk.co.strangeskies.modabi.core.provider/src/uk/co/strangeskies/modabi/impl/PartialSchemaProcessor.java +++ b/uk.co.strangeskies.modabi.core.provider/src/uk/co/strangeskies/modabi/impl/PartialSchemaProcessor.java @@ -56,7 +56,7 @@ default void accept(ChoiceNode.Effective node) { } default void accept(BindingChildNode.Effective node) { - accept(node); + accept((InputNode.Effective) node); } default void accept(InputNode.Effective node) { diff --git a/uk.co.strangeskies.modabi.core.provider/src/uk/co/strangeskies/modabi/impl/ProcessingContextImpl.java b/uk.co.strangeskies.modabi.core.provider/src/uk/co/strangeskies/modabi/impl/ProcessingContextImpl.java index 2354c95b..9c3f0dfb 100644 --- a/uk.co.strangeskies.modabi.core.provider/src/uk/co/strangeskies/modabi/impl/ProcessingContextImpl.java +++ b/uk.co.strangeskies.modabi.core.provider/src/uk/co/strangeskies/modabi/impl/ProcessingContextImpl.java @@ -266,6 +266,10 @@ public List> getMatchingModels( .map(n -> n.effective()).collect(Collectors.toList()); } + public SchemaManager getSchemaManager() { + return manager; + } + @SuppressWarnings("unchecked") public ComputingMap, DataNode.Effective> getDataNodeOverrides( DataNode node) { diff --git a/uk.co.strangeskies.modabi.core.provider/src/uk/co/strangeskies/modabi/impl/SchemaManagerImpl.java b/uk.co.strangeskies.modabi.core.provider/src/uk/co/strangeskies/modabi/impl/SchemaManagerImpl.java index ff769c41..e3a43700 100644 --- a/uk.co.strangeskies.modabi.core.provider/src/uk/co/strangeskies/modabi/impl/SchemaManagerImpl.java +++ b/uk.co.strangeskies.modabi.core.provider/src/uk/co/strangeskies/modabi/impl/SchemaManagerImpl.java @@ -22,6 +22,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.net.URL; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -34,8 +35,10 @@ import java.util.Set; import java.util.SortedSet; import java.util.TreeSet; +import java.util.function.BiPredicate; import java.util.function.Consumer; import java.util.function.Function; +import java.util.function.Predicate; import java.util.function.Supplier; import java.util.stream.Collectors; @@ -63,11 +66,13 @@ import uk.co.strangeskies.modabi.impl.processing.SchemaUnbinder; import uk.co.strangeskies.modabi.impl.schema.building.DataTypeBuilderImpl; import uk.co.strangeskies.modabi.impl.schema.building.ModelBuilderImpl; -import uk.co.strangeskies.modabi.io.structured.DataInterface; +import uk.co.strangeskies.modabi.io.structured.StructuredDataFormat; import uk.co.strangeskies.modabi.io.structured.StructuredDataSource; import uk.co.strangeskies.modabi.io.structured.StructuredDataTarget; import uk.co.strangeskies.modabi.processing.BindingContext; import uk.co.strangeskies.modabi.processing.BindingFuture; +import uk.co.strangeskies.modabi.processing.BindingState; +import uk.co.strangeskies.modabi.processing.UnbindingState; import uk.co.strangeskies.modabi.processing.providers.DereferenceSource; import uk.co.strangeskies.modabi.processing.providers.ImportSource; import uk.co.strangeskies.modabi.processing.providers.IncludeTarget; @@ -99,7 +104,7 @@ public class SchemaManagerImpl implements SchemaManager { private final BindingProviders bindingProviders; - private final Map dataInterfaces; + private final Map dataInterfaces; public SchemaManagerImpl() { this(new SchemaBuilderImpl(), new ModelBuilderImpl(), @@ -163,8 +168,7 @@ DataTypeBuilder getDataTypeBuilder() { return dataTypeBuilder; } - @Override - public void registerSchema(Schema schema) { + private boolean registerSchemaImpl(Schema schema) { if (registeredSchemata.add(schema)) { for (Schema dependency : schema.getDependencies()) registerSchema(dependency); @@ -175,12 +179,75 @@ public void registerSchema(Schema schema) { for (DataType type : schema.getDataTypes()) registerDataType(type); + return true; + } else { + return false; + } + } + + @Override + public boolean registerSchema(Schema schema) { + if (registerSchemaImpl(schema)) { bindingFutures.add(coreSchemata.metaSchema().getSchemaModel().getName(), - BindingFuture.forBinding(new Binding<>( - coreSchemata.metaSchema().getSchemaModel(), schema))); + registerBinding(coreSchemata.metaSchema().getSchemaModel(), schema)); + + return true; + } else { + return false; } } + @Override + public Binder bindSchema() { + Binder binder = bind(getMetaSchema().getSchemaModel()); + + return new Binder() { + @Override + public BindingFuture from(StructuredDataSource input) { + return registerFuture(binder.from(input)); + } + + @Override + public BindingFuture from(URL input) { + return registerFuture(binder.from(input)); + } + + @Override + public BindingFuture from(InputStream input) { + return registerFuture(binder.from(input)); + } + + @Override + public BindingFuture from(String extension, InputStream input) { + return registerFuture(binder.from(extension, input)); + } + + private BindingFuture registerFuture( + BindingFuture schema) { + new Thread(() -> { + registerSchemaImpl(schema.resolve()); + bindingFutures.add( + coreSchemata.metaSchema().getSchemaModel().getName(), schema); + }).start(); + + return schema; + } + + @Override + public Binder with(Consumer errorHandler) { + binder.with(errorHandler); + return this; + } + + @Override + public Binder supply( + Function> action) { + binder.supply(action); + return this; + } + }; + } + void registerModel(Model model) { registeredModels.add(model); } @@ -189,8 +256,7 @@ void registerDataType(DataType type) { registeredTypes.add(type); } - @Override - public void registerBinding(Binding binding) { + protected void registerBindingImpl(Binding binding) { bindingFutures.add(binding.getModel().getName(), BindingFuture.forBinding(binding)); } @@ -224,7 +290,7 @@ public BindingFuture from(String extension, InputStream input) { } private BindingFuture from(InputStream input, - Collection loaders) { + Collection loaders) { BufferedInputStream bufferedInput = new BufferedInputStream(input); bufferedInput.mark(4096); @@ -234,7 +300,7 @@ private BindingFuture from(InputStream input, Exception exception = null; - for (DataInterface loader : loaders) { + for (StructuredDataFormat loader : loaders) { try { return from(loader.loadData(bufferedInput)); } catch (Exception e) { @@ -251,6 +317,18 @@ private BindingFuture from(InputStream input, throw new IllegalArgumentException( "Could not bind input with any registered file loaders", exception); } + + @Override + public Binder supply(Function> action) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Binder with(Consumer errorHandler) { + // TODO Auto-generated method stub + return null; + } }; } @@ -308,39 +386,58 @@ public Set> bindingFutures(Model model) { .collect(Collectors.toSet()); } - private Unbinder createUnbinder( + private Unbinder createUnbinder( Consumer unbindingFunction) { - return new Unbinder() { + return new Unbinder() { @Override - public U to(String extension, U output) { + public BindingFuture to(String extension, OutputStream output) { unbindingFunction.accept(getDataInterface(extension).saveData(output)); - return output; + return null; } @Override - public U to(U output) { + public BindingFuture to(StructuredDataTarget output) { unbindingFunction.accept(output); - return output; + return null; + } + + @Override + public Unbinder consume(Predicate filter) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Unbinder consume(TypeToken type, + BiPredicate> filter) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Unbinder with(Consumer errorHandler) { + // TODO Auto-generated method stub + return null; } }; } @Override - public Unbinder unbind(Model model, T data) { + public Unbinder unbind(Model model, T data) { return createUnbinder(output -> new SchemaUnbinder(this) .unbind(model.effective(), output, data)); } @Override - public Unbinder unbind(Object data) { + public Unbinder unbind(T data) { return createUnbinder( output -> new SchemaUnbinder(this).unbind(output, data)); } @Override - public Unbinder unbind(TypeToken dataType, T data) { + public Unbinder unbind(TypeToken dataType, T data) { return createUnbinder( output -> new SchemaUnbinder(this).unbind(output, dataType, data)); } @@ -418,27 +515,27 @@ public GeneratedSchema generateSchema(QualifiedName name, @Override @Reference(cardinality = ReferenceCardinality.MULTIPLE, policy = ReferencePolicy.DYNAMIC, unbind = "unregisterDataInterface") - public void registerDataInterface(DataInterface loader) { + public void registerDataInterface(StructuredDataFormat loader) { dataInterfaces.put(loader.getFormatId(), loader); } @Override - public void unregisterDataInterface(DataInterface loader) { + public void unregisterDataInterface(StructuredDataFormat loader) { dataInterfaces.remove(loader.getFormatId(), loader); } @Override - public Set getRegisteredDataInterfaces() { + public Set getRegisteredDataInterfaces() { return new HashSet<>(dataInterfaces.values()); } @Override - public DataInterface getDataInterface(String id) { + public StructuredDataFormat getDataInterface(String id) { return dataInterfaces.get(id); } @Override - public Set getDataInterfaces(String extension) { + public Set getDataInterfaces(String extension) { return getRegisteredDataInterfaces().stream() .filter(l -> l.getFileExtensions().contains(extension)) .collect(Collectors.toCollection(LinkedHashSet::new)); diff --git a/uk.co.strangeskies.modabi.core.provider/src/uk/co/strangeskies/modabi/impl/processing/SchemaBinder.java b/uk.co.strangeskies.modabi.core.provider/src/uk/co/strangeskies/modabi/impl/processing/SchemaBinder.java index 6e3f2627..ae0ea8d5 100644 --- a/uk.co.strangeskies.modabi.core.provider/src/uk/co/strangeskies/modabi/impl/processing/SchemaBinder.java +++ b/uk.co.strangeskies.modabi.core.provider/src/uk/co/strangeskies/modabi/impl/processing/SchemaBinder.java @@ -27,6 +27,7 @@ import uk.co.strangeskies.modabi.Binding; import uk.co.strangeskies.modabi.QualifiedName; import uk.co.strangeskies.modabi.SchemaException; +import uk.co.strangeskies.modabi.io.structured.NavigableStructuredDataSource; import uk.co.strangeskies.modabi.io.structured.StructuredDataSource; import uk.co.strangeskies.modabi.processing.BindingException; import uk.co.strangeskies.modabi.processing.BindingFuture; @@ -34,7 +35,7 @@ public class SchemaBinder { private static interface TryGet { - T tryGet() + Binding tryGet() throws InterruptedException, ExecutionException, TimeoutException; } @@ -63,11 +64,41 @@ public BindingFuture bind(Model.Effective model, throw new BindingException("Model '" + model.getName() + "' does not match root input node '" + inputRoot + "'", context); - FutureTask future = new FutureTask<>(() -> { + FutureTask> future = new FutureTask>(() -> { Thread.currentThread().setContextClassLoader(classLoader); try { - return new BindingNodeBinder(context).bind(model); + return new Binding() { + private T data = new BindingNodeBinder(context).bind(model); + + @Override + public Model getModel() { + return model; + } + + @Override + public T getData() { + return data; + } + + @Override + public void updateBinding() { + // TODO Auto-generated method stub + + } + + @Override + public NavigableStructuredDataSource getSource() { + // TODO Auto-generated method stub + return null; + } + + @Override + public void updateSource() { + // TODO Auto-generated method stub + + } + }; } catch (SchemaException e) { throw e; } catch (Exception e) { @@ -106,7 +137,7 @@ public Binding get(long timeout, TimeUnit unit) { private Binding tryGet(TryGet get) { try { - return new Binding(getModel(), get.tryGet()); + return get.tryGet(); } catch (InterruptedException e) { throw new SchemaException("Unexpected interrupt during binding of '" + getName() + "' with model '" + getModel().getName() + "'", e); diff --git a/uk.co.strangeskies.modabi.io.xml/src/uk/co/strangeskies/modabi/io/xml/XmlInterface.java b/uk.co.strangeskies.modabi.io.xml/src/uk/co/strangeskies/modabi/io/xml/XmlInterface.java index d06481ce..87630a33 100644 --- a/uk.co.strangeskies.modabi.io.xml/src/uk/co/strangeskies/modabi/io/xml/XmlInterface.java +++ b/uk.co.strangeskies.modabi.io.xml/src/uk/co/strangeskies/modabi/io/xml/XmlInterface.java @@ -26,12 +26,12 @@ import org.osgi.service.component.annotations.Component; -import uk.co.strangeskies.modabi.io.structured.DataInterface; +import uk.co.strangeskies.modabi.io.structured.StructuredDataFormat; import uk.co.strangeskies.modabi.io.structured.StructuredDataSource; import uk.co.strangeskies.modabi.io.structured.StructuredDataTarget; @Component(property = "formatId=" + XmlInterface.XML_ID) -public class XmlInterface implements DataInterface { +public class XmlInterface implements StructuredDataFormat { public static final String XML_ID = "xml"; @Override diff --git a/uk.co.strangeskies.modabi.mapping/bnd.bnd b/uk.co.strangeskies.modabi.mapping/bnd.bnd index 835e49d4..128f1cdc 100644 --- a/uk.co.strangeskies.modabi.mapping/bnd.bnd +++ b/uk.co.strangeskies.modabi.mapping/bnd.bnd @@ -21,4 +21,5 @@ Export-Package: uk.co.strangeskies.modabi.mapping uk.co.strangeskies.modabi.bnd.xml.ModabiXmlRegistration -includeresource: \ - META-INF/modabi=schemata + META-INF/modabi=modabi/schemata, \ + META-INF/modabi=modabi/mappings diff --git a/uk.co.strangeskies.modabi.mapping/modabi/mappings/MappingTest.xml b/uk.co.strangeskies.modabi.mapping/modabi/mappings/MappingTest.xml new file mode 100644 index 00000000..44760952 --- /dev/null +++ b/uk.co.strangeskies.modabi.mapping/modabi/mappings/MappingTest.xml @@ -0,0 +1,15 @@ + + + + + + + + + + + diff --git a/uk.co.strangeskies.modabi.mapping/schemata/MappingSchema.xml b/uk.co.strangeskies.modabi.mapping/modabi/schemata/MappingSchema.xml similarity index 91% rename from uk.co.strangeskies.modabi.mapping/schemata/MappingSchema.xml rename to uk.co.strangeskies.modabi.mapping/modabi/schemata/MappingSchema.xml index 497e53d1..607fde6f 100644 --- a/uk.co.strangeskies.modabi.mapping/schemata/MappingSchema.xml +++ b/uk.co.strangeskies.modabi.mapping/modabi/schemata/MappingSchema.xml @@ -17,7 +17,7 @@ diff --git a/uk.co.strangeskies.modabi.mapping/src/uk/co/strangeskies/modabi/mapping/Mapping.java b/uk.co.strangeskies.modabi.mapping/src/uk/co/strangeskies/modabi/mapping/Mapping.java index cdd00679..bee9e3ad 100644 --- a/uk.co.strangeskies.modabi.mapping/src/uk/co/strangeskies/modabi/mapping/Mapping.java +++ b/uk.co.strangeskies.modabi.mapping/src/uk/co/strangeskies/modabi/mapping/Mapping.java @@ -4,6 +4,13 @@ import uk.co.strangeskies.modabi.schema.Model; +/* + * Ways of doing this: + * + * 1) Unbind to structured data, map structured data to other structured data, rebind + * + * 2) Unbind to objects at specified nodes, rebind objects to certain nodes of another schema. + */ public interface Mapping { Model fromModel(); diff --git a/uk.co.strangeskies.modabi.mapping/src/uk/co/strangeskies/modabi/mapping/MappingBuilder.java b/uk.co.strangeskies.modabi.mapping/src/uk/co/strangeskies/modabi/mapping/MappingBuilder.java new file mode 100644 index 00000000..4ca6589b --- /dev/null +++ b/uk.co.strangeskies.modabi.mapping/src/uk/co/strangeskies/modabi/mapping/MappingBuilder.java @@ -0,0 +1,15 @@ +package uk.co.strangeskies.modabi.mapping; + +import java.util.List; + +import uk.co.strangeskies.modabi.schema.Model; + +public interface MappingBuilder { + Model fromModel(); + + Model toModel(); + + T map(F from); + + List> children(); +}