Danny van Bruggen avatar Danny van Bruggen committed 1d0a228

Document & reformat

Comments (0)

Files changed (1)

snelxml-core/src/main/java/com/laamella/snelxml/generator/xsdreader/XsdReader.java

 import org.apache.xerces.xs.XSSimpleTypeDefinition;
 import org.apache.xerces.xs.XSTerm;
 import org.apache.xerces.xs.XSTypeDefinition;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.w3c.dom.bootstrap.DOMImplementationRegistry;
 
 import java.util.ArrayList;
 import static org.apache.xerces.xs.XSTypeDefinition.SIMPLE_TYPE;
 
 // Useful reference: http://docs.jboss.org/jbossas/javadoc/4.0.2/org/jboss/xml/binding/XercesXsMarshaller.java.html
+
+/**
+ * Takes all useful information from an XSD and stores it in a Schema object.
+ */
 public class XsdReader {
-	public XSLoader createSchemaLoader() throws ClassCastException, ClassNotFoundException, InstantiationException, IllegalAccessException {
-		final DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
-		final XSImplementation impl = (XSImplementation) registry.getDOMImplementation("XS-Loader");
-		return impl.createXSLoader(null);
-	}
+    private Logger log = LoggerFactory.getLogger(XsdReader.class);
 
-	public Schema readXsd(String serviceName, final XSModel model) {
-		if (model == null) {
-			return new Schema(serviceName, new ArrayList<Node>());
-		}
-		final XSNamedMap components = model.getComponents(ELEMENT_DECLARATION);
+    /**
+     * @return an object that will load XSD's into XSModels that can be passed to readXsd.
+     */
+    public XSLoader createSchemaLoader() throws ClassCastException, ClassNotFoundException, InstantiationException, IllegalAccessException {
+        final DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
+        final XSImplementation impl = (XSImplementation) registry.getDOMImplementation("XS-Loader");
+        return impl.createXSLoader(null);
+    }
+
+    /**
+     * @param serviceName TODO
+     * @param model       an XSModel, probably loaded by an XSLoader created by createSchemaLoader
+     * @return a snelxml schema.
+     */
+    public Schema readXsd(String serviceName, final XSModel model) {
+        if (model == null) {
+            return new Schema(serviceName, new ArrayList<Node>());
+        }
+        final XSNamedMap components = model.getComponents(ELEMENT_DECLARATION);
 
         List<Node> topLevelElements = new ArrayList<Node>();
         final Schema structureFile = new Schema(serviceName, topLevelElements);
         return structureFile;
     }
 
-	private Node parseElementChildren(final Node parent, final XSElementDeclaration xsElement) {
-		final XSTypeDefinition typeDefinition = xsElement.getTypeDefinition();
-		switch (typeDefinition.getTypeCategory()) {
-		case SIMPLE_TYPE: {
-			final XSSimpleTypeDefinition simpleTypeDefinition = (XSSimpleTypeDefinition) typeDefinition;
-			return new Node(parent, new QName(xsElement.getNamespace(), xsElement.getName()), simpleTypeDefinition);
-		}
-		case COMPLEX_TYPE: {
-			final XSComplexTypeDefinition complexTypeDefinition = (XSComplexTypeDefinition) typeDefinition;
-			final Node element = new Node(parent, new QName(xsElement.getNamespace(), xsElement.getName()), new HashSet<Node>(), new ArrayList<Node>());
+    private Node parseElementChildren(final Node parent, final XSElementDeclaration xsElement) {
+        final XSTypeDefinition typeDefinition = xsElement.getTypeDefinition();
+        switch (typeDefinition.getTypeCategory()) {
+            case SIMPLE_TYPE: {
+                final XSSimpleTypeDefinition simpleTypeDefinition = (XSSimpleTypeDefinition) typeDefinition;
+                return new Node(parent, new QName(xsElement.getNamespace(), xsElement.getName()), simpleTypeDefinition);
+            }
+            case COMPLEX_TYPE: {
+                final XSComplexTypeDefinition complexTypeDefinition = (XSComplexTypeDefinition) typeDefinition;
+                final Node element = new Node(parent, new QName(xsElement.getNamespace(), xsElement.getName()), new HashSet<Node>(), new ArrayList<Node>());
 
                 final XSObjectList attributeUses = complexTypeDefinition.getAttributeUses();
                 for (final Object attributeObject : attributeUses) {
                     final XSAttributeUse attributeUse = (XSAttributeUse) attributeObject;
                     final XSAttributeDeclaration attributeDeclaration = attributeUse.getAttrDeclaration();
 
-				final Node attribute = new Node(element, new QName(attributeDeclaration.getNamespace(), attributeDeclaration.getName()), attributeDeclaration.getTypeDefinition());
-				element.attributes.add(attribute);
-			}
+                    final Node attribute = new Node(element, new QName(attributeDeclaration.getNamespace(), attributeDeclaration.getName()), attributeDeclaration.getTypeDefinition());
+                    element.attributes.add(attribute);
+                }
 
                 final XSParticle particle = complexTypeDefinition.getParticle();
                 if (particle != null) {
         throw new AssertionError();
     }
 
-	private void generateParticle(final XSParticle particle, final Node parentNode) {
-		final XSTerm term = particle.getTerm();
-		switch (term.getType()) {
-		case MODEL_GROUP:
-			final XSModelGroup modelGroup = (XSModelGroup) term;
-			for (final Object subParticle : modelGroup.getParticles()) {
-				final XSParticle subParticle1 = (XSParticle) subParticle;
-				generateParticle(subParticle1, parentNode);
-			}
-			break;
-		case WILDCARD:
-			// marshalled = marshalWildcard((XSWildcard) term);
-			break;
-		case ELEMENT_DECLARATION:
-			final XSElementDeclaration xsSubElement = (XSElementDeclaration) term;
-			final Node subElement = parseElementChildren(parentNode, xsSubElement);
-			parentNode.children.add(subElement);
-		}
-	}
+    private void generateParticle(final XSParticle particle, final Node parentNode) {
+        final XSTerm term = particle.getTerm();
+        switch (term.getType()) {
+            case MODEL_GROUP:
+                final XSModelGroup modelGroup = (XSModelGroup) term;
+                for (final Object subParticle : modelGroup.getParticles()) {
+                    final XSParticle subParticle1 = (XSParticle) subParticle;
+                    generateParticle(subParticle1, parentNode);
+                }
+                break;
+            case WILDCARD:
+                // marshalled = marshalWildcard((XSWildcard) term);
+                break;
+            case ELEMENT_DECLARATION:
+                final XSElementDeclaration xsSubElement = (XSElementDeclaration) term;
+                final Node subElement = parseElementChildren(parentNode, xsSubElement);
+                parentNode.children.add(subElement);
+        }
+    }
 }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.