You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@beehive.apache.org by ek...@apache.org on 2005/09/20 20:01:52 UTC
svn commit: r290497 - in /beehive/branches/v1/final/netui:
src/pageflow/org/apache/beehive/netui/pageflow/internal/
src/util/org/apache/beehive/netui/util/config/parser/
src/util/org/apache/beehive/netui/util/xml/validation/
test/src/junitTests/org/apa...
Author: ekoneil
Date: Tue Sep 20 11:01:44 2005
New Revision: 290497
URL: http://svn.apache.org/viewcvs?rev=290497&view=rev
Log:
Integrate SVN 290494 from trunk/ into branches/v1/final.
BB: self
Test: NetUI DRT pass
Removed:
beehive/branches/v1/final/netui/src/util/org/apache/beehive/netui/util/xml/validation/
Modified:
beehive/branches/v1/final/netui/src/pageflow/org/apache/beehive/netui/pageflow/internal/DefaultURLTemplatesFactory.java
beehive/branches/v1/final/netui/src/util/org/apache/beehive/netui/util/config/parser/NetUIConfigParser.java
beehive/branches/v1/final/netui/test/src/junitTests/org/apache/beehive/netui/test/util/config/ConfigBeanTest.java
beehive/branches/v1/final/netui/test/src/junitTests/org/apache/beehive/netui/test/util/config/SchemaValidationTest.java
beehive/branches/v1/final/netui/test/src/junitTests/org/apache/beehive/netui/test/util/config/TestConfigUtil.java
Modified: beehive/branches/v1/final/netui/src/pageflow/org/apache/beehive/netui/pageflow/internal/DefaultURLTemplatesFactory.java
URL: http://svn.apache.org/viewcvs/beehive/branches/v1/final/netui/src/pageflow/org/apache/beehive/netui/pageflow/internal/DefaultURLTemplatesFactory.java?rev=290497&r1=290496&r2=290497&view=diff
==============================================================================
--- beehive/branches/v1/final/netui/src/pageflow/org/apache/beehive/netui/pageflow/internal/DefaultURLTemplatesFactory.java (original)
+++ beehive/branches/v1/final/netui/src/pageflow/org/apache/beehive/netui/pageflow/internal/DefaultURLTemplatesFactory.java Tue Sep 20 11:01:44 2005
@@ -23,9 +23,6 @@
import org.apache.beehive.netui.util.logging.Logger;
import org.apache.beehive.netui.util.xml.DomUtils;
import org.apache.beehive.netui.util.xml.XmlInputStreamResolver;
-import org.apache.beehive.netui.util.xml.validation.SchemaValidationException;
-import org.apache.beehive.netui.util.xml.validation.SchemaValidator;
-import org.apache.beehive.netui.util.xml.validation.SchemaValidatorFactory;
import java.io.InputStream;
import java.io.IOException;
@@ -39,6 +36,10 @@
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;
+import org.xml.sax.ErrorHandler;
+import org.xml.sax.SAXParseException;
+import org.xml.sax.EntityResolver;
+import org.xml.sax.InputSource;
/**
* Methods for configuring and retrieving the URLTemplate object.
@@ -137,70 +138,83 @@
try
{
xmlInputStream = servletContext.getResourceAsStream( _configFilePath );
- if ( xmlInputStream != null )
- {
- // Validate the XML against the schema
+ if ( xmlInputStream != null ) {
+ /* load the XSD input stream */
xsdInputStream = SCHEMA_RESOLVER.getInputStream();
- SchemaValidator schemaValidator = SchemaValidatorFactory.getInstance();
- schemaValidator.validate(SCHEMA_RESOLVER.getResourcePath(), xsdInputStream,
- _configFilePath, xmlInputStream);
- try
- {
- if ( xmlInputStream != null )
- xmlInputStream.close();
- }
- catch ( IOException io )
- {
- _log.error( "An exception occurred closing the input stream for \"" + _configFilePath + "\"", io );
- }
-
- // Parse, then load the templates and template ref groups
- xmlInputStream = servletContext.getResourceAsStream( _configFilePath );
-
- DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
- DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
- Document document = dBuilder.parse( xmlInputStream );
+
+ final String W3C_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema";
+ final String JAXP_SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
+ final String JAXP_SCHEMA_SOURCE = "http://java.sun.com/xml/jaxp/properties/schemaSource";
+
+ DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
+ dbf.setValidating(true);
+ dbf.setNamespaceAware(true);
+ dbf.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
+ dbf.setAttribute(JAXP_SCHEMA_SOURCE, xsdInputStream);
+
+ DocumentBuilder db = dbf.newDocumentBuilder();
+
+ /* add an ErrorHandler that just logs validation problems */
+ db.setErrorHandler(new ErrorHandler() {
+ public void warning(SAXParseException exception) {
+ _log.info("Validation warning validating config file \"" + _configFilePath + "\" against XML Schema \"" + SCHEMA_RESOLVER.getResourcePath());
+ }
+
+ public void error(SAXParseException exception) {
+ _log.error("Validation errors occurred parsing the config file \"" + _configFilePath + "\". Cause: " + exception, exception);
+ }
+
+ public void fatalError(SAXParseException exception) {
+ _log.error("Validation errors occurred parsing the config file \"" + _configFilePath + "\". Cause: " + exception, exception);
+ }
+ });
+
+ db.setEntityResolver(new EntityResolver() {
+ public InputSource resolveEntity(String publicId, String systemId) {
+ if(systemId.endsWith("/url-template-config.xsd")) {
+ InputStream inputStream = DefaultURLTemplatesFactory.class.getClassLoader().getResourceAsStream(CONFIG_SCHEMA);
+ return new InputSource(inputStream);
+ }
+ else return null;
+ }
+ });
+
+ Document document = db.parse(xmlInputStream);
Element root = document.getDocumentElement();
loadTemplates( root );
loadTemplateRefGroups( root );
- }
- else
- {
- if ( _log.isInfoEnabled() )
- {
- _log.info( "Running without URL template descriptor, " + _configFilePath );
- }
- }
- }
- catch ( SchemaValidationException sve )
- {
- _log.error("Validation errors occurred parsing the config file \"" + _configFilePath + "\". Cause: " + sve, sve);
- }
- catch ( ParserConfigurationException pce )
- {
- _log.error( "Problem loading URL template descriptor file " + _configFilePath, pce );
- }
- catch ( SAXException se )
- {
- _log.error( "Problem parsing URL template descriptor in " + _configFilePath, se );
- }
- catch ( IOException ioe )
- {
- _log.error( "Problem reading URL template descriptor file " + _configFilePath, ioe );
- }
- finally
- {
- // Close the streams
- try { if ( xmlInputStream != null ) xmlInputStream.close(); } catch ( Exception ignore ) {}
- try { if ( xsdInputStream != null ) xsdInputStream.close(); } catch( IOException ignore ) {}
- }
- }
+ }
+ else
+ {
+ if ( _log.isInfoEnabled() )
+ _log.info( "Running without URL template descriptor, " + _configFilePath );
+ }
+ }
+ catch ( ParserConfigurationException pce )
+ {
+ _log.error( "Problem loading URL template descriptor file " + _configFilePath, pce );
+ }
+ catch ( SAXException se )
+ {
+ _log.error( "Problem parsing URL template descriptor in " + _configFilePath, se );
+ }
+ catch ( IOException ioe )
+ {
+ _log.error( "Problem reading URL template descriptor file " + _configFilePath, ioe );
+ }
+ finally
+ {
+ // Close the streams
+ try { if ( xmlInputStream != null ) xmlInputStream.close(); } catch ( Exception ignore ) {}
+ try { if ( xsdInputStream != null ) xsdInputStream.close(); } catch( IOException ignore ) {}
+ }
+}
- /**
- * Loads the templates from a URL template config document.
- *
- * @param parent
- */
+/**
+* Loads the templates from a URL template config document.
+*
+* @param parent
+*/
private void loadTemplates( Element parent )
{
// Load templates
Modified: beehive/branches/v1/final/netui/src/util/org/apache/beehive/netui/util/config/parser/NetUIConfigParser.java
URL: http://svn.apache.org/viewcvs/beehive/branches/v1/final/netui/src/util/org/apache/beehive/netui/util/config/parser/NetUIConfigParser.java?rev=290497&r1=290496&r2=290497&view=diff
==============================================================================
--- beehive/branches/v1/final/netui/src/util/org/apache/beehive/netui/util/config/parser/NetUIConfigParser.java (original)
+++ beehive/branches/v1/final/netui/src/util/org/apache/beehive/netui/util/config/parser/NetUIConfigParser.java Tue Sep 20 11:01:44 2005
@@ -53,9 +53,6 @@
import org.apache.beehive.netui.util.config.bean.BindingContextConfig;
import org.apache.beehive.netui.util.config.bean.HandlerConfig;
import org.apache.beehive.netui.util.config.bean.CustomPropertyConfig;
-import org.apache.beehive.netui.util.xml.validation.SchemaValidatorFactory;
-import org.apache.beehive.netui.util.xml.validation.SchemaValidator;
-import org.apache.beehive.netui.util.xml.validation.SchemaValidationException;
import org.apache.beehive.netui.util.xml.DomUtils;
import org.apache.beehive.netui.util.xml.XmlInputStreamResolver;
import org.apache.beehive.netui.util.logging.Logger;
@@ -64,6 +61,10 @@
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;
+import org.xml.sax.InputSource;
+import org.xml.sax.EntityResolver;
+import org.xml.sax.ErrorHandler;
+import org.xml.sax.SAXParseException;
public final class NetUIConfigParser {
@@ -96,7 +97,6 @@
NetUIConfig configBean = null;
InputStream xmlInputStream = null;
- InputStream xsdInputStream = null;
XmlInputStreamResolver theXmlResolver = xmlResolver;
try {
/* use the default XmlInputStream */
@@ -113,42 +113,20 @@
if(LOGGER.isInfoEnabled())
LOGGER.info("Loading the default NetUI config file. The runtime will be configured " +
"with a set of minimum parameters.");
+
+ /* todo: should this throw an exception? */
+ if(xmlInputStream == null)
+ throw new ConfigInitializationException("The NetUI runtime could not find the default config file. " +
+ "The webapp may not function properly.");
}
if(LOGGER.isInfoEnabled())
LOGGER.info("NetUIConfigParser -- load config: " + theXmlResolver.getResourcePath());
- /* todo: should this throw an exception? */
- if(xmlInputStream == null)
- throw new ConfigInitializationException("The NetUI runtime could not find the default config file. " +
- "The webapp may not function properly.");
-
- xsdInputStream = SCHEMA_RESOLVER.getInputStream();
- try {
- SchemaValidator schemaValidator = SchemaValidatorFactory.getInstance();
- schemaValidator.validate(SCHEMA_RESOLVER.getResourcePath(), xsdInputStream,
- theXmlResolver.getResourcePath(), xmlInputStream);
- }
- catch(SchemaValidationException e) {
- throw new ConfigInitializationException("Validation errors occurred parsing the config file \"" +
- theXmlResolver.getResourcePath() + "\". Cause: " + e, e);
- }
-
- try {
- if(xmlInputStream != null)
- xmlInputStream.close();
- }
- catch(IOException io) {
- throw new ConfigInitializationException("An exception occurred closing the input stream for \"" +
- theXmlResolver.getResourcePath() + "\"");
- }
-
- xmlInputStream = theXmlResolver.getInputStream();
configBean = parse(theXmlResolver.getResourcePath(), xmlInputStream);
}
finally {
try {if(xmlInputStream != null) xmlInputStream.close();} catch(IOException ignore) {}
- try {if(xsdInputStream != null) xsdInputStream.close();} catch(IOException ignore) {}
}
return configBean;
@@ -158,10 +136,49 @@
assert is != null;
NetUIConfig netuiConfig = null;
+ InputStream xsdInputStream = null;
try {
/* parse the config document */
+ xsdInputStream = SCHEMA_RESOLVER.getInputStream();
+ final String W3C_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema";
+ final String JAXP_SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
+ final String JAXP_SCHEMA_SOURCE = "http://java.sun.com/xml/jaxp/properties/schemaSource";
+
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
+ dbf.setValidating(true);
+ dbf.setNamespaceAware(true);
+ dbf.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
+ dbf.setAttribute(JAXP_SCHEMA_SOURCE, xsdInputStream);
+
DocumentBuilder db = dbf.newDocumentBuilder();
+ db.setErrorHandler(new ErrorHandler() {
+ public void warning(SAXParseException exception) {
+ if(LOGGER.isInfoEnabled())
+ LOGGER.info("Validation warning validating config file \"" + resourcePath +
+ "\" against XML Schema \"" + SCHEMA_RESOLVER.getResourcePath());
+ }
+
+ public void error(SAXParseException exception) {
+ throw new ConfigInitializationException("Validation errors occurred parsing the config file \"" +
+ resourcePath + "\". Cause: " + exception, exception);
+ }
+
+ public void fatalError(SAXParseException exception) {
+ throw new ConfigInitializationException("Validation errors occurred parsing the config file \"" +
+ resourcePath + "\". Cause: " + exception, exception);
+ }
+ });
+
+ db.setEntityResolver(new EntityResolver() {
+ public InputSource resolveEntity(String publicId, String systemId) {
+ if(systemId.endsWith("/beehive-netui-config.xsd")) {
+ InputStream inputStream = NetUIConfigParser.class.getClassLoader().getResourceAsStream(CONFIG_SCHEMA);
+ return new InputSource(inputStream);
+ }
+ else return null;
+ }
+ });
+
Document document = db.parse(is);
PageFlowActionInterceptorsConfig pfActionInterceptorsConfig = parsePfActionInterceptorsConfig(document);
@@ -201,6 +218,9 @@
}
catch(SAXException e) {
throw new ConfigInitializationException("Error occurred parsing the config file \"" + resourcePath + "\"", e);
+ }
+ finally {
+ try{if(xsdInputStream != null) xsdInputStream.close();} catch(IOException e) {}
}
return netuiConfig;
}
Modified: beehive/branches/v1/final/netui/test/src/junitTests/org/apache/beehive/netui/test/util/config/ConfigBeanTest.java
URL: http://svn.apache.org/viewcvs/beehive/branches/v1/final/netui/test/src/junitTests/org/apache/beehive/netui/test/util/config/ConfigBeanTest.java?rev=290497&r1=290496&r2=290497&view=diff
==============================================================================
--- beehive/branches/v1/final/netui/test/src/junitTests/org/apache/beehive/netui/test/util/config/ConfigBeanTest.java (original)
+++ beehive/branches/v1/final/netui/test/src/junitTests/org/apache/beehive/netui/test/util/config/ConfigBeanTest.java Tue Sep 20 11:01:44 2005
@@ -17,8 +17,6 @@
*/
package org.apache.beehive.netui.test.util.config;
-import java.io.InputStream;
-
import junit.framework.TestCase;
import junit.framework.Test;
import junit.framework.TestSuite;
Modified: beehive/branches/v1/final/netui/test/src/junitTests/org/apache/beehive/netui/test/util/config/SchemaValidationTest.java
URL: http://svn.apache.org/viewcvs/beehive/branches/v1/final/netui/test/src/junitTests/org/apache/beehive/netui/test/util/config/SchemaValidationTest.java?rev=290497&r1=290496&r2=290497&view=diff
==============================================================================
--- beehive/branches/v1/final/netui/test/src/junitTests/org/apache/beehive/netui/test/util/config/SchemaValidationTest.java (original)
+++ beehive/branches/v1/final/netui/test/src/junitTests/org/apache/beehive/netui/test/util/config/SchemaValidationTest.java Tue Sep 20 11:01:44 2005
@@ -17,23 +17,29 @@
*/
package org.apache.beehive.netui.test.util.config;
+import java.io.InputStream;
+import java.io.IOException;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.ParserConfigurationException;
+
import junit.framework.TestCase;
-import org.apache.beehive.netui.util.xml.validation.SchemaValidationException;
-import org.apache.beehive.netui.util.xml.validation.SchemaValidator;
-import org.apache.beehive.netui.util.xml.validation.SchemaValidatorFactory;
import org.apache.beehive.netui.util.xml.XmlInputStreamResolver;
-
-import java.io.InputStream;
-import java.io.IOException;
+import org.apache.beehive.netui.util.config.ConfigInitializationException;
+import org.xml.sax.ErrorHandler;
+import org.xml.sax.SAXParseException;
+import org.xml.sax.EntityResolver;
+import org.xml.sax.InputSource;
+import org.w3c.dom.Document;
/**
*/
public class SchemaValidationTest
extends TestCase {
- public void testValidationSuccess() {
- SchemaValidator schemaValidator = SchemaValidatorFactory.getInstance();
+ public void testValidationSuccess()
+ throws Exception {
XmlInputStreamResolver xsdInputStreamResolver =
new TestXmlInputStreamResolver("org/apache/beehive/netui/util/config/schema/beehive-netui-config.xsd");
@@ -44,8 +50,7 @@
InputStream xsdIs = xsdInputStreamResolver.getInputStream();
InputStream xmlIs = xmlInputStreamResolver.getInputStream();
try {
- schemaValidator.validate(xsdInputStreamResolver.getResourcePath(), xsdIs,
- xmlInputStreamResolver.getResourcePath(), xmlIs);
+ validate(xsdInputStreamResolver, xmlInputStreamResolver, "/beehive-netui-config.xsd");
}
catch(SchemaValidationException e) {
assertTrue("Received an unexpected schema validation error", false);
@@ -56,9 +61,8 @@
}
}
- public void testValidationFailure() {
-
- SchemaValidator schemaValidator = SchemaValidatorFactory.getInstance();
+ public void testValidationFailure()
+ throws Exception {
XmlInputStreamResolver xsdInputStreamResolver =
new TestXmlInputStreamResolver("org/apache/beehive/netui/util/config/schema/beehive-netui-config.xsd");
@@ -69,8 +73,7 @@
InputStream xsdIs = xsdInputStreamResolver.getInputStream();
InputStream xmlIs = xmlInputStreamResolver.getInputStream();
try {
- schemaValidator.validate(xsdInputStreamResolver.getResourcePath(), xsdIs,
- xmlInputStreamResolver.getResourcePath(), xmlIs);
+ validate(xsdInputStreamResolver, xmlInputStreamResolver, "/beehive-netui-config.xsd");
}
catch(SchemaValidationException e) {
return;
@@ -82,4 +85,66 @@
assertTrue("Expected a validation failure but did not receive one", false);
}
+
+ private void validate(final XmlInputStreamResolver xsdResolver, final XmlInputStreamResolver xmlResolver, String systemId)
+ throws Exception {
+ InputStream xmlIs = null;
+ InputStream xsdIs = null;
+ try {
+ final String W3C_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema";
+ final String JAXP_SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
+ final String JAXP_SCHEMA_SOURCE = "http://java.sun.com/xml/jaxp/properties/schemaSource";
+
+ xmlIs = xmlResolver.getInputStream();
+ xsdIs = xsdResolver.getInputStream();
+
+ DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
+ dbf.setValidating(true);
+ dbf.setNamespaceAware(true);
+ dbf.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
+ dbf.setAttribute(JAXP_SCHEMA_SOURCE, xsdIs);
+
+ DocumentBuilder db = dbf.newDocumentBuilder();
+ db.setErrorHandler(new ErrorHandler() {
+ public void warning(SAXParseException exception) {
+ System.out.println("Validation warning validating config file \"" + xmlResolver.getResourcePath() +
+ "\" against XML Schema \"" + xsdResolver.getResourcePath());
+ }
+
+ public void error(SAXParseException exception) {
+ throw new SchemaValidationException("Validation errors occurred parsing the config file \"" +
+ xmlResolver.getResourcePath() + "\". Cause: " + exception, exception);
+ }
+
+ public void fatalError(SAXParseException exception) {
+ throw new SchemaValidationException("Validation errors occurred parsing the config file \"" +
+ xmlResolver.getResourcePath() + "\". Cause: " + exception, exception);
+ }
+ });
+
+ db.setEntityResolver(new EntityResolver() {
+ public InputSource resolveEntity(String publicId, String systemId) {
+ if(systemId.endsWith(systemId)) {
+ return new InputSource(xsdResolver.getInputStream());
+ }
+ else return null;
+ }
+ });
+
+ Document document = db.parse(xmlIs);
+ }
+ catch(ParserConfigurationException ignore) {}
+ finally {
+ try {if(xsdIs != null) xsdIs.close();}catch(IOException io) {}
+ try {if(xmlIs != null) xmlIs.close();}catch(IOException io) {}
+ }
+ }
}
+
+class SchemaValidationException
+ extends RuntimeException {
+
+ SchemaValidationException(String message, Throwable cause) {
+ super(message, cause);
+ }
+}
\ No newline at end of file
Modified: beehive/branches/v1/final/netui/test/src/junitTests/org/apache/beehive/netui/test/util/config/TestConfigUtil.java
URL: http://svn.apache.org/viewcvs/beehive/branches/v1/final/netui/test/src/junitTests/org/apache/beehive/netui/test/util/config/TestConfigUtil.java?rev=290497&r1=290496&r2=290497&view=diff
==============================================================================
--- beehive/branches/v1/final/netui/test/src/junitTests/org/apache/beehive/netui/test/util/config/TestConfigUtil.java (original)
+++ beehive/branches/v1/final/netui/test/src/junitTests/org/apache/beehive/netui/test/util/config/TestConfigUtil.java Tue Sep 20 11:01:44 2005
@@ -18,7 +18,6 @@
package org.apache.beehive.netui.test.util.config;
import java.io.InputStream;
-import java.io.IOException;
import org.apache.beehive.netui.util.config.ConfigUtil;
import org.apache.beehive.netui.util.config.ConfigInitializationException;