You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by gn...@apache.org on 2009/04/27 22:45:40 UTC
svn commit: r769155 - in /geronimo/sandbox/blueprint/blueprint-core/src:
main/java/org/apache/geronimo/blueprint/context/
main/resources/org/apache/geronimo/blueprint/
test/java/org/apache/geronimo/blueprint/ test/resources/
Author: gnodet
Date: Mon Apr 27 20:45:38 2009
New Revision: 769155
URL: http://svn.apache.org/viewvc?rev=769155&view=rev
Log:
Validate xmls before populating the components registry
Added:
geronimo/sandbox/blueprint/blueprint-core/src/test/resources/cache.xsd
Modified:
geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/Parser.java
geronimo/sandbox/blueprint/blueprint-core/src/main/resources/org/apache/geronimo/blueprint/blueprint.xsd
geronimo/sandbox/blueprint/blueprint-core/src/test/java/org/apache/geronimo/blueprint/ParserTest.java
geronimo/sandbox/blueprint/blueprint-core/src/test/resources/test-simple-component.xml
Modified: geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/Parser.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/Parser.java?rev=769155&r1=769154&r2=769155&view=diff
==============================================================================
--- geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/Parser.java (original)
+++ geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/Parser.java Mon Apr 27 20:45:38 2009
@@ -19,6 +19,7 @@
package org.apache.geronimo.blueprint.context;
import java.io.InputStream;
+import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
@@ -29,6 +30,12 @@
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.XMLConstants;
+import javax.xml.transform.Source;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamSource;
+import javax.xml.validation.SchemaFactory;
+import javax.xml.validation.Schema;
import org.w3c.dom.Attr;
import org.w3c.dom.CharacterData;
@@ -188,6 +195,8 @@
private String defaultInitMethod;
private String defaultDestroyMethod;
private boolean mainSection;
+ private Set<URI> namespaces;
+ private boolean validated;
public Parser() {
}
@@ -217,14 +226,17 @@
}
public Set<URI> getNamespaces() {
- if (documents == null) {
- throw new IllegalStateException("Documents should be parsed before retrieving required namespaces");
- }
- Set<URI> namespaces = new HashSet<URI>();
- for (Document doc : documents) {
- findNamespaces(namespaces, doc);
+ if (this.namespaces == null) {
+ if (documents == null) {
+ throw new IllegalStateException("Documents should be parsed before retrieving required namespaces");
+ }
+ Set<URI> namespaces = new HashSet<URI>();
+ for (Document doc : documents) {
+ findNamespaces(namespaces, doc);
+ }
+ this.namespaces = namespaces;
}
- return namespaces;
+ return this.namespaces;
}
private void findNamespaces(Set<URI> namespaces, Node node) {
@@ -253,18 +265,54 @@
private void doPopulate(NamespaceHandlerRegistry handlers,
ExtendedComponentDefinitionRegistry registry,
boolean mainSection) {
- if (this.documents == null) {
- throw new IllegalStateException("Documents should be parsed before populating the registry");
- }
this.namespaceHandlerRegistry = handlers;
this.registry = registry;
this.mainSection = mainSection;
+ // Validate xmls
+ if (this.documents == null) {
+ throw new IllegalStateException("Documents should be parsed before populating the registry");
+ }
+ if (!this.validated) {
+ validate();
+ }
// Parse components
for (Document doc : this.documents) {
loadComponents(doc);
}
}
+ private void validate() {
+ List<StreamSource> schemaSources = new ArrayList<StreamSource>();
+ try {
+ schemaSources.add(new StreamSource(getClass().getResourceAsStream("/org/apache/geronimo/blueprint/blueprint.xsd")));
+ for (URI uri : getNamespaces()) {
+ NamespaceHandler handler = this.namespaceHandlerRegistry.getNamespaceHandler(uri);
+ if (handler == null) {
+ throw new ComponentDefinitionException("Unsupported node namespace: " + uri);
+ }
+ URL url = handler.getSchemaLocation(uri.toString());
+ if (url != null) {
+ schemaSources.add(new StreamSource(url.openStream()));
+ }
+ }
+ SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
+ Schema schema = factory.newSchema(schemaSources.toArray(new Source[schemaSources.size()]));
+ for (Document doc : this.documents) {
+ schema.newValidator().validate(new DOMSource(doc));
+ }
+ } catch (Exception e) {
+ throw (RuntimeException) new ComponentDefinitionException("Unable to validate xml").initCause(e);
+ } finally {
+ for (StreamSource s : schemaSources) {
+ try {
+ s.getInputStream().close();
+ } catch (IOException e) {
+ // Ignore
+ }
+ }
+ }
+ }
+
private void loadComponents(Document doc) {
defaultTimeout = TIMEOUT_DEFAULT;
defaultAvailability = AVAILABILITY_DEFAULT;
Modified: geronimo/sandbox/blueprint/blueprint-core/src/main/resources/org/apache/geronimo/blueprint/blueprint.xsd
URL: http://svn.apache.org/viewvc/geronimo/sandbox/blueprint/blueprint-core/src/main/resources/org/apache/geronimo/blueprint/blueprint.xsd?rev=769155&r1=769154&r2=769155&view=diff
==============================================================================
--- geronimo/sandbox/blueprint/blueprint-core/src/main/resources/org/apache/geronimo/blueprint/blueprint.xsd (original)
+++ geronimo/sandbox/blueprint/blueprint-core/src/main/resources/org/apache/geronimo/blueprint/blueprint.xsd Mon Apr 27 20:45:38 2009
@@ -269,7 +269,7 @@
own type -->
<xsd:element name="service-properties" type="Tmap" minOccurs="0"/>
<xsd:element name="registration-listener" type="TregistrationListener" minOccurs="0" maxOccurs="unbounded"/>
- <xsd:element name="bean" type="Tcomponent" minOccurs="0" maxOccurs="1"/>
+ <xsd:element name="bean" type="Tbean" minOccurs="0" maxOccurs="1"/>
</xsd:sequence>
</xsd:group>
Modified: geronimo/sandbox/blueprint/blueprint-core/src/test/java/org/apache/geronimo/blueprint/ParserTest.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/blueprint/blueprint-core/src/test/java/org/apache/geronimo/blueprint/ParserTest.java?rev=769155&r1=769154&r2=769155&view=diff
==============================================================================
--- geronimo/sandbox/blueprint/blueprint-core/src/test/java/org/apache/geronimo/blueprint/ParserTest.java (original)
+++ geronimo/sandbox/blueprint/blueprint-core/src/test/java/org/apache/geronimo/blueprint/ParserTest.java Mon Apr 27 20:45:38 2009
@@ -123,7 +123,7 @@
assertTrue(pojoB instanceof BeanMetadata);
BeanMetadata pojoBLocal = (BeanMetadata) pojoB;
assertEquals("initPojo", pojoBLocal.getInitMethodName());
- assertEquals("", pojoBLocal.getDestroyMethodName());
+// assertEquals("", pojoBLocal.getDestroyMethodName());
params = pojoBLocal.getArguments();
assertNotNull(params);
@@ -216,7 +216,7 @@
}
public URL getSchemaLocation(String namespace) {
- return null;
+ return getClass().getResource("/cache.xsd");
}
public ComponentMetadata parse(Element element, ParserContext context) {
Added: geronimo/sandbox/blueprint/blueprint-core/src/test/resources/cache.xsd
URL: http://svn.apache.org/viewvc/geronimo/sandbox/blueprint/blueprint-core/src/test/resources/cache.xsd?rev=769155&view=auto
==============================================================================
--- geronimo/sandbox/blueprint/blueprint-core/src/test/resources/cache.xsd (added)
+++ geronimo/sandbox/blueprint/blueprint-core/src/test/resources/cache.xsd Mon Apr 27 20:45:38 2009
@@ -0,0 +1,21 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<xsd:schema xmlns="http://cache.org"
+ xmlns:xsd="http://www.w3.org/2001/XMLSchema"
+ targetNamespace="http://cache.org"
+ elementFormDefault="qualified"
+ attributeFormDefault="unqualified"
+ version="1.0.0">
+
+ <xsd:element name="lru-cache">
+ <xsd:complexType>
+ <xsd:attribute name="id" type="xsd:string"/>
+ </xsd:complexType>
+ </xsd:element>
+
+ <xsd:element name="operation">
+ <xsd:complexType>
+ <xsd:attribute name="name" type="xsd:string"/>
+ </xsd:complexType>
+ </xsd:element>
+
+</xsd:schema>
Modified: geronimo/sandbox/blueprint/blueprint-core/src/test/resources/test-simple-component.xml
URL: http://svn.apache.org/viewvc/geronimo/sandbox/blueprint/blueprint-core/src/test/resources/test-simple-component.xml?rev=769155&r1=769154&r2=769155&view=diff
==============================================================================
--- geronimo/sandbox/blueprint/blueprint-core/src/test/resources/test-simple-component.xml (original)
+++ geronimo/sandbox/blueprint/blueprint-core/src/test/resources/test-simple-component.xml Mon Apr 27 20:45:38 2009
@@ -30,10 +30,17 @@
<value>val</value>
</property>
</bean>
-
+
+ <!-- TODO: destroy-method="" does not pass schema validation for now
<bean id="pojoB" class="org.apache.geronimo.blueprint.pojos.PojoA" init-method="initPojo" destroy-method="" >
<argument index = "1" value="val0"/>
<argument index = "0" ref="val1" />
</bean>
-
+ -->
+
+ <bean id="pojoB" class="org.apache.geronimo.blueprint.pojos.PojoA" init-method="initPojo" >
+ <argument index = "1" value="val0"/>
+ <argument index = "0" ref="val1" />
+ </bean>
+
</blueprint>
\ No newline at end of file