Author: tfischer
Date: Fri Sep 3 19:53:06 2010
New Revision: 992439
URL: http://svn.apache.org/viewvc?rev=992439&view=rev
Log:
Add schema validation when reading source files
Modified:
db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/stream/FileSource.java
db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/stream/FileSourceProvider.java
db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/stream/PropertiesSourceFormat.java
db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/stream/StreamSourceFormat.java
db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/stream/XmlSourceFormat.java
db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/stream/XmlSourceSaxHandler.java
Modified: db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/stream/FileSource.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/stream/FileSource.java?rev=992439&r1=992438&r2=992439&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/stream/FileSource.java
(original)
+++ db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/stream/FileSource.java
Fri Sep 3 19:53:06 2010
@@ -27,6 +27,7 @@ import java.io.InputStream;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
+import org.apache.torque.generator.control.ControllerState;
import org.apache.torque.generator.source.SourceElement;
import org.apache.torque.generator.source.SourceException;
import org.apache.torque.generator.source.SourceImpl;
@@ -42,26 +43,33 @@ public class FileSource extends SourceIm
private static Log log = LogFactory.getLog(FileSource.class);
/**
- * The format of the file, e.g. properties or XML.
+ * The format of the file, e.g. properties or XML, not null.
*/
private StreamSourceFormat format;
/**
- * The path of the file.
+ * The path of the file, not null.
*/
private File path;
/**
+ * The controller state, not null.
+ */
+ private ControllerState controllerState;
+
+ /**
* Constructor.
*
* @param format the source format, not null.
* @param path the path to the file to read, not null.
+ * @param ControllerState the controller state, not null.
*
* @throws NullPointerException if path or format is null.
*/
public FileSource(
StreamSourceFormat format,
- File path)
+ File path,
+ ControllerState controllerState)
{
if (path == null)
{
@@ -71,8 +79,13 @@ public class FileSource extends SourceIm
{
throw new NullPointerException("format must not be null");
}
+ if (controllerState == null)
+ {
+ throw new NullPointerException("controllerState must not be null");
+ }
this.format = format;
this.path = path;
+ this.controllerState = controllerState;
}
/**
@@ -118,7 +131,7 @@ public class FileSource extends SourceIm
+ path.getAbsolutePath()
+ " of type "
+ format.getKey());
- result = format.parse(inputStream);
+ result = format.parse(inputStream, controllerState);
}
catch (FileNotFoundException e)
{
Modified: db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/stream/FileSourceProvider.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/stream/FileSourceProvider.java?rev=992439&r1=992438&r2=992439&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/stream/FileSourceProvider.java
(original)
+++ db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/stream/FileSourceProvider.java
Fri Sep 3 19:53:06 2010
@@ -63,6 +63,10 @@ public class FileSourceProvider extends
/** The known stream source formats. */
private Set<StreamSourceFormat> streamSourceFormats;
+ /** The Controller state. */
+ private ControllerState controllerState;
+
+
/**
* Constructor.
*
@@ -107,6 +111,12 @@ public class FileSourceProvider extends
ControllerState controllerState)
throws ConfigurationException
{
+ if (controllerState == null)
+ {
+ throw new NullPointerException("controllerState must not be null");
+ }
+ this.controllerState = controllerState;
+
if (sourceFileset.getBasedir() == null)
{
throw new ConfigurationException(
@@ -132,6 +142,7 @@ public class FileSourceProvider extends
paths = null;
pathIt = null;
streamSourceFormats = null;
+ controllerState = null;
}
public boolean hasNext()
@@ -177,7 +188,8 @@ public class FileSourceProvider extends
}
return new FileSource(
currentSourceFormat,
- currentPath);
+ currentPath,
+ controllerState);
}
public void remove()
Modified: db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/stream/PropertiesSourceFormat.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/stream/PropertiesSourceFormat.java?rev=992439&r1=992438&r2=992439&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/stream/PropertiesSourceFormat.java
(original)
+++ db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/stream/PropertiesSourceFormat.java
Fri Sep 3 19:53:06 2010
@@ -26,6 +26,7 @@ import java.util.Properties;
import java.util.Set;
import org.apache.commons.collections.set.ListOrderedSet;
+import org.apache.torque.generator.control.ControllerState;
import org.apache.torque.generator.source.SourceElement;
import org.apache.torque.generator.source.SourceException;
@@ -82,6 +83,7 @@ public final class PropertiesSourceForma
* created element tree.
*
* @param inputStream the input stream to parse, not null.
+ * @param ControllerState the controller state.
*
* @return the root element of the created tree, not null.
*
@@ -91,7 +93,9 @@ public final class PropertiesSourceForma
* malformed unicode escape sequence.
* @throws NullPointerException if inputStream is null.
*/
- public SourceElement parse(InputStream inputStream)
+ public SourceElement parse(
+ InputStream inputStream,
+ ControllerState controllerState)
throws SourceException
{
if (inputStream == null)
Modified: db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/stream/StreamSourceFormat.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/stream/StreamSourceFormat.java?rev=992439&r1=992438&r2=992439&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/stream/StreamSourceFormat.java
(original)
+++ db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/stream/StreamSourceFormat.java
Fri Sep 3 19:53:06 2010
@@ -22,6 +22,7 @@ package org.apache.torque.generator.sour
import java.io.InputStream;
+import org.apache.torque.generator.control.ControllerState;
import org.apache.torque.generator.source.SourceElement;
import org.apache.torque.generator.source.SourceException;
@@ -51,6 +52,7 @@ public interface StreamSourceFormat
* Parses a source file and returns its root element.
*
* @param inputStream the stream to read the source file from, not null.
+ * @param ControllerState the controller state, not null.
*
* @return the root element of the source, containing the rest of
* the source as linked elements.
@@ -58,5 +60,8 @@ public interface StreamSourceFormat
* @throws SourceException if reading or parsing the source fails.
* @throws NullPointerException if <code>inputStream</code> is null.
*/
- SourceElement parse(InputStream inputStream) throws SourceException;
+ SourceElement parse(
+ InputStream inputStream,
+ ControllerState controllerState)
+ throws SourceException;
}
Modified: db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/stream/XmlSourceFormat.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/stream/XmlSourceFormat.java?rev=992439&r1=992438&r2=992439&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/stream/XmlSourceFormat.java
(original)
+++ db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/stream/XmlSourceFormat.java
Fri Sep 3 19:53:06 2010
@@ -26,9 +26,13 @@ import javax.xml.parsers.ParserConfigura
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
+import org.apache.torque.generator.configuration.source.EntityReferences;
+import org.apache.torque.generator.control.ControllerState;
import org.apache.torque.generator.source.SourceElement;
import org.apache.torque.generator.source.SourceException;
import org.xml.sax.SAXException;
+import org.xml.sax.SAXNotRecognizedException;
+import org.xml.sax.SAXNotSupportedException;
/**
* The source type representing an XML source.
@@ -51,7 +55,26 @@ public final class XmlSourceFormat imple
static
{
SAX_FACTORY = SAXParserFactory.newInstance();
- SAX_FACTORY.setValidating(true);
+ SAX_FACTORY.setNamespaceAware(true);
+ try
+ {
+ SAX_FACTORY.setFeature(
+ "http://apache.org/xml/features/validation/dynamic", true);
+ SAX_FACTORY.setFeature(
+ "http://apache.org/xml/features/validation/schema", true);
+ }
+ catch (SAXNotSupportedException e)
+ {
+ throw new RuntimeException(e);
+ }
+ catch (SAXNotRecognizedException e)
+ {
+ throw new RuntimeException(e);
+ }
+ catch (ParserConfigurationException e)
+ {
+ throw new RuntimeException(e);
+ }
}
/**
@@ -78,7 +101,8 @@ public final class XmlSourceFormat imple
* Parses a stream containing xml data and creates a source element
* hierarchy from it.
*
- * @param inputStream the stream containing the xml data.
+ * @param xmlStream the stream containing the xml data, not null.
+ * @param ControllerState the controller state, not null.
*
* @return the root element of the created hierarchy.
*
@@ -86,10 +110,12 @@ public final class XmlSourceFormat imple
* parsing the XML data or if the SAX parser is not configured
* correctly.
*/
- public SourceElement parse(InputStream inputStream)
+ public SourceElement parse(
+ InputStream xmlStream,
+ ControllerState controllerState)
throws SourceException
{
- if (inputStream == null)
+ if (xmlStream == null)
{
throw new NullPointerException("No Input path specified");
}
@@ -97,9 +123,13 @@ public final class XmlSourceFormat imple
{
SAXParser parser = SAX_FACTORY.newSAXParser();
- XmlSourceSaxHandler handler = new XmlSourceSaxHandler();
+ EntityReferences entityReferences
+ = controllerState.getUnitConfiguration()
+ .getEntityReferences();
+ XmlSourceSaxHandler handler = new XmlSourceSaxHandler(
+ entityReferences);
- parser.parse(inputStream, handler);
+ parser.parse(xmlStream, handler);
return handler.getRoot();
}
catch (IOException e)
Modified: db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/stream/XmlSourceSaxHandler.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/stream/XmlSourceSaxHandler.java?rev=992439&r1=992438&r2=992439&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/stream/XmlSourceSaxHandler.java
(original)
+++ db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/stream/XmlSourceSaxHandler.java
Fri Sep 3 19:53:06 2010
@@ -19,9 +19,15 @@ package org.apache.torque.generator.sour
* under the License.
*/
+import java.io.IOException;
+
+import org.apache.torque.generator.configuration.ConfigurationEntityResolver;
+import org.apache.torque.generator.configuration.source.EntityReferences;
import org.apache.torque.generator.source.SourceElement;
import org.xml.sax.Attributes;
+import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
+import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;
/**
@@ -39,6 +45,23 @@ public class XmlSourceSaxHandler extends
*/
private SourceElement root;
+ /** The known entity references, not null. */
+ private EntityReferences entityReferences;
+
+ /**
+ * Constructor.
+ *
+ * @param entityReferences the known entity references, not null.
+ */
+ public XmlSourceSaxHandler(EntityReferences entityReferences)
+ {
+ if (entityReferences == null)
+ {
+ throw new NullPointerException("entityReferences msut not be null");
+ }
+ this.entityReferences = entityReferences;
+ }
+
@Override
public void startElement(String uri, String localName,
String qName, Attributes attributes)
@@ -96,6 +119,23 @@ public class XmlSourceSaxHandler extends
}
/**
+ * EntityResolver implementation. Called by the XML parser
+ *
+ * @param publicId The public identifier of the external entity.
+ * @param systemId The system identifier of the external entity.
+ *
+ * @return an InputSource for the entity, or null if the URI is not known.
+ *
+ * @see ConfigurationEntityResolver#resolveEntity(String, String)
+ */
+ @Override
+ public InputSource resolveEntity(String publicId, String systemId)
+ throws SAXException, IOException
+ {
+ return entityReferences.resolveEntity(publicId, systemId);
+ }
+
+ /**
* Returns the root element of the tree.
*
* @return the root element (may be null if no elements were in the xml)
@@ -126,4 +166,25 @@ public class XmlSourceSaxHandler extends
}
return true;
}
+
+ @Override
+ public void error(SAXParseException exception)
+ throws SAXParseException
+ {
+ throw exception;
+ }
+
+ @Override
+ public void fatalError(SAXParseException exception)
+ throws SAXParseException
+ {
+ throw exception;
+ }
+
+ @Override
+ public void warning(SAXParseException exception)
+ throws SAXParseException
+ {
+ throw exception;
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: torque-dev-unsubscribe@db.apache.org
For additional commands, e-mail: torque-dev-help@db.apache.org
|