Author: alexsmirnov
Date: 2009-09-21 17:21:50 -0400 (Mon, 21 Sep 2009)
New Revision: 15652
Added:
root/cdk/trunk/plugins/generator/src/test/resources/org/richfaces/cdk/xmlconfig/parent2.xml
Modified:
root/cdk/trunk/plugins/generator/pom.xml
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/xmlconfig/JAXBBinding.java
root/cdk/trunk/plugins/generator/src/test/java/org/richfaces/cdk/xmlconfig/FragmentParserTest.java
root/cdk/trunk/plugins/generator/src/test/resources/org/richfaces/cdk/xmlconfig/include.xml
root/cdk/trunk/plugins/generator/src/test/resources/org/richfaces/cdk/xmlconfig/parent.xml
root/cdk/trunk/plugins/xinclude/src/main/java/org/apache/cocoon/pipeline/util/dom/DOMUtils.java
Log:
got Xinclude works with JAXB unmarshaller.
Modified: root/cdk/trunk/plugins/generator/pom.xml
===================================================================
--- root/cdk/trunk/plugins/generator/pom.xml 2009-09-21 20:33:51 UTC (rev 15651)
+++ root/cdk/trunk/plugins/generator/pom.xml 2009-09-21 21:21:50 UTC (rev 15652)
@@ -110,6 +110,11 @@
<artifactId>annotations</artifactId>
<version>4.0.0-SNAPSHOT</version>
</dependency>
+ <dependency>
+ <groupId>org.richfaces.cdk</groupId>
+ <artifactId>xinclude</artifactId>
+ <version>4.0.0-SNAPSHOT</version>
+ </dependency>
<!--
<dependency> <groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId> <version>2.1</version>
Modified:
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/xmlconfig/JAXBBinding.java
===================================================================
---
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/xmlconfig/JAXBBinding.java 2009-09-21
20:33:51 UTC (rev 15651)
+++
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/xmlconfig/JAXBBinding.java 2009-09-21
21:21:50 UTC (rev 15652)
@@ -36,12 +36,15 @@
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
+import java.net.URI;
+import java.net.URISyntaxException;
import java.util.Collection;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
+import javax.xml.bind.UnmarshallerHandler;
import javax.xml.bind.util.ValidationEventCollector;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
@@ -53,6 +56,7 @@
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamResult;
+import org.apache.cocoon.pipeline.component.sax.XIncludeTransformer;
import org.richfaces.cdk.CdkContext;
import org.richfaces.cdk.CdkException;
import org.richfaces.cdk.CdkWorker;
@@ -67,6 +71,7 @@
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;
import org.xml.sax.XMLReader;
+import org.xml.sax.helpers.XMLReaderFactory;
import com.google.common.collect.ImmutableSet;
@@ -85,9 +90,6 @@
private CdkEntityResolver resolver;
- private SAXParserFactory spf;
-
-
public JAXBBinding() {
}
@@ -101,35 +103,8 @@
public void init(CdkContext context) throws CdkException {
this.context = context;
this.resolver = new CdkEntityResolver(context);
- spf = createParserFactory(context);
}
- private SAXParserFactory createParserFactory(CdkContext context)
- throws CdkException {
- SAXParserFactory spf = SAXParserFactory.newInstance(
- "org.apache.xerces.jaxp.SAXParserFactoryImpl",
- FragmentParser.class.getClassLoader());
- spf.setNamespaceAware(true);
- spf.setValidating(true);
- try {
-
spf.setFeature("http://apache.org/xml/features/validation/schema",
- true);
-
spf.setFeature("http://apache.org/xml/features/xinclude", true);
- spf.setFeature(
- "http://apache.org/xml/features/xinclude/fixup-base-uris",
- false);
- spf.setXIncludeAware(true);
- } catch (ParserConfigurationException e) {
- throw new CdkException(
- "parser does not support desired configuration", e);
- } catch (SAXNotRecognizedException e) {
- throw new CdkException("parser does not recofnize property", e);
- } catch (SAXNotSupportedException e) {
- throw new CdkException("parser feature does not supported", e);
- }
- return spf;
- }
-
public <T> T unmarshal(File file, String schemaLocation, Class<T>
bindClass)
throws CdkException {
try {
@@ -159,8 +134,10 @@
public <T> T unmarshal(String schemaLocation, Class<T> bindClass,
InputSource inputSource) throws CdkException {
T unmarshal = null;
- Source source = createXmlSource(inputSource, schemaLocation);
try {
+ XMLReader xmlReader = XMLReaderFactory.createXMLReader();
+ xmlReader.setEntityResolver(resolver);
+
xmlReader.setFeature("http://xml.org/sax/features/validation",
true);
// Setup JAXB to unmarshal
// TODO - create xinclude content handler that process xinclude directives
// and send SAX event to the unmarshaller handler.
@@ -168,15 +145,29 @@
Unmarshaller u = jc.createUnmarshaller();
ValidationEventCollector vec = new ValidationEventCollector();
u.setEventHandler(vec);
-
+ XIncludeTransformer xIncludeTransformer = new XIncludeTransformer();
+ if(null != inputSource.getSystemId()){
+ xIncludeTransformer.setBaseUri(new URI(inputSource.getSystemId()));
+ }
+ UnmarshallerHandler unmarshallerHandler = u.getUnmarshallerHandler();
+ xIncludeTransformer.setContentHandler(unmarshallerHandler);
+ xIncludeTransformer.setResolver(resolver);
+ xmlReader.setContentHandler(xIncludeTransformer);
+
xmlReader.setProperty("http://xml.org/sax/properties/lexical-handler...;,
xIncludeTransformer);
+ xmlReader.parse(inputSource);
// turn off the JAXB provider's default validation mechanism to
// avoid duplicate validation
// u.setValidating(false);
- unmarshal = (T) u.unmarshal(source);
+ unmarshal = (T) unmarshallerHandler.getResult();
} catch (JAXBException e) {
throw new CdkException("JAXB Unmarshaller error", e);
+ } catch (URISyntaxException e) {
+ throw new CdkException("Invalid XML source URI", e);
+ } catch (IOException e) {
+ throw new CdkException("JAXB Unmarshaller input error", e);
+ } catch (SAXException e) {
+ throw new CdkException("XML error", e);
} finally {
- closeSource(source);
}
return unmarshal;
}
@@ -245,31 +236,6 @@
}
}
- private Source createXmlSource(InputSource input, String schemaLocation)
- throws CdkException {
- try {
- SAXParser saxParser = spf.newSAXParser();
- if (null != schemaLocation) {
- saxParser.setProperty(CdkEntityResolver.JAXP_SCHEMA_LANGUAGE,
- CdkEntityResolver.W3C_XML_SCHEMA);
- saxParser.setProperty(CdkEntityResolver.JAXP_SCHEMA_LOCATION,
- schemaLocation);
-
- }
- XMLReader xmlReader = saxParser.getXMLReader();
- xmlReader.setEntityResolver(resolver);
- SAXSource source = new SAXSource(xmlReader, input);
- return source;
- } catch (SAXNotRecognizedException x) {
- throw new CdkException(x);
- } catch (SAXException e) {
- throw new CdkException(e);
- } catch (ParserConfigurationException e) {
- throw new CdkException(e);
- }
-
- }
-
private static final ImmutableSet<String> ignoreProperties =
ImmutableSet.of("class","extension");
/**
* <p class="changed_added_4_0">
Modified:
root/cdk/trunk/plugins/generator/src/test/java/org/richfaces/cdk/xmlconfig/FragmentParserTest.java
===================================================================
---
root/cdk/trunk/plugins/generator/src/test/java/org/richfaces/cdk/xmlconfig/FragmentParserTest.java 2009-09-21
20:33:51 UTC (rev 15651)
+++
root/cdk/trunk/plugins/generator/src/test/java/org/richfaces/cdk/xmlconfig/FragmentParserTest.java 2009-09-21
21:21:50 UTC (rev 15652)
@@ -63,6 +63,15 @@
}
@Test
+ public void nestedXincludeTest() throws Exception {
+ FragmentParser parser = new FragmentParser();
+ CdkContextBase contextBase = new CdkContextBase(this.getClass().getClassLoader());
+ parser.init(contextBase);
+ Collection<Property> properties =
parser.parseProperties("urn:resource:org/richfaces/cdk/xmlconfig/parent2.xml");
+ assertEquals(2,properties.size());
+ }
+
+ @Test
public void propertyTest() throws Exception {
FragmentParser parser = new FragmentParser();
CdkContextBase contextBase = new CdkContextBase(this.getClass().getClassLoader());
Modified:
root/cdk/trunk/plugins/generator/src/test/resources/org/richfaces/cdk/xmlconfig/include.xml
===================================================================
---
root/cdk/trunk/plugins/generator/src/test/resources/org/richfaces/cdk/xmlconfig/include.xml 2009-09-21
20:33:51 UTC (rev 15651)
+++
root/cdk/trunk/plugins/generator/src/test/resources/org/richfaces/cdk/xmlconfig/include.xml 2009-09-21
21:21:50 UTC (rev 15652)
@@ -1,43 +1,45 @@
- <property
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:cdk="http://richfaces.org/cdk/extensions">
- <description><![CDATA[test property]]></description>
- <display-name>test event property</display-name>
- <icon>ontest.png</icon>
- <property-name>ontest</property-name>
- <property-class>boolean</property-class>
- <default-value>1</default-value>
- <suggested-value>1</suggested-value>
- <property-extension>
- <cdk:literal>true</cdk:literal>
- <cdk:generate>true</cdk:generate>
- <cdk:hidden>true</cdk:hidden>
- <cdk:required>true</cdk:required>
- <cdk:pass-through>true</cdk:pass-through>
- <cdk:event-name>test</cdk:event-name>
- <cdk:event-name default="true">action</cdk:event-name>
- </property-extension>
- </property >
- <property
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:cdk="http://richfaces.org/cdk/extensions">
- <description><![CDATA[test2 property]]></description>
- <display-name>test2 event property</display-name>
- <icon>ontest2.png</icon>
- <property-name>ontest2</property-name>
- <property-class>int</property-class>
- <default-value>3</default-value>
- <suggested-value>15</suggested-value>
- <property-extension>
- <cdk:literal>true</cdk:literal>
- <cdk:generate>true</cdk:generate>
- <cdk:hidden>true</cdk:hidden>
- <cdk:required>true</cdk:required>
- <cdk:pass-through>true</cdk:pass-through>
- <cdk:event-name>ontest2</cdk:event-name>
- <cdk:event-name default="true">action</cdk:event-name>
- <cdk:signature>
- <cdk:param>boolean</cdk:param>
- <cdk:param>java.lang.String</cdk:param>
- </cdk:signature>
- <cdk:alias>foo</cdk:alias>
- <cdk:alias>bar</cdk:alias>
- </property-extension>
- </property>
-
+<cdk:properties
xmlns:xi="http://www.w3.org/2001/XInclude"
+
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:cdk="http://richfaces.org/cdk/extensions">
+ <property>
+ <description><![CDATA[test property]]></description>
+ <display-name>test event property</display-name>
+ <icon>ontest.png</icon>
+ <property-name>ontest</property-name>
+ <property-class>boolean</property-class>
+ <default-value>1</default-value>
+ <suggested-value>1</suggested-value>
+ <property-extension>
+ <cdk:literal>true</cdk:literal>
+ <cdk:generate>true</cdk:generate>
+ <cdk:hidden>true</cdk:hidden>
+ <cdk:required>true</cdk:required>
+ <cdk:pass-through>true</cdk:pass-through>
+ <cdk:event-name>test</cdk:event-name>
+ <cdk:event-name default="true">action</cdk:event-name>
+ </property-extension>
+ </property>
+ <property>
+ <description><![CDATA[test2 property]]></description>
+ <display-name>test2 event property</display-name>
+ <icon>ontest2.png</icon>
+ <property-name>ontest2</property-name>
+ <property-class>int</property-class>
+ <default-value>3</default-value>
+ <suggested-value>15</suggested-value>
+ <property-extension>
+ <cdk:literal>true</cdk:literal>
+ <cdk:generate>true</cdk:generate>
+ <cdk:hidden>true</cdk:hidden>
+ <cdk:required>true</cdk:required>
+ <cdk:pass-through>true</cdk:pass-through>
+ <cdk:event-name>ontest2</cdk:event-name>
+ <cdk:event-name default="true">action</cdk:event-name>
+ <cdk:signature>
+ <cdk:param>boolean</cdk:param>
+ <cdk:param>java.lang.String</cdk:param>
+ </cdk:signature>
+ <cdk:alias>foo</cdk:alias>
+ <cdk:alias>bar</cdk:alias>
+ </property-extension>
+ </property>
+</cdk:properties>
Modified:
root/cdk/trunk/plugins/generator/src/test/resources/org/richfaces/cdk/xmlconfig/parent.xml
===================================================================
---
root/cdk/trunk/plugins/generator/src/test/resources/org/richfaces/cdk/xmlconfig/parent.xml 2009-09-21
20:33:51 UTC (rev 15651)
+++
root/cdk/trunk/plugins/generator/src/test/resources/org/richfaces/cdk/xmlconfig/parent.xml 2009-09-21
21:21:50 UTC (rev 15652)
@@ -1,13 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<cdk:properties
xmlns:xi="http://www.w3.org/2001/XInclude"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:cdk="http://richfaces.org/cdk/extensions">
- <!--
<xi:include
href="urn:resource:org/richfaces/cdk/xmlconfig/include.xml"
-
xpointer="xmlns(jsf=http://java.sun.com/xml/ns/javaee)xpointer(//jsf...
- -->
+
xpointer="xmlns(cdk=http://richfaces.org/cdk/extensions)xpointer(/cd...
- <xi:include
- href="urn:resource:org/richfaces/cdk/xmlconfig/include.xml"/>
-
</cdk:properties>
Added:
root/cdk/trunk/plugins/generator/src/test/resources/org/richfaces/cdk/xmlconfig/parent2.xml
===================================================================
---
root/cdk/trunk/plugins/generator/src/test/resources/org/richfaces/cdk/xmlconfig/parent2.xml
(rev 0)
+++
root/cdk/trunk/plugins/generator/src/test/resources/org/richfaces/cdk/xmlconfig/parent2.xml 2009-09-21
21:21:50 UTC (rev 15652)
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<cdk:properties
xmlns:xi="http://www.w3.org/2001/XInclude"
+
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:cdk="http://richfaces.org/cdk/extensions">
+ <xi:include
+ href="urn:resource:org/richfaces/cdk/xmlconfig/parent.xml"
+
xpointer="xmlns(cdk=http://richfaces.org/cdk/extensions)xpointer(/cd...
+
+</cdk:properties>
Property changes on:
root/cdk/trunk/plugins/generator/src/test/resources/org/richfaces/cdk/xmlconfig/parent2.xml
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Modified:
root/cdk/trunk/plugins/xinclude/src/main/java/org/apache/cocoon/pipeline/util/dom/DOMUtils.java
===================================================================
---
root/cdk/trunk/plugins/xinclude/src/main/java/org/apache/cocoon/pipeline/util/dom/DOMUtils.java 2009-09-21
20:33:51 UTC (rev 15651)
+++
root/cdk/trunk/plugins/xinclude/src/main/java/org/apache/cocoon/pipeline/util/dom/DOMUtils.java 2009-09-21
21:21:50 UTC (rev 15652)
@@ -36,6 +36,11 @@
public final class DOMUtils {
private final static DocumentBuilderFactory DBF =
DocumentBuilderFactory.newInstance();
+
+ static {
+ DBF.setNamespaceAware(true);
+ DBF.setXIncludeAware(false);
+ }
private DOMUtils() {
// instances are not allowed
@@ -44,6 +49,7 @@
public static Document toDOM(InputSource source, EntityResolver resolver) throws
SAXException, IOException {
DocumentBuilder documentBuilder;
try {
+
// TODO - configuration
documentBuilder = DBF.newDocumentBuilder();
documentBuilder.setEntityResolver(resolver);