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/15 17:23:59 UTC
svn commit: r289258 - in
/beehive/trunk/netui/src/util/org/apache/beehive/netui/util/xml: ./
validation/ validation/internal/
Author: ekoneil
Date: Thu Sep 15 08:23:54 2005
New Revision: 289258
URL: http://svn.apache.org/viewcvs?rev=289258&view=rev
Log:
Add a set of XML utilities to NetUI. These do three major things:
- provide convenience methods for dealing with DOM
- provide a way to validate XML instance documents against an XSD with the possibility of making this JVM independent
- provide an abstraction for resolving an InputStream from a resource path
All of this works using the XML parsing / validating infrastructure that is available in the Java 5 JDK. It should be possible to switch this for other XML parsing libraries by adding JARs to the NetUI webapp and registering the correct XML handling services.
The XSD / XML validation code uses validation from the Java 5 JDK; in certain cases (like Tomcat 5.0.x), the VM environment is not configured correctly for the Java 5 JDK's XML configuration. In this case, the root of the problem is that a SAX TransformerFactory has been registered that exists in JDK 1.4 but not in JDK 5 (they've been repackaged, as you'll recall...). This happens because something in Tomcat steals the TransformerFactory service and registers a class that doesn't exist. The SchemaValidatorJava5 handles this by catching TransformerFactoryConfigurationError and logging a verbose warning when an XML instance document can not be validated. Obviously, this isn't ideal, but validation can be enabled on Tomcat 5.0 easily.
BB: self
Test: NetUI BVT pass
Added:
beehive/trunk/netui/src/util/org/apache/beehive/netui/util/xml/
beehive/trunk/netui/src/util/org/apache/beehive/netui/util/xml/DomUtils.java (with props)
beehive/trunk/netui/src/util/org/apache/beehive/netui/util/xml/XmlInputStreamResolver.java (with props)
beehive/trunk/netui/src/util/org/apache/beehive/netui/util/xml/validation/
beehive/trunk/netui/src/util/org/apache/beehive/netui/util/xml/validation/SchemaValidationException.java (with props)
beehive/trunk/netui/src/util/org/apache/beehive/netui/util/xml/validation/SchemaValidator.java (with props)
beehive/trunk/netui/src/util/org/apache/beehive/netui/util/xml/validation/SchemaValidatorFactory.java (with props)
beehive/trunk/netui/src/util/org/apache/beehive/netui/util/xml/validation/internal/
beehive/trunk/netui/src/util/org/apache/beehive/netui/util/xml/validation/internal/SchemaValidatorJava5.java (with props)
Added: beehive/trunk/netui/src/util/org/apache/beehive/netui/util/xml/DomUtils.java
URL: http://svn.apache.org/viewcvs/beehive/trunk/netui/src/util/org/apache/beehive/netui/util/xml/DomUtils.java?rev=289258&view=auto
==============================================================================
--- beehive/trunk/netui/src/util/org/apache/beehive/netui/util/xml/DomUtils.java (added)
+++ beehive/trunk/netui/src/util/org/apache/beehive/netui/util/xml/DomUtils.java Thu Sep 15 08:23:54 2005
@@ -0,0 +1,204 @@
+/*
+ * B E A S Y S T E M S
+ * Copyright 2002-2004 BEA Systems, Inc.
+ *
+ * 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.
+ *
+ * $Header:$
+ */
+package org.apache.beehive.netui.util.xml;
+
+import java.util.List;
+import java.util.ArrayList;
+
+import org.w3c.dom.Node;
+import org.w3c.dom.Element;
+import org.w3c.dom.NodeList;
+import org.w3c.dom.Text;
+import org.w3c.dom.Attr;
+
+/**
+ * <p>This class exists simply because DOM does so inconvenient to use.</p>
+ */
+public final class DomUtils {
+
+ /* do not construct */
+ private DomUtils() {}
+
+ /**
+ * <p>Returns the first child element with the given name. Returns
+ * <code>null</code> if not found.</p>
+ *
+ * @param parent parent element
+ * @param name name of the child element
+ * @return child element
+ */
+ public static Element getChildElementByName(Element parent, String name)
+ {
+ NodeList children = parent.getChildNodes();
+
+ for(int i = 0; i < children.getLength(); i++) {
+ Node node = children.item(i);
+ if(node.getNodeType() == Node.ELEMENT_NODE) {
+ Element element = (Element) node;
+ if(element.getTagName().equals(name)) {
+ return element;
+ }
+ }
+ }
+
+ return null;
+ }
+
+ /**
+ * <p>Returns a list of child elements with the given
+ * name. Returns an empty list if there are no such child
+ * elements.</p>
+ *
+ * @param parent parent element
+ * @param name name of the child element
+ * @return child elements
+ */
+ public static List getChildElementsByName(Element parent, String name)
+ {
+ List elements = new ArrayList();
+
+ NodeList children = parent.getChildNodes();
+
+ for(int i = 0; i < children.getLength(); i++) {
+ Node node = children.item(i);
+ if(node.getNodeType() == Node.ELEMENT_NODE) {
+ Element element = (Element) node;
+ if(element.getTagName().equals(name)) {
+ elements.add(element);
+ }
+ }
+ }
+
+ return elements;
+ }
+
+ /**
+ * <p>Returns the text value of a child element. Returns
+ * <code>null</code> if there is no child element found.</p>
+ *
+ * @param parent parent element
+ * @param name name of the child element
+ * @return text value
+ */
+ public static String getChildElementText(Element parent, String name)
+ {
+ // Get children
+ List list = DomUtils.getChildElementsByName(parent, name);
+
+ if(list.size() == 1) {
+ Element child = (Element) list.get(0);
+
+ StringBuffer buf = new StringBuffer();
+
+ NodeList children = child.getChildNodes();
+ for(int i = 0; i < children.getLength(); i++) {
+ Node node = children.item(i);
+ if(node.getNodeType() == Node.TEXT_NODE ||
+ node.getNodeType() == Node.CDATA_SECTION_NODE) {
+ Text text = (Text) node;
+ buf.append(text.getData().trim());
+ }
+ }
+
+ return buf.toString();
+ }
+ else {
+ return null;
+ }
+ }
+
+ /**
+ * <p>Returns the text value of a child element. Returns
+ * <code>null</code> if there is no child element found.</p>
+ *
+ * @param element element
+ * @return text value
+ */
+ public static String getElementText(Element element)
+ {
+ StringBuffer buf = new StringBuffer();
+
+ NodeList children = element.getChildNodes();
+ for(int i = 0; i < children.getLength(); i++) {
+ Node node = children.item(i);
+ if(node.getNodeType() == Node.TEXT_NODE ||
+ node.getNodeType() == Node.CDATA_SECTION_NODE) {
+ Text text = (Text) node;
+ buf.append(text.getData().trim());
+ }
+ }
+
+ return buf.toString();
+ }
+
+ /**
+ * <p>Returns an array of text values of a child element. Returns
+ * <code>null</code> if there is no child element found.</p>
+ *
+ * @param parent parent element
+ * @param name name of the child element
+ * @return text value
+ */
+ public static String[] getChildElementTextArr(Element parent, String name)
+ {
+ // Get all the elements
+ List children = getChildElementsByName(parent, name);
+
+ String str[] = new String[children.size()];
+
+ for(int i = 0; i < children.size(); i++) {
+ Node child = (Node) children.get(i);
+
+ StringBuffer buf = new StringBuffer();
+
+ NodeList nodes = child.getChildNodes();
+ for(int j = 0; j < nodes.getLength(); j++) {
+ Node node = nodes.item(j);
+ if(node.getNodeType() == Node.TEXT_NODE ||
+ node.getNodeType() == Node.CDATA_SECTION_NODE) {
+ Text text = (Text) node;
+ buf.append(text.getData().trim());
+ }
+ }
+
+ str[i] = buf.toString();
+ }
+
+ return str;
+ }
+
+ /**
+ * <p>Retutns the value of the named attribute of the given
+ * element. If there is no such attribute, returns null.</p>
+ *
+ * @param element element
+ * @param name name
+ * @return value
+ */
+ public static String getAttributeValue(Element element, String name)
+ {
+ Attr attribute = element.getAttributeNode(name);
+ if(attribute == null) {
+ return null;
+ }
+ else {
+ return attribute.getValue();
+ }
+ }
+}
Propchange: beehive/trunk/netui/src/util/org/apache/beehive/netui/util/xml/DomUtils.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: beehive/trunk/netui/src/util/org/apache/beehive/netui/util/xml/XmlInputStreamResolver.java
URL: http://svn.apache.org/viewcvs/beehive/trunk/netui/src/util/org/apache/beehive/netui/util/xml/XmlInputStreamResolver.java?rev=289258&view=auto
==============================================================================
--- beehive/trunk/netui/src/util/org/apache/beehive/netui/util/xml/XmlInputStreamResolver.java (added)
+++ beehive/trunk/netui/src/util/org/apache/beehive/netui/util/xml/XmlInputStreamResolver.java Thu Sep 15 08:23:54 2005
@@ -0,0 +1,27 @@
+/*
+ * Copyright 2004-2005 The Apache Software Foundation.
+ *
+ * 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.
+ *
+ * $Header:$
+ */
+package org.apache.beehive.netui.util.xml;
+
+import java.io.InputStream;
+
+public abstract class XmlInputStreamResolver {
+
+ public abstract String getResourcePath();
+
+ public abstract InputStream getInputStream();
+}
Propchange: beehive/trunk/netui/src/util/org/apache/beehive/netui/util/xml/XmlInputStreamResolver.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: beehive/trunk/netui/src/util/org/apache/beehive/netui/util/xml/validation/SchemaValidationException.java
URL: http://svn.apache.org/viewcvs/beehive/trunk/netui/src/util/org/apache/beehive/netui/util/xml/validation/SchemaValidationException.java?rev=289258&view=auto
==============================================================================
--- beehive/trunk/netui/src/util/org/apache/beehive/netui/util/xml/validation/SchemaValidationException.java (added)
+++ beehive/trunk/netui/src/util/org/apache/beehive/netui/util/xml/validation/SchemaValidationException.java Thu Sep 15 08:23:54 2005
@@ -0,0 +1,41 @@
+/*
+ * B E A S Y S T E M S
+ * Copyright 2002-2004 BEA Systems, Inc.
+ *
+ * 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.
+ *
+ * $Header:$
+ */
+package org.apache.beehive.netui.util.xml.validation;
+
+/**
+ */
+public class SchemaValidationException
+ extends RuntimeException {
+
+ public SchemaValidationException() {
+ super();
+ }
+
+ public SchemaValidationException(String msg) {
+ super(msg);
+ }
+
+ public SchemaValidationException(Throwable cause) {
+ super(cause);
+ }
+
+ public SchemaValidationException(String msg, Throwable cause) {
+ super(msg, cause);
+ }
+}
Propchange: beehive/trunk/netui/src/util/org/apache/beehive/netui/util/xml/validation/SchemaValidationException.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: beehive/trunk/netui/src/util/org/apache/beehive/netui/util/xml/validation/SchemaValidator.java
URL: http://svn.apache.org/viewcvs/beehive/trunk/netui/src/util/org/apache/beehive/netui/util/xml/validation/SchemaValidator.java?rev=289258&view=auto
==============================================================================
--- beehive/trunk/netui/src/util/org/apache/beehive/netui/util/xml/validation/SchemaValidator.java (added)
+++ beehive/trunk/netui/src/util/org/apache/beehive/netui/util/xml/validation/SchemaValidator.java Thu Sep 15 08:23:54 2005
@@ -0,0 +1,30 @@
+/*
+ * B E A S Y S T E M S
+ * Copyright 2002-2004 BEA Systems, Inc.
+ *
+ * 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.
+ *
+ * $Header:$
+ */
+package org.apache.beehive.netui.util.xml.validation;
+
+import java.io.InputStream;
+
+/**
+ */
+public abstract class SchemaValidator {
+
+ /**
+ */
+ public abstract void validate(InputStream xsdInputStream, InputStream xmlInputStrea);
+}
Propchange: beehive/trunk/netui/src/util/org/apache/beehive/netui/util/xml/validation/SchemaValidator.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: beehive/trunk/netui/src/util/org/apache/beehive/netui/util/xml/validation/SchemaValidatorFactory.java
URL: http://svn.apache.org/viewcvs/beehive/trunk/netui/src/util/org/apache/beehive/netui/util/xml/validation/SchemaValidatorFactory.java?rev=289258&view=auto
==============================================================================
--- beehive/trunk/netui/src/util/org/apache/beehive/netui/util/xml/validation/SchemaValidatorFactory.java (added)
+++ beehive/trunk/netui/src/util/org/apache/beehive/netui/util/xml/validation/SchemaValidatorFactory.java Thu Sep 15 08:23:54 2005
@@ -0,0 +1,34 @@
+/*
+ * B E A S Y S T E M S
+ * Copyright 2002-2004 BEA Systems, Inc.
+ *
+ * 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.
+ *
+ * $Header:$
+ */
+package org.apache.beehive.netui.util.xml.validation;
+
+import org.apache.beehive.netui.util.xml.validation.internal.SchemaValidatorJava5;
+import org.apache.beehive.netui.util.xml.validation.SchemaValidator;
+
+/**
+ */
+public class SchemaValidatorFactory {
+
+ public static SchemaValidator getInstance() {
+ return new SchemaValidatorJava5();
+ }
+
+ /* do not construct */
+ private SchemaValidatorFactory() {}
+}
Propchange: beehive/trunk/netui/src/util/org/apache/beehive/netui/util/xml/validation/SchemaValidatorFactory.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: beehive/trunk/netui/src/util/org/apache/beehive/netui/util/xml/validation/internal/SchemaValidatorJava5.java
URL: http://svn.apache.org/viewcvs/beehive/trunk/netui/src/util/org/apache/beehive/netui/util/xml/validation/internal/SchemaValidatorJava5.java?rev=289258&view=auto
==============================================================================
--- beehive/trunk/netui/src/util/org/apache/beehive/netui/util/xml/validation/internal/SchemaValidatorJava5.java (added)
+++ beehive/trunk/netui/src/util/org/apache/beehive/netui/util/xml/validation/internal/SchemaValidatorJava5.java Thu Sep 15 08:23:54 2005
@@ -0,0 +1,76 @@
+/*
+ * B E A S Y S T E M S
+ * Copyright 2002-2004 BEA Systems, Inc.
+ *
+ * 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.
+ *
+ * $Header:$
+ */
+package org.apache.beehive.netui.util.xml.validation.internal;
+
+import java.io.InputStream;
+import java.io.IOException;
+import javax.xml.XMLConstants;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.stream.StreamSource;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.TransformerFactoryConfigurationError;
+import javax.xml.validation.SchemaFactory;
+import javax.xml.validation.Schema;
+import javax.xml.validation.Validator;
+
+import org.apache.beehive.netui.util.xml.validation.SchemaValidationException;
+import org.apache.beehive.netui.util.xml.validation.SchemaValidator;
+import org.apache.beehive.netui.util.logging.Logger;
+import org.w3c.dom.Document;
+import org.xml.sax.SAXException;
+
+/**
+ *
+ */
+public final class SchemaValidatorJava5
+ extends SchemaValidator {
+
+ private static final Logger LOGGER = Logger.getInstance(SchemaValidatorJava5.class);
+
+ public void validate(InputStream xsdInputStream, InputStream xmlInputStream) {
+
+ try {
+ SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
+ StreamSource xsdStreamSource = new StreamSource(xsdInputStream);
+ Schema schema = factory.newSchema(xsdStreamSource);
+ Validator validator = schema.newValidator();
+
+ DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
+ Document document = documentBuilder.parse(xmlInputStream);
+
+ validator.validate(new DOMSource(document));
+ }
+ catch(SAXException e) {
+ throw new SchemaValidationException("Exception occurred validating document. Cause: " + e, e);
+ }
+ catch(IOException e) {
+ throw new SchemaValidationException("Exception occurred validating document. Cause: " + e, e);
+ }
+ catch(ParserConfigurationException e) {
+ throw new SchemaValidationException("Exception occurred validating document. Cause: " + e, e);
+ }
+ catch(TransformerFactoryConfigurationError error) {
+ if(LOGGER.isWarnEnabled())
+ LOGGER.warn("Beehive NetUI disabled validation on the given file because the XML TransformFactory was configured incorrectly. " +
+ "We suggest fixing this error as errors may occur as a result of having an invalid XML instance document. Cause: " + error, error);
+ }
+ }
+}
Propchange: beehive/trunk/netui/src/util/org/apache/beehive/netui/util/xml/validation/internal/SchemaValidatorJava5.java
------------------------------------------------------------------------------
svn:eol-style = native