[jboss-svn-commits] JBoss Common SVN: r3031 - jbossxb/trunk/src/main/java/org/jboss/xb/binding.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Fri Mar 6 17:52:05 EST 2009
Author: alesj
Date: 2009-03-06 17:52:05 -0500 (Fri, 06 Mar 2009)
New Revision: 3031
Added:
jbossxb/trunk/src/main/java/org/jboss/xb/binding/FeatureAware.java
jbossxb/trunk/src/main/java/org/jboss/xb/binding/JBossXBDeployerHelper.java
Modified:
jbossxb/trunk/src/main/java/org/jboss/xb/binding/Unmarshaller.java
Log:
[JBXB-191]; move JBossXBDeployerHelper
Added: jbossxb/trunk/src/main/java/org/jboss/xb/binding/FeatureAware.java
===================================================================
--- jbossxb/trunk/src/main/java/org/jboss/xb/binding/FeatureAware.java (rev 0)
+++ jbossxb/trunk/src/main/java/org/jboss/xb/binding/FeatureAware.java 2009-03-06 22:52:05 UTC (rev 3031)
@@ -0,0 +1,39 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file 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.xb.binding;
+
+/**
+ * We can set the feature on this impl.
+ *
+ * @author <a href="mailto:ales.justin at jboss.org">Ales Justin</a>
+ */
+public interface FeatureAware
+{
+ /**
+ * Apply feature.
+ *
+ * @param featureName the feature name
+ * @param flag the feature flag
+ * @throws Exception for any error
+ */
+ void setFeature(String featureName, boolean flag) throws Exception;
+}
Added: jbossxb/trunk/src/main/java/org/jboss/xb/binding/JBossXBDeployerHelper.java
===================================================================
--- jbossxb/trunk/src/main/java/org/jboss/xb/binding/JBossXBDeployerHelper.java (rev 0)
+++ jbossxb/trunk/src/main/java/org/jboss/xb/binding/JBossXBDeployerHelper.java 2009-03-06 22:52:05 UTC (rev 3031)
@@ -0,0 +1,235 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file 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.xb.binding;
+
+import org.jboss.logging.Logger;
+import org.jboss.xb.annotations.JBossXmlSchema;
+import org.jboss.xb.binding.resolver.MutableSchemaResolver;
+import org.jboss.xb.binding.sunday.unmarshalling.SingletonSchemaResolverFactory;
+import org.xml.sax.InputSource;
+
+/**
+ * JBossXB deployer helper.
+ *
+ * @param <T> the expected type
+ * @author <a href="ales.justin at jboss.com">Ales Justin</a>
+ */
+public class JBossXBDeployerHelper<T> implements FeatureAware
+{
+ /** The log */
+ private Logger log = Logger.getLogger(JBossXBDeployerHelper.class);
+
+ /** Unmarshaller factory */
+ private static final UnmarshallerFactory factory = UnmarshallerFactory.newInstance();
+
+ /** The singleton schema resolver */
+ private static MutableSchemaResolver resolver = SingletonSchemaResolverFactory.getInstance().getSchemaBindingResolver();
+
+ /** The output */
+ private Class<T> output;
+
+ /** Whether the Unmarshaller will use schema validation */
+ private boolean useSchemaValidation = true;
+
+ /** Whether to validate */
+ private boolean useValidation = true;
+
+ /**
+ * Create a new SchemaResolverDeployer.
+ *
+ * @param output the output
+ * @throws IllegalArgumentException for a null output
+ */
+ protected JBossXBDeployerHelper(Class<T> output)
+ {
+ if (output == null)
+ throw new IllegalArgumentException("Null output.");
+ this.output = output;
+ }
+
+ public void setFeature(String featureName, boolean flag) throws Exception
+ {
+ factory.setFeature(featureName, flag);
+ }
+
+ /**
+ * Get the useSchemaValidation.
+ *
+ * @return the useSchemaValidation.
+ */
+ public boolean isUseSchemaValidation()
+ {
+ return useSchemaValidation;
+ }
+
+ /**
+ * Set the useSchemaValidation.
+ *
+ * @param useSchemaValidation the useSchemaValidation.
+ */
+ public void setUseSchemaValidation(boolean useSchemaValidation)
+ {
+ this.useSchemaValidation = useSchemaValidation;
+ }
+
+ /**
+ * Get the useValidation.
+ *
+ * @return the useValidation.
+ */
+ public boolean isUseValidation()
+ {
+ return useValidation;
+ }
+
+ /**
+ * Set the useValidation.
+ *
+ * @param useValidation the useValidation.
+ */
+ public void setUseValidation(boolean useValidation)
+ {
+ this.useValidation = useValidation;
+ }
+
+ /**
+ * Add class binding.
+ *
+ * @param namespace the namespace
+ * @param metadata the metadata
+ */
+ public static void addClassBinding(String namespace, Class<?> metadata)
+ {
+ resolver.mapURIToClass(namespace, metadata);
+ }
+
+ /**
+ * Remove class binding.
+ *
+ * @param namespace the namespace
+ */
+ public static void removeClassBinding(String namespace)
+ {
+ resolver.removeURIToClassMapping(namespace);
+ }
+
+ /**
+ * Find the namespace on class/package
+ *
+ * @param metadata the metadata class
+ * @return jboss xml schema namespace
+ */
+ public static String findNamespace(Class<?> metadata)
+ {
+ JBossXmlSchema jBossXmlSchema = metadata.getAnnotation(JBossXmlSchema.class);
+ if (jBossXmlSchema == null)
+ {
+ Package pckg = metadata.getPackage();
+ if (pckg != null)
+ jBossXmlSchema = pckg.getAnnotation(JBossXmlSchema.class);
+ }
+ return jBossXmlSchema != null ? jBossXmlSchema.namespace() : null;
+ }
+
+ /**
+ * Parse file to output metadata.
+ *
+ * @param source the source to parse
+ * @return new metadata instance
+ * @throws Exception for any error
+ */
+ public T parse(InputSource source) throws Exception
+ {
+ return parse(output, source);
+ }
+
+ /**
+ * Parse the file to create metadata instance.
+ *
+ * @param <U> the expect type
+ * @param expectedType the expected type
+ * @param source the source
+ * @return new metadata instance
+ * @throws Exception for any error
+ */
+ public <U> U parse(Class<U> expectedType, InputSource source) throws Exception
+ {
+ if (expectedType == null)
+ throw new IllegalArgumentException("Null expected type");
+ if (source == null)
+ throw new IllegalArgumentException("Null source");
+
+ log.debug("Parsing file: " + source + " for type: " + expectedType);
+ Unmarshaller unmarshaller = factory.newUnmarshaller();
+ unmarshaller.setSchemaValidation(isUseSchemaValidation());
+ unmarshaller.setValidation(isUseValidation());
+ Object parsed = unmarshaller.unmarshal(source, resolver);
+ if (parsed == null)
+ throw new Exception("The xml " + source + " is not well formed!");
+
+ log.debug("Parsed file: " + source + " to: " + parsed);
+ return expectedType.cast(parsed);
+ }
+
+ /**
+ * Parse the file using object model factory.
+ *
+ * @param source the source to parse
+ * @param root the previous root
+ * @param omf the object model factory
+ * @return new metadata instance
+ * @throws Exception for any error
+ */
+ public T parse(InputSource source, T root, ObjectModelFactory omf) throws Exception
+ {
+ return parse(output, source, root, omf);
+ }
+
+ /**
+ * Parse the file using object model factory.
+ *
+ * @param <U> the expect type
+ * @param expectedType the expected type
+ * @param source the source to parse
+ * @param root the previous root
+ * @param omf the object model factory
+ * @return new metadata instance
+ * @throws Exception for any error
+ */
+ public <U> U parse(Class<U> expectedType, InputSource source, U root, ObjectModelFactory omf) throws Exception
+ {
+ if (source == null)
+ throw new IllegalArgumentException("Null source");
+
+ log.debug("Parsing source: " + source + " for deploymentType: " + expectedType);
+
+ Unmarshaller unmarshaller = factory.newUnmarshaller();
+ unmarshaller.setSchemaValidation(isUseSchemaValidation());
+ unmarshaller.setValidation(isUseValidation());
+ Object parsed = unmarshaller.unmarshal(source, omf, root);
+ if (parsed == null)
+ throw new Exception("The xml " + source + " is not well formed!");
+
+ log.debug("Parsed file: " + source + " to: "+parsed);
+ return expectedType.cast(parsed);
+ }
+}
\ No newline at end of file
Modified: jbossxb/trunk/src/main/java/org/jboss/xb/binding/Unmarshaller.java
===================================================================
--- jbossxb/trunk/src/main/java/org/jboss/xb/binding/Unmarshaller.java 2009-03-06 16:27:03 UTC (rev 3030)
+++ jbossxb/trunk/src/main/java/org/jboss/xb/binding/Unmarshaller.java 2009-03-06 22:52:05 UTC (rev 3031)
@@ -36,7 +36,7 @@
* @author <a href="mailto:alex at jboss.org">Alexey Loubyansky</a>
* @version <tt>$Revision$</tt>
*/
-public interface Unmarshaller
+public interface Unmarshaller extends FeatureAware
{
String VALIDATION = "http://xml.org/sax/features/validation";
String NAMESPACES = "http://xml.org/sax/features/namespaces";
@@ -52,8 +52,6 @@
void setSchemaValidation(boolean validation) throws JBossXBException;
- void setFeature(String feature, boolean value) throws JBossXBException;
-
void setEntityResolver(EntityResolver entityResolver) throws JBossXBException;
void setErrorHandler(ErrorHandler errorHandler);
More information about the jboss-svn-commits
mailing list