Skip to Content.
Sympa Menu

notify-dpml - svn commit: r1338 - in development/main/metro: composition/impl/src/main/net/dpml/composition/util composition/test/src/test/net/dpml/composition/util testing/testschema/src/main/net/dpml/test/testschema

notify-dpml AT lists.ibiblio.org

Subject: DPML Notify

List archive

Chronological Thread  
  • From: pneubauer AT netcompartner.com
  • To: notify-dpml AT lists.ibiblio.org
  • Subject: svn commit: r1338 - in development/main/metro: composition/impl/src/main/net/dpml/composition/util composition/test/src/test/net/dpml/composition/util testing/testschema/src/main/net/dpml/test/testschema
  • Date: Sat, 01 Jan 2005 16:12:44 +0100

Author: pneubauer
Date: Sat Jan 1 16:12:44 2005
New Revision: 1338

Added:

development/main/metro/testing/testschema/src/main/net/dpml/test/testschema/ConfigurableB.xschema
Modified:

development/main/metro/composition/impl/src/main/net/dpml/composition/util/ConfigurationSerializer.java

development/main/metro/composition/impl/src/main/net/dpml/composition/util/ConfigurationValidator.java

development/main/metro/composition/impl/src/main/net/dpml/composition/util/XercesSchemaValidator.java

development/main/metro/composition/test/src/test/net/dpml/composition/util/SchemaValidationTestCase.java

development/main/metro/testing/testschema/src/main/net/dpml/test/testschema/ConfigurableB.java
Log:
first working component validation. uses temporary files, but we have to
decide how to locate schemas anyway


