[jboss-cvs] JBossAS SVN: r70429 - projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/deployment/xml/test.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Wed Mar 5 10:16:22 EST 2008
Author: adrian at jboss.org
Date: 2008-03-05 10:16:22 -0500 (Wed, 05 Mar 2008)
New Revision: 70429
Added:
projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/deployment/xml/test/AbstractBuilderTest.java
projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/deployment/xml/test/DebugElementHandler.java
projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/deployment/xml/test/JBossXBTestDelegate.java
projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/deployment/xml/test/SchemaPrinter.java
projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/deployment/xml/test/TestSchemaResolver.java
Modified:
projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/deployment/xml/test/AbstractMCTest.java
Log:
Don't depend upon container to provide JBossXB test support - that's not its job :-)
Added: projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/deployment/xml/test/AbstractBuilderTest.java
===================================================================
--- projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/deployment/xml/test/AbstractBuilderTest.java (rev 0)
+++ projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/deployment/xml/test/AbstractBuilderTest.java 2008-03-05 15:16:22 UTC (rev 70429)
@@ -0,0 +1,269 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2006, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.test.kernel.deployment.xml.test;
+
+import java.net.URL;
+import java.util.Collection;
+
+import org.jboss.test.AbstractTestCaseWithSetup;
+import org.jboss.test.AbstractTestDelegate;
+import org.jboss.util.UnreachableStatementException;
+import org.jboss.xb.binding.sunday.unmarshalling.DefaultHandlers;
+import org.jboss.xb.binding.sunday.unmarshalling.DefaultSchemaResolver;
+import org.jboss.xb.binding.sunday.unmarshalling.ParticleBinding;
+import org.jboss.xb.binding.sunday.unmarshalling.SchemaBinding;
+import org.jboss.xb.binding.sunday.unmarshalling.SchemaBindingResolver;
+import org.jboss.xb.binding.sunday.unmarshalling.SequenceBinding;
+import org.jboss.xb.binding.sunday.unmarshalling.TermBinding;
+import org.jboss.xb.binding.sunday.unmarshalling.ParticleHandler;
+import org.jboss.xb.builder.JBossXBBuilder;
+
+/**
+ * AbstractBuilderTest.
+ *
+ * @author <a href="ales.justin at jboss.com">Ales Justin</a>
+ * @author <a href="adrian at jboss.com">Adrian Brock</a>
+ * @version $Revision: 1.1 $
+ */
+public class AbstractBuilderTest extends AbstractTestCaseWithSetup
+{
+ protected String rootName = getRootName();
+
+ private static final DebugElementHandler DEBUG_ELEMENT_HANDLER = new DebugElementHandler();
+ private ParticleHandler elementHandler;
+ private ParticleHandler simpleHandler;
+
+ public AbstractBuilderTest(String name)
+ {
+ super(name);
+ }
+
+ /**
+ * Setup the test delegate
+ *
+ * @param clazz the class
+ * @return the delegate
+ * @throws Exception for any error
+ */
+ public static AbstractTestDelegate getDelegate(Class<?> clazz) throws Exception
+ {
+ return new JBossXBTestDelegate(clazz);
+ }
+
+ protected JBossXBTestDelegate getJBossXBDelegate()
+ {
+ return (JBossXBTestDelegate) getDelegate();
+ }
+
+ @Override
+ protected void setUp() throws Exception
+ {
+ elementHandler = DefaultHandlers.ELEMENT_HANDLER;
+ simpleHandler = DefaultHandlers.SIMPLE_HANDLER;
+ DefaultHandlers.ELEMENT_HANDLER = DEBUG_ELEMENT_HANDLER;
+ DefaultHandlers.SIMPLE_HANDLER = DefaultHandlers.ELEMENT_HANDLER;
+
+ super.setUp();
+ configureLogging();
+ }
+
+ protected void tearDown() throws Exception
+ {
+ super.tearDown();
+
+ DefaultHandlers.ELEMENT_HANDLER = elementHandler;
+ elementHandler = null;
+ DefaultHandlers.SIMPLE_HANDLER = simpleHandler;
+ simpleHandler = null;
+ }
+
+ protected <T> T unmarshalObjectFromSchema(Class<T> expected) throws Exception
+ {
+ DefaultSchemaResolver resolver = new DefaultSchemaResolver();
+ // TODO this is a mess
+ String nsURI = "http://www.jboss.org/test/xml/" + rootName;
+ String packageName = getClass().getPackage().getName();
+ packageName = packageName.replace(".", "/");
+ String name = getClass().getName();
+ int dot = name.lastIndexOf('.');
+ if (dot != -1)
+ name = name.substring(dot + 1);
+ dot = name.lastIndexOf("UnitTestCase");
+ if (dot != -1)
+ name = name.substring(0, dot);
+ String testXsd = packageName + '/' + name + ".xsd";
+ resolver.addSchemaLocation(nsURI, testXsd);
+ resolver.addSchemaInitializer(nsURI, JBossXBBuilder.newInitializer(expected));
+
+ String testXml = findTestXml();
+ Object o = unmarshal(testXml, expected, resolver);
+ assertNotNull(o);
+ getLog().debug("Unmarshalled " + o + " of type " + o.getClass().getName());
+ return expected.cast(o);
+ }
+
+ protected <T, U> T unmarshalObject(Class<T> expected, Class<U> reference, Class<?>... others) throws Exception
+ {
+ TestSchemaResolver resolver = new TestSchemaResolver();
+
+ SchemaBinding schemaBinding = JBossXBBuilder.build(reference);
+ resolver.addSchemaBinding(schemaBinding);
+ if (others != null)
+ {
+ for (Class<?> other : others)
+ {
+ SchemaBinding otherBinding = JBossXBBuilder.build(other);
+ resolver.addSchemaBinding(otherBinding);
+ }
+ }
+
+ String testXml = findTestXml();
+ Object o = unmarshal(testXml, schemaBinding);
+ assertNotNull(o);
+ getLog().debug("Unmarshalled " + o + " of type " + o.getClass().getName());
+ try
+ {
+ return expected.cast(o);
+ }
+ catch (ClassCastException e)
+ {
+ fail("Expected " + expected.getName() + " got " + o.getClass().getName());
+ throw new UnreachableStatementException();
+ }
+ }
+
+ /**
+ * Unmarshal some xml
+ *
+ * @param name the name
+ * @param schema the schema
+ * @return the unmarshalled object
+ * @throws Exception for any error
+ */
+ protected Object unmarshal(String name, SchemaBinding schema) throws Exception
+ {
+ String url = findXML(name);
+ return getJBossXBDelegate().unmarshal(url, schema);
+ }
+
+ protected <T, U> T unmarshalObject(Class<T> expected, Class<U> reference) throws Exception
+ {
+ return unmarshalObject(expected, reference, null);
+ }
+
+ protected <T> T unmarshalObject(Class<T> expected) throws Exception
+ {
+ return unmarshalObject(expected, expected, null);
+ }
+
+ /**
+ * Unmarshal some xml
+ *
+ * @param name the name
+ * @param expected the expected type
+ * @param resolver the resolver
+ * @return the unmarshalled object
+ * @throws Exception for any error
+ */
+ protected Object unmarshal(String name, Class<?> expected, SchemaBindingResolver resolver) throws Exception
+ {
+ Object object = unmarshal(name, resolver);
+ if (object == null)
+ fail("No object from " + name);
+ assertTrue("Object '" + object + "' cannot be assigned to " + expected.getName(), expected.isAssignableFrom(object.getClass()));
+ return object;
+ }
+
+ /**
+ * Unmarshal some xml
+ *
+ * @param name the name
+ * @param resolver the resolver
+ * @return the unmarshalled object
+ * @throws Exception for any error
+ */
+ protected Object unmarshal(String name, SchemaBindingResolver resolver) throws Exception
+ {
+ String url = findXML(name);
+ return getJBossXBDelegate().unmarshal(url, resolver);
+ }
+
+ protected String findTestXml()
+ {
+ String result = rootName + "_" + getName() + ".xml";
+ if (getResource(result) == null)
+ result = rootName + ".xml";
+ return result;
+ }
+
+ /**
+ * Get the package root name
+ *
+ * @return the root name
+ */
+ protected String getRootName()
+ {
+ String longName = getClass().getName();
+ int dot = longName.lastIndexOf('.');
+ if (dot != -1)
+ longName = longName.substring(dot + 1);
+ dot = longName.lastIndexOf("UnitTestCase");
+ if (dot != -1)
+ longName = longName.substring(0, dot);
+ return longName;
+ }
+
+ @SuppressWarnings("unchecked")
+ protected TermBinding assertSingleSequence(TermBinding term)
+ {
+ assertNotNull(term);
+ assertTrue(term instanceof SequenceBinding);
+ SequenceBinding sequence = (SequenceBinding) term;
+ Collection<ParticleBinding> particles = sequence.getParticles();
+ assertTrue(particles.size() == 1);
+ ParticleBinding particle = particles.iterator().next();
+ term = particle.getTerm();
+ assertNotNull(term);
+ return term;
+ }
+
+ /**
+ * Find the xml
+ *
+ * @param name the name
+ * @return the url of the xml
+ */
+ protected String findXML(String name)
+ {
+ URL url = getResource(name);
+ if (url == null)
+ fail(name + " not found");
+ return url.toString();
+ }
+
+ @Override
+ public void configureLogging()
+ {
+ //enableTrace("org.jboss.xb");
+ //enableTrace("org.jboss.test.xb");
+ }
+}
Modified: projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/deployment/xml/test/AbstractMCTest.java
===================================================================
--- projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/deployment/xml/test/AbstractMCTest.java 2008-03-05 14:54:45 UTC (rev 70428)
+++ projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/deployment/xml/test/AbstractMCTest.java 2008-03-05 15:16:22 UTC (rev 70429)
@@ -50,7 +50,6 @@
import org.jboss.beans.metadata.spi.factory.GenericBeanFactoryMetaData;
import org.jboss.javabean.plugins.jaxb.JavaBean10;
import org.jboss.kernel.plugins.deployment.AbstractKernelDeployment;
-import org.jboss.test.xb.builder.AbstractBuilderTest;
import org.jboss.xb.binding.JBossXBException;
/**
Added: projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/deployment/xml/test/DebugElementHandler.java
===================================================================
--- projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/deployment/xml/test/DebugElementHandler.java (rev 0)
+++ projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/deployment/xml/test/DebugElementHandler.java 2008-03-05 15:16:22 UTC (rev 70429)
@@ -0,0 +1,54 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2006, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.test.kernel.deployment.xml.test;
+
+import javax.xml.namespace.NamespaceContext;
+import javax.xml.namespace.QName;
+
+import org.jboss.xb.binding.sunday.unmarshalling.ParticleBinding;
+import org.jboss.xb.binding.sunday.unmarshalling.ParticleHandler;
+import org.xml.sax.Attributes;
+
+/**
+ * DebugElementHandler.
+ *
+ * @author <a href="ales.justin at jboss.com">Ales Justin</a>
+ * @author <a href="adrian at jboss.com">Adrian Brock</a>
+ * @version $Revision: 1.1 $
+ */
+public class DebugElementHandler implements ParticleHandler
+{
+ public Object startParticle(Object parent, QName elementName, ParticleBinding particle, Attributes attrs, NamespaceContext nsCtx)
+ {
+ throw new UnsupportedOperationException("Using Default Handler! startParticle: " + elementName + " " + particle);
+ }
+
+ public void setParent(Object parent, Object o, QName elementName, ParticleBinding particle, ParticleBinding parentParticle)
+ {
+ throw new UnsupportedOperationException("Using Default Handler! setParent: " + elementName + " " + particle);
+ }
+
+ public Object endParticle(Object o, QName elementName, ParticleBinding particle)
+ {
+ throw new UnsupportedOperationException("Using Default Handler! endParticle: " + elementName + " " + particle);
+ }
+}
Added: projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/deployment/xml/test/JBossXBTestDelegate.java
===================================================================
--- projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/deployment/xml/test/JBossXBTestDelegate.java (rev 0)
+++ projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/deployment/xml/test/JBossXBTestDelegate.java 2008-03-05 15:16:22 UTC (rev 70429)
@@ -0,0 +1,171 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2006, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.test.kernel.deployment.xml.test;
+
+import java.lang.reflect.Method;
+import java.net.URL;
+
+import org.jboss.net.protocol.URLStreamHandlerFactory;
+import org.jboss.test.AbstractTestDelegate;
+import org.jboss.xb.binding.Unmarshaller;
+import org.jboss.xb.binding.UnmarshallerFactory;
+import org.jboss.xb.binding.sunday.unmarshalling.DefaultSchemaResolver;
+import org.jboss.xb.binding.sunday.unmarshalling.SchemaBinding;
+import org.jboss.xb.binding.sunday.unmarshalling.SchemaBindingResolver;
+import org.jboss.xb.binding.sunday.unmarshalling.XsdBinder;
+
+/**
+ * JBossXBTestDelegate.
+ *
+ * @author <a href="ales.justin at jboss.com">Ales Justin</a>
+ * @author <a href="adrian at jboss.com">Adrian Brock</a>
+ * @version $Revision: 40492 $
+ */
+public class JBossXBTestDelegate extends AbstractTestDelegate
+{
+ /** Whether initialization has been done */
+ private static boolean done = false;
+
+ /** The unmarshaller factory */
+ protected UnmarshallerFactory unmarshallerFactory;
+
+ /** The resolver */
+ protected SchemaBindingResolver defaultResolver;
+
+ /**
+ * Initialize
+ */
+ public synchronized static void init()
+ {
+ if (done)
+ return;
+ done = true;
+ URL.setURLStreamHandlerFactory(new URLStreamHandlerFactory());
+ URLStreamHandlerFactory.preload();
+ String handlerPkgs = System.getProperty("java.protocol.handler.pkgs");
+ if (handlerPkgs != null)
+ handlerPkgs += "|org.jboss.net.protocol";
+ else
+ handlerPkgs = "org.jboss.net.protocol";
+ System.setProperty("java.protocol.handler.pkgs", handlerPkgs);
+ }
+
+ /**
+ * Create a new JBossXBTestDelegate.
+ *
+ * @param clazz the test class
+ */
+ public JBossXBTestDelegate(Class<?> clazz)
+ {
+ super(clazz);
+ }
+
+ @Override
+ public void setUp() throws Exception
+ {
+ super.setUp();
+ init();
+ unmarshallerFactory = UnmarshallerFactory.newInstance();
+ initResolver();
+ }
+
+ protected void initResolver() throws Exception
+ {
+ try
+ {
+ Method method = clazz.getMethod("initResolver", null);
+ defaultResolver = (SchemaBindingResolver) method.invoke(null, null);
+ }
+ catch (NoSuchMethodException ignored)
+ {
+ defaultResolver = new DefaultSchemaResolver();
+ }
+ }
+
+ /**
+ * Unmarshal an object
+ *
+ * @param url the url
+ * @param resolver the resolver
+ * @return the object
+ * @throws Exception for any error
+ */
+ public Object unmarshal(String url, SchemaBindingResolver resolver) throws Exception
+ {
+ if (resolver == null)
+ resolver = defaultResolver;
+
+ long start = System.currentTimeMillis();
+ Unmarshaller unmarshaller = unmarshallerFactory.newUnmarshaller();
+ log.debug("Initialized parsing in " + (System.currentTimeMillis() - start) + "ms");
+ try
+ {
+ Object result = unmarshaller.unmarshal(url, resolver);
+ log.debug("Total parse for " + url + " took " + (System.currentTimeMillis() - start) + "ms");
+ return result;
+ }
+ catch (Exception e)
+ {
+ log.debug("Error during parsing: " + url, e);
+ throw e;
+ }
+ }
+
+ /**
+ * Unmarshal an object
+ *
+ * @param url the url
+ * @param schema the schema
+ * @return the object
+ * @throws Exception for any error
+ */
+ public Object unmarshal(String url, SchemaBinding schema) throws Exception
+ {
+ long start = System.currentTimeMillis();
+ Unmarshaller unmarshaller = unmarshallerFactory.newUnmarshaller();
+ log.debug("Initialized parsing in " + (System.currentTimeMillis() - start) + "ms");
+ try
+ {
+ Object result = unmarshaller.unmarshal(url, schema);
+ log.debug("Total parse for " + url + " took " + (System.currentTimeMillis() - start) + "ms");
+ return result;
+ }
+ catch (Exception e)
+ {
+ log.debug("Error during parsing: " + url, e);
+ throw e;
+ }
+ }
+
+ /**
+ * Bind a schema
+ *
+ * @param url the url
+ * @param resolver the resolver
+ * @return the object
+ * @throws Exception for any error
+ */
+ public SchemaBinding bind(String url, SchemaBindingResolver resolver) throws Exception
+ {
+ return XsdBinder.bind(url, resolver);
+ }
+}
Added: projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/deployment/xml/test/SchemaPrinter.java
===================================================================
--- projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/deployment/xml/test/SchemaPrinter.java (rev 0)
+++ projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/deployment/xml/test/SchemaPrinter.java 2008-03-05 15:16:22 UTC (rev 70429)
@@ -0,0 +1,277 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2006, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.test.kernel.deployment.xml.test;
+
+import java.util.Collection;
+import java.util.Iterator;
+
+import javax.xml.XMLConstants;
+
+import org.jboss.xb.binding.sunday.unmarshalling.AllBinding;
+import org.jboss.xb.binding.sunday.unmarshalling.AttributeBinding;
+import org.jboss.xb.binding.sunday.unmarshalling.ChoiceBinding;
+import org.jboss.xb.binding.sunday.unmarshalling.ElementBinding;
+import org.jboss.xb.binding.sunday.unmarshalling.ModelGroupBinding;
+import org.jboss.xb.binding.sunday.unmarshalling.ParticleBinding;
+import org.jboss.xb.binding.sunday.unmarshalling.SchemaBinding;
+import org.jboss.xb.binding.sunday.unmarshalling.SequenceBinding;
+import org.jboss.xb.binding.sunday.unmarshalling.TermBinding;
+import org.jboss.xb.binding.sunday.unmarshalling.TypeBinding;
+import org.jboss.xb.binding.sunday.unmarshalling.WildcardBinding;
+
+/**
+ * SchemaPrinter.
+ *
+ * TODO finish this off properly
+ * @author <a href="ales.justin at jboss.com">Ales Justin</a>
+ * @author <a href="adrian at jboss.com">Adrian Brock</a>
+ * @version $Revision: 1.1 $
+ */
+public class SchemaPrinter
+{
+ @SuppressWarnings("unchecked")
+ public static String printSchema(SchemaBinding schemaBinding)
+ {
+ String nsURI = (String) schemaBinding.getNamespaces().iterator().next();
+ StringBuilder builder = new StringBuilder();
+ builder.append("<xsd:schema xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"\n");
+ if (XMLConstants.NULL_NS_URI.equals(nsURI) == false)
+ {
+ builder.append(" targetNamespace=\"").append(nsURI).append("\"\n");
+ builder.append(" xmlns=\"").append(nsURI).append("\"\n");
+ }
+ builder.append(">\n");
+ int pad=3;
+ Iterator<ParticleBinding> elements = schemaBinding.getElementParticles();
+ while (elements != null && elements.hasNext())
+ printElement(builder, pad, elements.next());
+ Iterator<TypeBinding> types = schemaBinding.getTypes();
+ while (types != null && types.hasNext())
+ printType(builder, pad, types.next());
+ builder.append("</xsd:schema>");
+ return builder.toString();
+ }
+
+ public static void printElement(StringBuilder builder, int pad, ParticleBinding particle)
+ {
+ ElementBinding element = (ElementBinding) particle.getTerm();
+ pad(builder, pad);
+ builder.append("<element name=\"").append(element.getQName()).append("\"");
+ int minOccurs = particle.getMinOccurs();
+ if (minOccurs != 1)
+ builder.append(" minOccurs=\"").append(minOccurs).append("\"");
+ int maxOccurs = particle.getMaxOccurs();
+ if (maxOccurs != 1)
+ {
+ builder.append(" maxOccurs=\"");
+ if (maxOccurs == -1)
+ builder.append("unbounded");
+ else
+ builder.append(maxOccurs);
+ builder.append("\"");
+ }
+ TypeBinding type = element.getType();
+ if (type != null && type.getQName() != null)
+ builder.append(" type=\"").append(type.getQName()).append("\"");
+ builder.append(">");
+ if (type != null && type.getQName() == null)
+ printAnonymousType(builder, pad + 3, type);
+ pad(builder, pad);
+ builder.append("</element>");
+ }
+
+ public static void printAnonymousType(StringBuilder builder, int pad, TypeBinding type)
+ {
+ pad(builder, pad);
+ builder.append("<complexType>");
+ TypeBinding baseTypeBinding = type.getBaseType();
+ String qName;
+ if (baseTypeBinding != null)
+ qName = baseTypeBinding.getQName().toString();
+ else
+ qName = "xsd:anyType";
+ pad(builder, pad);
+ builder.append(" <restriction base=\"").append(qName).append("\">");
+ printTypeInfo(builder, pad+6, type);
+ pad(builder, pad);
+ builder.append(" </restriction>");
+ pad(builder, pad);
+ builder.append("</complexType>");
+ }
+
+ public static void printType(StringBuilder builder, int pad, TypeBinding type)
+ {
+ if (type.isSimple())
+ printSimpleType(builder, pad, type);
+ else
+ printComplexType(builder, pad, type);
+ }
+
+ public static void printSimpleType(StringBuilder builder, int pad, TypeBinding type)
+ {
+ pad(builder, pad);
+ builder.append("<simpleType name=\"").append(type.getQName()).append("\"");
+ TypeBinding baseTypeBinding = type.getBaseType();
+ String qName = null;
+ if (baseTypeBinding != null)
+ qName = baseTypeBinding.getQName().toString();
+ else
+ qName = "xsd:anySimpleType";
+ builder.append(" base=\"").append(qName).append("\">");
+ printTypeInfo(builder, pad+3, type);
+ pad(builder, pad);
+ builder.append("</simpleType>");
+ }
+
+ public static void printComplexType(StringBuilder builder, int pad, TypeBinding type)
+ {
+ pad(builder, pad);
+ builder.append("<complexType name=\"").append(type.getQName()).append("\"");
+ TypeBinding baseTypeBinding = type.getBaseType();
+ String qName = null;
+ if (baseTypeBinding != null)
+ qName = baseTypeBinding.getQName().toString();
+ else
+ qName = "xsd:anyType";
+ builder.append(" base=\"").append(qName).append("\">");
+ printTypeInfo(builder, pad+3, type);
+ pad(builder, pad);
+ builder.append("</complexType>");
+ }
+
+ @SuppressWarnings("unchecked")
+ public static void printTypeInfo(StringBuilder builder, int pad, TypeBinding type)
+ {
+ ParticleBinding particle = type.getParticle();
+ if (particle != null)
+ {
+ printParticle(builder, pad, particle);
+ }
+
+ Collection<AttributeBinding> attributes = type.getAttributes();
+ if (attributes != null)
+ {
+ for (AttributeBinding attribute : attributes)
+ {
+ printAttribute(builder, pad, attribute);
+ }
+ }
+ }
+
+ public static void printParticle(StringBuilder builder, int pad, ParticleBinding particle)
+ {
+ TermBinding term = particle.getTerm();
+ if (term instanceof ElementBinding)
+ printElement(builder, pad, particle);
+ else if (term instanceof WildcardBinding)
+ printWildcard(builder, pad, particle);
+ else if (term instanceof SequenceBinding)
+ printModel(builder, pad, particle, "sequence");
+ else if (term instanceof AllBinding)
+ printModel(builder, pad, particle, "all");
+ else if (term instanceof ChoiceBinding)
+ printModel(builder, pad, particle, "choice");
+ }
+
+ @SuppressWarnings("unchecked")
+ public static void printModel(StringBuilder builder, int pad, ParticleBinding particle, String prefix)
+ {
+ pad(builder, pad);
+ builder.append("<").append(prefix);
+ int minOccurs = particle.getMinOccurs();
+ if (minOccurs != 1)
+ builder.append(" minOccurs=\"").append(minOccurs).append("\"");
+ int maxOccurs = particle.getMaxOccurs();
+ if (maxOccurs != 1)
+ {
+ builder.append(" maxOccurs=\"");
+ if (maxOccurs == -1)
+ builder.append("unbounded");
+ else
+ builder.append(maxOccurs);
+ builder.append("\"");
+ }
+ builder.append(">");
+ ModelGroupBinding model = (ModelGroupBinding) particle.getTerm();
+ Collection<ParticleBinding> particles = model.getParticles();
+ boolean newLine = true;
+ if (particles != null)
+ {
+ for (ParticleBinding component : particles)
+ printParticle(builder, pad+3, component);
+ }
+ else
+ {
+ newLine = false;
+ }
+ pad(builder, pad, newLine);
+ builder.append("</").append(prefix).append(">");
+ }
+
+ public static void printWildcard(StringBuilder builder, int pad, ParticleBinding particle)
+ {
+ pad(builder, pad);
+ builder.append("<any");
+ int minOccurs = particle.getMinOccurs();
+ if (minOccurs != 1)
+ builder.append(" minOccurs=\"").append(minOccurs).append("\"");
+ int maxOccurs = particle.getMaxOccurs();
+ if (maxOccurs != 1)
+ {
+ builder.append(" maxOccurs=\"");
+ if (maxOccurs == -1)
+ builder.append("unbounded");
+ else
+ builder.append(maxOccurs);
+ builder.append("\"");
+ }
+ builder.append("/>");
+ }
+
+ public static void printAttribute(StringBuilder builder, int pad, AttributeBinding attribute)
+ {
+ pad(builder, pad);
+ TypeBinding typeBinding = attribute.getType();
+ String qName = null;
+ if (typeBinding != null && typeBinding.getQName() != null)
+ qName = typeBinding.getQName().toString();
+ else
+ qName = "CDATA";
+ builder.append("<attribute name=\"").append(attribute.getQName().getLocalPart()).append("\"");
+ builder.append(" type=\"").append(qName).append("\"");
+ if (attribute.getRequired() == false)
+ builder.append(" optional=\"true\"/>");
+ }
+
+ public static void pad(StringBuilder builder, int pad)
+ {
+ pad(builder, pad, true);
+ }
+
+ public static void pad(StringBuilder builder, int pad, boolean newLine)
+ {
+ if (newLine)
+ builder.append("\n");
+ for (int i = 0; i < pad; ++ i)
+ builder.append(' ');
+ }
+}
Added: projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/deployment/xml/test/TestSchemaResolver.java
===================================================================
--- projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/deployment/xml/test/TestSchemaResolver.java (rev 0)
+++ projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/deployment/xml/test/TestSchemaResolver.java 2008-03-05 15:16:22 UTC (rev 70429)
@@ -0,0 +1,78 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2006, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.test.kernel.deployment.xml.test;
+
+import java.util.HashMap;
+
+import org.jboss.logging.Logger;
+import org.jboss.xb.binding.sunday.unmarshalling.SchemaBinding;
+import org.jboss.xb.binding.sunday.unmarshalling.SchemaBindingResolver;
+import org.w3c.dom.ls.LSInput;
+
+/**
+ * TestSchemaResolver.
+ *
+ * @author <a href="ales.justin at jboss.com">Ales Justin</a>
+ * @author <a href="adrian at jboss.com">Adrian Brock</a>
+ * @version $Revision: 1.1 $
+ */
+public class TestSchemaResolver implements SchemaBindingResolver
+{
+ private static final Logger log = Logger.getLogger(TestSchemaResolver.class);
+
+ private HashMap<String, SchemaBinding> bindings = new HashMap<String, SchemaBinding>();
+
+ public String getBaseURI()
+ {
+ return null;
+ }
+
+ public void addSchemaBinding(SchemaBinding schemaBinding)
+ {
+ schemaBinding.setSchemaResolver(this);
+ String nsURI = (String) schemaBinding.getNamespaces().iterator().next();
+ bindings.put(nsURI, schemaBinding);
+ if (log.isTraceEnabled())
+ {
+ String schema = SchemaPrinter.printSchema(schemaBinding);
+ log.trace("Bound: " + nsURI + "\n" + schema);
+ }
+ }
+
+ public SchemaBinding resolve(String nsUri, String baseURI, String schemaLocation)
+ {
+ SchemaBinding result = bindings.get(nsUri);
+ if (result == null)
+ throw new RuntimeException("Schema not bound: " + nsUri + " available: " + bindings.keySet());
+ return result;
+ }
+
+ public LSInput resolveAsLSInput(String nsUri, String baseUri, String schemaLocation)
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ public void setBaseURI(String baseURI)
+ {
+ throw new org.jboss.util.NotImplementedException("setBaseURI");
+ }
+}
More information about the jboss-cvs-commits
mailing list