Skip to content

Commit

Permalink
Issues #40, #50.
Browse files Browse the repository at this point in the history
  • Loading branch information
highsource committed Sep 11, 2015
1 parent 254abb2 commit 5e4641b
Show file tree
Hide file tree
Showing 14 changed files with 417 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<Multiplicity> {

public static final XSTermFunction<Multiplicity> 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;
}

}
Original file line number Diff line number Diff line change
@@ -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<Multiplicity> {

public static final ParticleMultiplicityCounter INSTANCE = new ParticleMultiplicityCounter();

private final XSTermFunction<Multiplicity> 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;
}

}
Original file line number Diff line number Diff line change
@@ -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");
}
}
23 changes: 23 additions & 0 deletions compiler/src/test/resources/jsonschema/choice/elements01.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
jaxb:extensionBindingPrefixes="xjc" jaxb:version="2.0">

<xs:annotation>
<xs:appinfo>
<jaxb:globalBindings typesafeEnumBase="xs:token" />
<jaxb:schemaBindings>
<jaxb:package name="test" />
</jaxb:schemaBindings>
</xs:appinfo>
</xs:annotation>

<xs:element name="choice" type="choiceType" />
<xs:complexType name="choiceType">
<xs:choice>
<xs:element name="a" type="xs:string" minOccurs="0" />
<xs:element name="b" type="xs:integer" minOccurs="0" />
</xs:choice>
</xs:complexType>

</xs:schema>
23 changes: 23 additions & 0 deletions compiler/src/test/resources/jsonschema/choice/elements02.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
jaxb:extensionBindingPrefixes="xjc" jaxb:version="2.0">

<xs:annotation>
<xs:appinfo>
<jaxb:globalBindings typesafeEnumBase="xs:token" />
<jaxb:schemaBindings>
<jaxb:package name="test" />
</jaxb:schemaBindings>
</xs:appinfo>
</xs:annotation>

<xs:element name="choice" type="choiceType" />
<xs:complexType name="choiceType">
<xs:choice maxOccurs="50">
<xs:element name="a" type="xs:string" minOccurs="0" />
<xs:element name="b" type="xs:integer" minOccurs="0" />
</xs:choice>
</xs:complexType>

</xs:schema>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
jaxb:extensionBindingPrefixes="xjc" jaxb:version="2.0">

<xs:annotation>
<xs:appinfo>
<jaxb:globalBindings typesafeEnumBase="xs:token" />
<jaxb:schemaBindings>
<jaxb:package name="test" />
</jaxb:schemaBindings>
</xs:appinfo>
</xs:annotation>

<xs:complexType name="aa01">
<xs:anyAttribute />
</xs:complexType>
</xs:schema>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
jaxb:extensionBindingPrefixes="xjc" jaxb:version="2.0">

<xs:annotation>
<xs:appinfo>
<jaxb:globalBindings typesafeEnumBase="xs:token" />
<jaxb:schemaBindings>
<jaxb:package name="test" />
</jaxb:schemaBindings>
</xs:appinfo>
</xs:annotation>

<xs:complexType name="ae01">
<xs:sequence>
<xs:any minOccurs="2" maxOccurs="20" />
</xs:sequence>
</xs:complexType>
</xs:schema>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
jaxb:extensionBindingPrefixes="xjc" jaxb:version="2.0">

<xs:annotation>
<xs:appinfo>
<jaxb:globalBindings typesafeEnumBase="xs:token" />
<jaxb:schemaBindings>
<jaxb:package name="test" />
</jaxb:schemaBindings>
</xs:appinfo>
</xs:annotation>

<xs:complexType name="a01">
<xs:attribute name="a" type="xs:string" use="required" />
</xs:complexType>
</xs:schema>
21 changes: 21 additions & 0 deletions compiler/src/test/resources/jsonschema/minmaxoccurs/element01.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
jaxb:extensionBindingPrefixes="xjc" jaxb:version="2.0">

<xs:annotation>
<xs:appinfo>
<jaxb:globalBindings typesafeEnumBase="xs:token" />
<jaxb:schemaBindings>
<jaxb:package name="test" />
</jaxb:schemaBindings>
</xs:appinfo>
</xs:annotation>

<xs:complexType name="e01">
<xs:sequence>
<xs:element name="a" type="xs:string" minOccurs="10"
maxOccurs="20" />
</xs:sequence>
</xs:complexType>
</xs:schema>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
jaxb:extensionBindingPrefixes="xjc" jaxb:version="2.0">

<xs:annotation>
<xs:appinfo>
<jaxb:globalBindings typesafeEnumBase="xs:token" />
<jaxb:schemaBindings>
<jaxb:package name="test" />
</jaxb:schemaBindings>
</xs:appinfo>
</xs:annotation>

<xs:complexType name="er01">
<xs:sequence>
<xs:element name="a" type="xs:int" minOccurs="0"
maxOccurs="1" nillable="true" />
</xs:sequence>
</xs:complexType>

</xs:schema>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
jaxb:extensionBindingPrefixes="xjc" jaxb:version="2.0">

<xs:annotation>
<xs:appinfo>
<jaxb:globalBindings typesafeEnumBase="xs:token" />
<jaxb:schemaBindings>
<jaxb:package name="test" />
</jaxb:schemaBindings>
</xs:appinfo>
</xs:annotation>

<xs:complexType name="ers01">
<xs:sequence>
<xs:choice minOccurs="3" maxOccurs="5">
<xs:element name="a" type="xs:string" minOccurs="10"
maxOccurs="20" />
<xs:element name="b" type="xs:string" minOccurs="30"
maxOccurs="40" />
</xs:choice>
</xs:sequence>
</xs:complexType>

</xs:schema>
33 changes: 33 additions & 0 deletions compiler/src/test/resources/jsonschema/minmaxoccurs/elements01.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
jaxb:extensionBindingPrefixes="xjc" jaxb:version="2.0">

<xs:annotation>
<xs:appinfo>
<jaxb:globalBindings typesafeEnumBase="xs:token" />
<jaxb:schemaBindings>
<jaxb:package name="test" />
</jaxb:schemaBindings>
</xs:appinfo>
</xs:annotation>

<xs:complexType name="es01">
<xs:sequence>
<xs:choice minOccurs="5" maxOccurs="10">
<xs:element name="a" type="xs:string" minOccurs="10"
maxOccurs="20" />
<xs:element name="b" type="xs:int" minOccurs="30"
maxOccurs="40" />
</xs:choice>
</xs:sequence>
</xs:complexType>
<xs:complexType name="es02">
<xs:choice minOccurs="5" maxOccurs="10">
<xs:element name="a" type="xs:string" minOccurs="10"
maxOccurs="20" />
<xs:element name="b" type="xs:int" minOccurs="30"
maxOccurs="40" />
</xs:choice>
</xs:complexType>
</xs:schema>

0 comments on commit 5e4641b

Please sign in to comment.