Modified:
development/main/metro/composition/impl/src/main/net/dpml/composition/util/ConfigurationSerializer.java
==============================================================================
---
development/main/metro/composition/impl/src/main/net/dpml/composition/util/ConfigurationSerializer.java
(original)
+++
development/main/metro/composition/impl/src/main/net/dpml/composition/util/ConfigurationSerializer.java
Sat Jan 1 16:12:44 2005
@@ -29,67 +29,58 @@
public class ConfigurationSerializer
{
/**
- * @param schemaUrl
- * the schema the serialized configuration xml file
should
- * validate against
* @param iConfig
- * the configuration to serialze
+ * the configuration to serialze
* @throws ConfigurationException
- * configuration could not be serialized
+ * configuration could not be serialized
+ * @return the serialized configuration
*/
- public String serialize( Configuration iConfig, String schemaUrl )
+ public String serialize( Configuration iConfig )
throws ConfigurationException
{
StringBuffer result = new StringBuffer();
DefaultConfiguration tConfig = new DefaultConfiguration( iConfig );
result.append( "<?xml version=\"1.0\" ?>" );
- handleConfiguration( tConfig, result, true, schemaUrl );
+ handleConfiguration( tConfig, result );
return result.toString();

}

/**
- * @param config
- * @param result
+ * @param iConfig the config to handle
+ * @param result the result
*/
- private void handleConfiguration( Configuration config,
- StringBuffer result, boolean writeNS, String schemaUrl )
+ private void handleConfiguration( Configuration iConfig,
+ StringBuffer result )
{
result.append( "<" );
- result.append( config.getName() );
- if( writeNS )
- {
- result
- .append( "
xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""; );
- result.append( " xsi:noNamespaceSchemaLocation=\"" + schemaUrl
- + "\"" );
- }
- handleAttributes( config, result );
+ result.append( iConfig.getName() );
+ handleAttributes( iConfig, result );
result.append( ">" );
- Configuration[] children = config.getChildren();
+ Configuration[] children = iConfig.getChildren();
for ( int i = 0; i < children.length; i++ )
{
Configuration configuration = children[i];
- handleConfiguration( configuration, result, false, null );
+ handleConfiguration( configuration, result );

}
try
{
- result.append( config.getValue() );
+ result.append( iConfig.getValue() );
}
catch ( ConfigurationException e )
{
- //no problem, there was no value
+ // no problem, there was no value
}
result.append( "</" );
- result.append( config.getName() );
+ result.append( iConfig.getName() );
result.append( ">" );

}

/**
- * @param config
- * @param result
+ * @param config the config to handle
+ * @param result the resu
*/
private void handleAttributes( Configuration config, StringBuffer result
)
{

Modified:
development/main/metro/composition/impl/src/main/net/dpml/composition/util/ConfigurationValidator.java
==============================================================================
---
development/main/metro/composition/impl/src/main/net/dpml/composition/util/ConfigurationValidator.java
(original)
+++
development/main/metro/composition/impl/src/main/net/dpml/composition/util/ConfigurationValidator.java
Sat Jan 1 16:12:44 2005
@@ -18,16 +18,21 @@

package net.dpml.composition.util;

+import java.io.IOException;
+
import net.dpml.composition.model.ModelException;
import net.dpml.configuration.Configuration;
-import net.dpml.meta.info.Type;
-
+/**
+ * interface for plugging in of different validation strategies
+ */
public interface ConfigurationValidator
{
- // TODO: Peter! Are both methods expected?
- void validateConfiguration( String iSchemaUrl, Configuration iConfig )
- throws ModelException;
-
- void validate( Configuration iConfig, Type iType )
- throws ModelException;
+ /**
+ * @param iConfig the comfiguration to validate
+ * @param iComponent the component class to validate for
+ * @throws ModelException the validation failed
+ * @throws IOException the resources could not be found
+ */
+ void validate( Configuration iConfig, Object iComponent )
+ throws ModelException, IOException;
}

Modified:
development/main/metro/composition/impl/src/main/net/dpml/composition/util/XercesSchemaValidator.java
==============================================================================
---
development/main/metro/composition/impl/src/main/net/dpml/composition/util/XercesSchemaValidator.java
(original)
+++
development/main/metro/composition/impl/src/main/net/dpml/composition/util/XercesSchemaValidator.java
Sat Jan 1 16:12:44 2005
@@ -17,18 +17,21 @@

package net.dpml.composition.util;

+import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
+import java.io.File;
+import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
+import java.io.InputStreamReader;

import javax.xml.parsers.ParserConfigurationException;
-import javax.xml.parsers.SAXParser;
-import javax.xml.parsers.SAXParserFactory;

import net.dpml.composition.model.ModelException;
import net.dpml.configuration.Configuration;
-import net.dpml.meta.info.Type;
+import net.dpml.configuration.ConfigurationException;

+import org.apache.xerces.parsers.SAXParser;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXNotRecognizedException;
@@ -36,31 +39,43 @@
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

+/**
+ * Validator that serializes the Config into an XML stream and validates
using schema and Xerces parser
+ */
public class XercesSchemaValidator implements ConfigurationValidator
{
private ConfigurationSerializer m_serializer;

- private SAXParser m_parser;
+ private SAXParser m_parser;

- private XMLReader m_xmlReader;
+ private XMLReader m_xmlReader;

- public static final String JAXP_SCHEMA_LANGUAGE =
"http://java.sun.com/xml/jaxp/properties/schemaLanguage";;
+ public static final String JAXP_SCHEMA_LANGUAGE =
"http://java.sun.com/xml/jaxp/properties/schemaLanguage";;

- public static final String W3C_XML_SCHEMA =
"http://www.w3.org/2001/XMLSchema";;
+ public static final String W3C_XML_SCHEMA =
"http://www.w3.org/2001/XMLSchema";;

public XercesSchemaValidator() throws ParserConfigurationException,
SAXException
- {
- m_serializer = new ConfigurationSerializer();
- m_parser = setupParser();
- }
+ {
+ m_serializer = new ConfigurationSerializer();
+ m_parser = setupParser();
+ }

- private void validateSchema( InputStream xmlStream ) throws
ModelException
+ private void validateConfiguration( String iSchemaPath,
+ Configuration iConfig ) throws ModelException
{
+
Validator handler = new Validator();
try
{
- m_parser.parse( new InputSource( xmlStream ), handler );
+ String xml = m_serializer.serialize( iConfig );
+ m_parser.setErrorHandler( handler );
+ m_parser
+ .setProperty(
+
"http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation";,
+ iSchemaPath );
+ m_parser.parse( new InputSource( new ByteArrayInputStream( xml
+ .getBytes() ) ) );
}
catch ( SAXException e )
{
@@ -74,60 +89,47 @@
+ handler.getSaxParseException().getMessage(), handler
.getSaxParseException() );
}
- if( handler.HasValidationError() )
- {
- throw new ModelException( "XML Document has Error:"
- + handler.getSaxParseException().getMessage(), handler
- .getSaxParseException() );
- }
-
- }
-
- public void validateConfiguration( String iSchemaUrl, Configuration
iConfig )
- throws ModelException
- {
- String tConfigXml = "";
- try
+ catch ( ConfigurationException e )
{
- tConfigXml = m_serializer.serialize( iConfig, iSchemaUrl );
+ throw new ModelException( "could not serialize Configuration", e
);
}
- catch ( Exception e )
+ if ( handler.hasValidationError() )
{
- throw new ModelException( "Could not serialize Configuration ",
e );
+ throw new ModelException( "Configuration has Errors:"
+ + handler.getSaxParseException().getMessage(), handler
+ .getSaxParseException() );
}
- ByteArrayInputStream tConfigStream = new ByteArrayInputStream(
- tConfigXml.getBytes() );
-
- validateSchema( tConfigStream );
}

private SAXParser setupParser() throws ParserConfigurationException,
SAXException
{

- SAXParserFactory factory = SAXParserFactory.newInstance();
- factory.setValidating( true );
- factory.setNamespaceAware( true );
- SAXParser saxParser = factory.newSAXParser();
- //special Xerces setup
- if (saxParser instanceof org.apache.xerces.jaxp.SAXParserImpl)
- {
- try
- {
- saxParser.setProperty( JAXP_SCHEMA_LANGUAGE,
W3C_XML_SCHEMA );
- }
- catch ( SAXNotRecognizedException x )
- {
- throw new ParserConfigurationException(
- "the parser "+ saxParser + " does not support the
property " + JAXP_SCHEMA_LANGUAGE + ": " + x );
- }
+ SAXParser saxParser = new SAXParser();
+ try
+ {
+ saxParser.setProperty( JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA );
+ saxParser.setFeature( "http://xml.org/sax/features/validation";,
+ true );
+ saxParser.setFeature(
+ "http://apache.org/xml/features/validation/schema";, true
);
+ saxParser
+ .setFeature(
+
"http://apache.org/xml/features/validation/schema-full-checking";,
+ true );
+ }
+ catch ( SAXNotRecognizedException x )
+ {
+ throw new ParserConfigurationException( "the parser " + saxParser
+ + " does not support the property " +
JAXP_SCHEMA_LANGUAGE
+ + ": " + x );
}
return saxParser;
}

private class Validator extends DefaultHandler
{
- private boolean m_validationError = false;
+ private boolean m_validationError = false;

private SAXParseException m_saxParseException = null;

@@ -142,7 +144,7 @@
return m_saxParseException;
}

- public boolean HasValidationError()
+ public boolean hasValidationError()
{
return m_validationError;
}
@@ -159,23 +161,54 @@
}
}

- /*
- * (non-Javadoc)
- *
+ /**
+ * Validates a CConfiguration by
+ * 1. serializing the configuration object into a XML configuration
+ * 2. try to locate the .xschema file, in the same manner as .xinfo
files (same location as Component)
+ * 3. write the contents of that resource into a temp File
+ * 4. validate the configuration via Schema validation, using Xerces
SAXParser and providing the schema file location
+ * 5. delete the temp file
+ * @throws IOException
* @see
net.dpml.composition.util.ConfigurationValidator#validate(net.dpml.configuration.Configuration,
- * net.dpml.meta.info.Type)
+ * net.dpml.meta.info.Type)
*/
- public void validate( Configuration iConfig, Type iType )
- throws ModelException
+ public void validate( Configuration iConfig, Object iComponent )
+ throws ModelException, IOException
{
- if( iType.getInfo().getConfigurationSchema() == null )
+ if ( iConfig == null )
{
- //no validation if no schema is provided
- return;
+ throw new ModelException( "null configuration!" );
}

- String schemaURL = iType.getInfo().getConfigurationSchema();
- validateConfiguration( schemaURL, iConfig );
-
+ Class t_class = iComponent.getClass();
+ ClassLoader cl = t_class.getClassLoader();
+ String iSchemaLocation = t_class.getName().replace( '.', '/' )
+ + ".xschema";
+ InputStream is = cl.getResourceAsStream( iSchemaLocation );
+ BufferedReader in = new BufferedReader( new InputStreamReader( is )
);
+ String inputLine;
+ StringBuffer buffer = new StringBuffer();
+ while ( ( inputLine = in.readLine() ) != null )
+ {
+ buffer.append( inputLine );
+ }
+ in.close();
+ File tempFile = new File( "temp.xshcema" );
+ FileOutputStream os = new FileOutputStream( tempFile );
+ os.write( buffer.toString().getBytes() );
+ os.flush();
+ os.close();
+ try
+ {
+ validateConfiguration( tempFile.getAbsolutePath(), iConfig );
+ }
+ catch (ModelException e)
+ {
+ throw e;
+ }
+ finally
+ {
+ tempFile.delete();
+ }
}
}
\ No newline at end of file

Modified:
development/main/metro/composition/test/src/test/net/dpml/composition/util/SchemaValidationTestCase.java
==============================================================================
---
development/main/metro/composition/test/src/test/net/dpml/composition/util/SchemaValidationTestCase.java
(original)
+++
development/main/metro/composition/test/src/test/net/dpml/composition/util/SchemaValidationTestCase.java
Sat Jan 1 16:12:44 2005
@@ -1,11 +1,32 @@
+/*
+ * Copyright 2004 Stephen J. McConnell.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ * implied.
+ *
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
package net.dpml.composition.util;

import java.io.ByteArrayInputStream;
import java.io.File;
-import java.io.FileInputStream;
-import java.io.InputStream;
import java.util.Hashtable;

+import junit.framework.TestCase;
+import net.dpml.composition.model.ModelException;
+import net.dpml.configuration.Configuration;
+import net.dpml.configuration.impl.DefaultConfigurationBuilder;
+import net.dpml.meta.info.builder.impl.TypeBuilder;
+import net.dpml.test.testschema.ConfigurableB;

import org.apache.xerces.parsers.SAXParser;
import org.xml.sax.InputSource;
@@ -14,20 +35,14 @@
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;

-import junit.framework.TestCase;
-import net.dpml.composition.model.ModelException;
-import net.dpml.configuration.Configuration;
-import net.dpml.meta.info.Type;
-import net.dpml.meta.info.builder.impl.TypeBuilder;
-import net.dpml.meta.info.impl.ConfigurationBuilder;
-import net.dpml.test.testschema.ConfigurableA;
-import net.dpml.test.testschema.ConfigurableB;
-
+/**
+ * testing Schema validation
+ */
public class SchemaValidationTestCase extends TestCase
{
static final String JAXP_SCHEMA_LANGUAGE =
"http://java.sun.com/xml/jaxp/properties/schemaLanguage";;

- static final String W3C_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema";;
+ static final String W3C_XML_SCHEMA =
"http://www.w3.org/2001/XMLSchema";;

/**
*/
@@ -37,27 +52,26 @@
private Hashtable m_errors;

/**
- *
+ *
*/
public TestHandler()
- {
- super();
- m_errors = new Hashtable();
- }
+ {
+ super();
+ m_errors = new Hashtable();
+ }

- /*
- * (non-Javadoc)
+ /**
*
* @see org.xml.sax.ErrorHandler#error(org.xml.sax.SAXParseException)
*/
public void error( SAXParseException e ) throws SAXException
{
- m_errors.put("lastError", e);
+ m_errors.put( "lastError", e );
e.printStackTrace();
}

/**
- * @return
+ * @return the errors
*/
public Hashtable getErrors()
{
@@ -67,53 +81,78 @@

private XercesSchemaValidator m_validator;

- private static final String XML_CONFIGURATION = "<?xml
version=\"1.0\"?>" +
- "<configuration attr1=\"foo\">" +
- "<child1/>" +
- "</configuration>";
+ private static final String XML_CONFIGURATION = "<?xml
version=\"1.0\"?>"
+ +
"<configuration attr1=\"foo\">"
+ + "<child1/>"
+ +
"</configuration>";
+
+ private Configuration m_config;

- protected void _setUp() throws Exception
+ /**
+ * @see junit.framework.TestCase#setUp()
+ */
+ protected void setUp() throws Exception
{

m_validator = new XercesSchemaValidator();
+ DefaultConfigurationBuilder builder = new
DefaultConfigurationBuilder();
+ m_config = builder.build( new ByteArrayInputStream( XML_CONFIGURATION
+ .getBytes() ) );

}

-
-
public void testParsingOfValidatingXMLOKWhenUsingFiles() throws Exception
{
SAXParser saxParser = new SAXParser();
- String projectDir = System.getProperty("project.dir");
- String schemaLocation = new File(projectDir +
"/ConfigurableB.xschema").getAbsolutePath();
- System.out.println("schema: " + schemaLocation);
- try
- {
- saxParser.setProperty( JAXP_SCHEMA_LANGUAGE,
W3C_XML_SCHEMA );
-
saxParser.setFeature("http://xml.org/sax/features/validation";, true);
-
saxParser.setFeature("http://apache.org/xml/features/validation/schema";,
- true);
-
saxParser.setFeature("http://apache.org/xml/features/validation/schema-full-checking",true);
- saxParser.setProperty(
-
"http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation";,
- schemaLocation);
- }
- catch ( SAXNotRecognizedException x )
- {
- fail( "could not set property " + x );
- }
+ String projectDir = System.getProperty( "project.dir" );
+ String schemaLocation = new File( projectDir +
"/ConfigurableB.xschema" )
+ .getAbsolutePath();
+ try
+ {
+ saxParser.setProperty( JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA );
+ saxParser.setFeature( "http://xml.org/sax/features/validation";,
+ true );
+ saxParser.setFeature(
+ "http://apache.org/xml/features/validation/schema";, true
);
+ saxParser
+ .setFeature(
+
"http://apache.org/xml/features/validation/schema-full-checking";,
+ true );
+ saxParser
+ .setProperty(
+
"http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation";,
+ schemaLocation );
+ }
+ catch ( SAXNotRecognizedException x )
+ {
+ fail( "could not set property " + x );
+ }
TestHandler testHandler = new TestHandler();
- saxParser.setErrorHandler(testHandler);
- saxParser.parse( new InputSource(new
ByteArrayInputStream(XML_CONFIGURATION.getBytes())));
- assertEquals(0, testHandler.m_errors.size());
+ saxParser.setErrorHandler( testHandler );
+ saxParser.parse( new InputSource( new ByteArrayInputStream(
+ XML_CONFIGURATION.getBytes() ) ) );
+ assertEquals( 0, testHandler.m_errors.size() );

-
+ }
+
+ public void testValidationOfComponentConfig() throws Exception
+ {
+ ConfigurableB confB = new ConfigurableB();
+ try
+ {
+ m_validator.validate( m_config, confB );
+ }
+ catch (ModelException e) {
+ fail("should bo ok");
+ e.printStackTrace();
+ }
+

}

public void testDummyAlwaysTrue()
{
- //System.out.println(System.getProperties());
+ // System.out.println(System.getProperties());
assertTrue( true );
}


Modified:
development/main/metro/testing/testschema/src/main/net/dpml/test/testschema/ConfigurableB.java
==============================================================================
---
development/main/metro/testing/testschema/src/main/net/dpml/test/testschema/ConfigurableB.java
(original)
+++
development/main/metro/testing/testschema/src/main/net/dpml/test/testschema/ConfigurableB.java
Sat Jan 1 16:12:44 2005
@@ -31,11 +31,6 @@

private Logger m_logger;

- /**
- * @see
net.dpml.configuration.Configurable#configure(net.dpml.configuration.Configuration)
- * TODO: how should that URI look like? with streams, only absolute URIs
work!
- * @metro.configuration schema="ConfigurableB.xschema"
- */
public void configure( Configuration configuration ) throws
ConfigurationException
{
m_logger.info( "inside configure" );

Added:
development/main/metro/testing/testschema/src/main/net/dpml/test/testschema/ConfigurableB.xschema
==============================================================================
--- (empty file)
+++
development/main/metro/testing/testschema/src/main/net/dpml/test/testschema/ConfigurableB.xschema
Sat Jan 1 16:12:44 2005
@@ -0,0 +1,11 @@
+<?xml version="1.0" encoding="utf-8"?>
+<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema";>
+ <xs:element name="configuration">
+ <xs:complexType>
+ <xs:sequence>
+ <xs:element name="child1" type="xs:string"/>
+ </xs:sequence>
+ <xs:attribute name="attr1" type="xs:string"/>
+ </xs:complexType>
+ </xs:element>
+ </xs:schema>



  • svn commit: r1338 - in development/main/metro: composition/impl/src/main/net/dpml/composition/util composition/test/src/test/net/dpml/composition/util testing/testschema/src/main/net/dpml/test/testschema, pneubauer, 01/01/2005

Archive powered by MHonArc 2.6.24.

Top of Page