diff --git a/compiler/src/main/java/org/hisrc/jsonix/xml/xsom/MultiplicityCounterNG.java b/compiler/src/main/java/org/hisrc/jsonix/xml/xsom/MultiplicityCounterNG.java new file mode 100644 index 0000000..1350d03 --- /dev/null +++ b/compiler/src/main/java/org/hisrc/jsonix/xml/xsom/MultiplicityCounterNG.java @@ -0,0 +1,71 @@ +package org.hisrc.jsonix.xml.xsom; + +import static com.sun.tools.xjc.model.Multiplicity.ONE; +import static com.sun.tools.xjc.model.Multiplicity.ZERO; + +import java.math.BigInteger; + +import com.sun.tools.xjc.model.Multiplicity; +import com.sun.xml.xsom.XSElementDecl; +import com.sun.xml.xsom.XSModelGroup; +import com.sun.xml.xsom.XSModelGroupDecl; +import com.sun.xml.xsom.XSParticle; +import com.sun.xml.xsom.XSWildcard; +import com.sun.xml.xsom.visitor.XSTermFunction; + +public class MultiplicityCounterNG implements XSTermFunction { + + public static final XSTermFunction INSTANCE = new MultiplicityCounterNG(); + + private MultiplicityCounterNG() { + } + + public Multiplicity particle(XSParticle p) { + Multiplicity m = p.getTerm().apply(this); + + BigInteger max; + if (m.max == null + || (BigInteger.valueOf(XSParticle.UNBOUNDED).equals(p + .getMaxOccurs()))) + max = null; + else + max = p.getMaxOccurs(); + + return Multiplicity.multiply(m, + Multiplicity.create(p.getMinOccurs(), max)); + } + + public Multiplicity wildcard(XSWildcard wc) { + return ONE; + } + + public Multiplicity modelGroupDecl(XSModelGroupDecl decl) { + return modelGroup(decl.getModelGroup()); + } + + public Multiplicity modelGroup(XSModelGroup group) { + boolean isChoice = group.getCompositor() == XSModelGroup.CHOICE; + + Multiplicity r = null; + + for (XSParticle p : group.getChildren()) { + Multiplicity m = particle(p); + + if (r == null) { + r = m; + continue; + } + if (isChoice) { + r = Multiplicity.choice(r, m); + } else { + r = Multiplicity.group(r, m); + } + } + return r; + } + + public Multiplicity elementDecl(XSElementDecl decl) { + return ONE; + } + +} diff --git a/compiler/src/main/java/org/hisrc/jsonix/xml/xsom/ParticleMultiplicityCounter.java b/compiler/src/main/java/org/hisrc/jsonix/xml/xsom/ParticleMultiplicityCounter.java new file mode 100644 index 0000000..6c59df2 --- /dev/null +++ b/compiler/src/main/java/org/hisrc/jsonix/xml/xsom/ParticleMultiplicityCounter.java @@ -0,0 +1,45 @@ +package org.hisrc.jsonix.xml.xsom; + +import java.math.BigInteger; + +import org.hisrc.xml.xsom.DefaultFunctionImpl; + +import com.sun.tools.xjc.model.Multiplicity; +import com.sun.xml.xsom.XSAttributeUse; +import com.sun.xml.xsom.XSParticle; +import com.sun.xml.xsom.visitor.XSTermFunction; + +public class ParticleMultiplicityCounter extends + DefaultFunctionImpl { + + public static final ParticleMultiplicityCounter INSTANCE = new ParticleMultiplicityCounter(); + + private final XSTermFunction counter = MultiplicityCounterNG.INSTANCE; + + protected ParticleMultiplicityCounter() { + super(); + } + + @Override + public Multiplicity particle(XSParticle p) { + + Multiplicity m = p.getTerm().apply(this.counter); + + BigInteger max; + if (m.max == null + || (BigInteger.valueOf(XSParticle.UNBOUNDED).equals(p + .getMaxOccurs()))) + max = null; + else + max = p.getMaxOccurs(); + + return Multiplicity.multiply(m, + Multiplicity.create(p.getMinOccurs(), max)); + } + + @Override + public Multiplicity attributeUse(XSAttributeUse use) { + return use.isRequired() ? Multiplicity.ONE : Multiplicity.OPTIONAL; + } + +} diff --git a/compiler/src/test/java/org/hisrc/jsonix/xjc/plugin/tests/jsonschemas/JsonixPluginJsonSchemasChoiceTest.java b/compiler/src/test/java/org/hisrc/jsonix/xjc/plugin/tests/jsonschemas/JsonixPluginJsonSchemasChoiceTest.java new file mode 100644 index 0000000..d27ffd9 --- /dev/null +++ b/compiler/src/test/java/org/hisrc/jsonix/xjc/plugin/tests/jsonschemas/JsonixPluginJsonSchemasChoiceTest.java @@ -0,0 +1,50 @@ +/** + * Jsonix is a JavaScript library which allows you to convert between XML + * and JavaScript object structures. + * + * Copyright (c) 2010 - 2014, Alexey Valikov, Highsource.org + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * * Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.hisrc.jsonix.xjc.plugin.tests.jsonschemas; + +import org.hisrc.xml.bind.model.util.MModelInfoLoader; +import org.junit.Test; + +public class JsonixPluginJsonSchemasChoiceTest { + + @Test + public void elements01() throws Exception { + MModelInfoLoader.INSTANCE.loadModel("jsonschema/choice/elements01.xsd"); + } + + @Test + public void elements02() throws Exception { + MModelInfoLoader.INSTANCE.loadModel("jsonschema/choice/elements02.xsd"); + } +} diff --git a/compiler/src/test/resources/jsonschema/choice/elements01.xsd b/compiler/src/test/resources/jsonschema/choice/elements01.xsd new file mode 100644 index 0000000..23a396c --- /dev/null +++ b/compiler/src/test/resources/jsonschema/choice/elements01.xsd @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/compiler/src/test/resources/jsonschema/choice/elements02.xsd b/compiler/src/test/resources/jsonschema/choice/elements02.xsd new file mode 100644 index 0000000..e8d2c42 --- /dev/null +++ b/compiler/src/test/resources/jsonschema/choice/elements02.xsd @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/compiler/src/test/resources/jsonschema/minmaxoccurs/anyAttribute01.xsd b/compiler/src/test/resources/jsonschema/minmaxoccurs/anyAttribute01.xsd new file mode 100644 index 0000000..6080bd6 --- /dev/null +++ b/compiler/src/test/resources/jsonschema/minmaxoccurs/anyAttribute01.xsd @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/compiler/src/test/resources/jsonschema/minmaxoccurs/anyElement01.xsd b/compiler/src/test/resources/jsonschema/minmaxoccurs/anyElement01.xsd new file mode 100644 index 0000000..901eba6 --- /dev/null +++ b/compiler/src/test/resources/jsonschema/minmaxoccurs/anyElement01.xsd @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/compiler/src/test/resources/jsonschema/minmaxoccurs/attribute01.xsd b/compiler/src/test/resources/jsonschema/minmaxoccurs/attribute01.xsd new file mode 100644 index 0000000..bdd74df --- /dev/null +++ b/compiler/src/test/resources/jsonschema/minmaxoccurs/attribute01.xsd @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/compiler/src/test/resources/jsonschema/minmaxoccurs/element01.xsd b/compiler/src/test/resources/jsonschema/minmaxoccurs/element01.xsd new file mode 100644 index 0000000..754098a --- /dev/null +++ b/compiler/src/test/resources/jsonschema/minmaxoccurs/element01.xsd @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/compiler/src/test/resources/jsonschema/minmaxoccurs/elementRef01.xsd b/compiler/src/test/resources/jsonschema/minmaxoccurs/elementRef01.xsd new file mode 100644 index 0000000..d33fbef --- /dev/null +++ b/compiler/src/test/resources/jsonschema/minmaxoccurs/elementRef01.xsd @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/compiler/src/test/resources/jsonschema/minmaxoccurs/elementRefs01.xsd b/compiler/src/test/resources/jsonschema/minmaxoccurs/elementRefs01.xsd new file mode 100644 index 0000000..3d5a068 --- /dev/null +++ b/compiler/src/test/resources/jsonschema/minmaxoccurs/elementRefs01.xsd @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/compiler/src/test/resources/jsonschema/minmaxoccurs/elements01.xsd b/compiler/src/test/resources/jsonschema/minmaxoccurs/elements01.xsd new file mode 100644 index 0000000..348f1ab --- /dev/null +++ b/compiler/src/test/resources/jsonschema/minmaxoccurs/elements01.xsd @@ -0,0 +1,33 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/compiler/src/test/resources/jsonschema/minmaxoccurs/schema02.xsd b/compiler/src/test/resources/jsonschema/minmaxoccurs/schema02.xsd new file mode 100644 index 0000000..ea7ea1d --- /dev/null +++ b/compiler/src/test/resources/jsonschema/minmaxoccurs/schema02.xsd @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/compiler/src/test/resources/jsonschema/minmaxoccurs/value01.xsd b/compiler/src/test/resources/jsonschema/minmaxoccurs/value01.xsd new file mode 100644 index 0000000..1e83bca --- /dev/null +++ b/compiler/src/test/resources/jsonschema/minmaxoccurs/value01.xsd @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + + + \ No newline at end of file