You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by js...@apache.org on 2007/04/07 03:55:55 UTC
svn commit: r526343 -
/incubator/tuscany/java/sca/modules/assembly-xml/src/main/java/org/apache/tuscany/assembly/xml/impl/
Author: jsdelfino
Date: Fri Apr 6 18:55:54 2007
New Revision: 526343
URL: http://svn.apache.org/viewvc?view=rev&rev=526343
Log:
Minor code cleanup, renamed Attr to XAttr, refactored DOMUtil into BaseArtifactProcessor, changed XAttr from public to package visibility.
Added:
incubator/tuscany/java/sca/modules/assembly-xml/src/main/java/org/apache/tuscany/assembly/xml/impl/XAttr.java (with props)
Removed:
incubator/tuscany/java/sca/modules/assembly-xml/src/main/java/org/apache/tuscany/assembly/xml/impl/Attr.java
incubator/tuscany/java/sca/modules/assembly-xml/src/main/java/org/apache/tuscany/assembly/xml/impl/DOMUtil.java
Modified:
incubator/tuscany/java/sca/modules/assembly-xml/src/main/java/org/apache/tuscany/assembly/xml/impl/BaseArtifactProcessor.java
incubator/tuscany/java/sca/modules/assembly-xml/src/main/java/org/apache/tuscany/assembly/xml/impl/ComponentTypeProcessor.java
incubator/tuscany/java/sca/modules/assembly-xml/src/main/java/org/apache/tuscany/assembly/xml/impl/CompositeProcessor.java
incubator/tuscany/java/sca/modules/assembly-xml/src/main/java/org/apache/tuscany/assembly/xml/impl/ConstrainingTypeProcessor.java
Modified: incubator/tuscany/java/sca/modules/assembly-xml/src/main/java/org/apache/tuscany/assembly/xml/impl/BaseArtifactProcessor.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/assembly-xml/src/main/java/org/apache/tuscany/assembly/xml/impl/BaseArtifactProcessor.java?view=diff&rev=526343&r1=526342&r2=526343
==============================================================================
--- incubator/tuscany/java/sca/modules/assembly-xml/src/main/java/org/apache/tuscany/assembly/xml/impl/BaseArtifactProcessor.java (original)
+++ incubator/tuscany/java/sca/modules/assembly-xml/src/main/java/org/apache/tuscany/assembly/xml/impl/BaseArtifactProcessor.java Fri Apr 6 18:55:54 2007
@@ -31,6 +31,8 @@
import javax.xml.XMLConstants;
import javax.xml.namespace.QName;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
@@ -58,6 +60,7 @@
import org.apache.tuscany.services.spi.contribution.ContributionReadException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
+import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
/**
@@ -70,6 +73,11 @@
private AssemblyFactory factory;
private PolicyFactory policyFactory;
+ private static final DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
+ static {
+ domFactory.setNamespaceAware(true);
+ }
+
/**
* Constructs a new BaseArtifactProcessor.
*/
@@ -280,8 +288,12 @@
prop.setMustSupply(getBoolean(reader, "mustSupply"));
prop.setXSDElement(getQName(reader, "element"));
prop.setXSDType(getQName(reader, "type"));
- Node value = readPropertyValue(reader, prop.getXSDType());
- prop.setDefaultValue(value);
+ try {
+ Document value = readPropertyValue(reader, prop.getXSDType());
+ prop.setDefaultValue(value);
+ } catch (ParserConfigurationException e) {
+ throw new ContributionReadException(e);
+ }
}
/**
@@ -384,49 +396,13 @@
}
/**
- * Read a property value.
- * @param reader
- * @param type
- * @return
- * @throws XMLStreamException
- * @throws ContributionReadException
- */
- protected Document readPropertyValue(XMLStreamReader reader, QName type)
- throws XMLStreamException, ContributionReadException {
- Document doc = DOMUtil.newDocument();
-
- // root element has no namespace and local name "value"
- Element root = doc.createElementNS(null, "value");
- if (type != null) {
- org.w3c.dom.Attr xsi = doc.createAttributeNS(XMLNS_ATTRIBUTE_NS_URI, "xmlns:xsi");
- xsi.setValue(W3C_XML_SCHEMA_INSTANCE_NS_URI);
- root.setAttributeNodeNS(xsi);
-
- String prefix = type.getPrefix();
- if (prefix == null || prefix.length() == 0) {
- prefix = "ns";
- }
-
- DOMUtil.declareNamespace(root, prefix, type.getNamespaceURI());
-
- org.w3c.dom.Attr xsiType = doc.createAttributeNS(W3C_XML_SCHEMA_INSTANCE_NS_URI, "xsi:type");
- xsiType.setValue(prefix + ":" + type.getLocalPart());
- root.setAttributeNodeNS(xsiType);
- }
- doc.appendChild(root);
-
- DOMUtil.loadDOM(reader, root);
- return doc;
- }
-
- /**
* Start an element.
* @param uri
* @param name
* @param attrs
* @throws XMLStreamException
*/
- protected void writeStart(XMLStreamWriter writer, String uri, String name, Attr... attrs) throws XMLStreamException {
+ protected void writeStart(XMLStreamWriter writer, String uri, String name, XAttr... attrs) throws XMLStreamException {
writer.writeStartElement(uri, name);
writeAttributes(writer, attrs);
}
@@ -438,7 +414,7 @@
* @param attrs
* @throws XMLStreamException
*/
- protected void writeStart(XMLStreamWriter writer, String name, Attr... attrs) throws XMLStreamException {
+ protected void writeStart(XMLStreamWriter writer, String name, XAttr... attrs) throws XMLStreamException {
writer.writeStartElement(SCA10_NS, name);
writeAttributes(writer, attrs);
}
@@ -457,7 +433,7 @@
* @param writer
* @throws XMLStreamException
*/
- protected void writeStartDocument(XMLStreamWriter writer, String name, Attr... attrs) throws XMLStreamException {
+ protected void writeStartDocument(XMLStreamWriter writer, String name, XAttr... attrs) throws XMLStreamException {
writer.writeStartDocument();
writer.setDefaultNamespace(SCA10_NS);
writeStart(writer, name, attrs);
@@ -479,8 +455,8 @@
* @param attrs
* @throws XMLStreamException
*/
- protected void writeAttributes(XMLStreamWriter writer, Attr... attrs) throws XMLStreamException {
- for (Attr attr : attrs) {
+ protected void writeAttributes(XMLStreamWriter writer, XAttr... attrs) throws XMLStreamException {
+ for (XAttr attr : attrs) {
if (attr != null)
attr.write(writer);
}
@@ -516,4 +492,158 @@
return null;
}
+ /**
+ * Read a property value into a DOM document.
+ * @param reader
+ * @param type
+ * @return
+ * @throws XMLStreamException
+ * @throws ContributionReadException
+ * @throws ParserConfigurationException
+ */
+ protected Document readPropertyValue(XMLStreamReader reader, QName type)
+ throws XMLStreamException, ParserConfigurationException {
+
+ Document doc = createDocument();
+
+ // root element has no namespace and local name "value"
+ Element root = doc.createElementNS(null, "value");
+ if (type != null) {
+ org.w3c.dom.Attr xsi = doc.createAttributeNS(XMLNS_ATTRIBUTE_NS_URI, "xmlns:xsi");
+ xsi.setValue(W3C_XML_SCHEMA_INSTANCE_NS_URI);
+ root.setAttributeNodeNS(xsi);
+
+ String prefix = type.getPrefix();
+ if (prefix == null || prefix.length() == 0) {
+ prefix = "ns";
+ }
+
+ declareNamespace(root, prefix, type.getNamespaceURI());
+
+ org.w3c.dom.Attr xsiType = doc.createAttributeNS(W3C_XML_SCHEMA_INSTANCE_NS_URI, "xsi:type");
+ xsiType.setValue(prefix + ":" + type.getLocalPart());
+ root.setAttributeNodeNS(xsiType);
+ }
+ doc.appendChild(root);
+
+ loadElement(reader, root);
+ return doc;
+ }
+
+ /**
+ * Create a new DOM document.
+ * @return
+ * @throws ContributionReadException
+ */
+ private Document createDocument() throws ParserConfigurationException {
+ return domFactory.newDocumentBuilder().newDocument();
+ }
+
+ /**
+ * Create a DOM element
+ * @param document
+ * @param name
+ * @return
+ */
+ private Element createElement(Document document, QName name) {
+ String prefix = name.getPrefix();
+ String qname = (prefix != null && prefix.length() > 0) ? prefix + ":" + name.getLocalPart() : name
+ .getLocalPart();
+ return document.createElementNS(name.getNamespaceURI(), qname);
+ }
+
+ /**
+ * Declare a namespace.
+ * @param element
+ * @param prefix
+ * @param ns
+ */
+ private void declareNamespace(Element element, String prefix, String ns) {
+ String qname = null;
+ if ("".equals(prefix)) {
+ qname = "xmlns";
+ } else {
+ qname = "xmlns:" + prefix;
+ }
+ Node node = element;
+ boolean declared = false;
+ while (node != null && node.getNodeType() == Node.ELEMENT_NODE) {
+ NamedNodeMap attrs = node.getAttributes();
+ if (attrs == null) {
+ break;
+ }
+ Node attr = attrs.getNamedItem(qname);
+ if (attr != null) {
+ declared = ns.equals(attr.getNodeValue());
+ break;
+ }
+ node = node.getParentNode();
+ }
+ if (!declared) {
+ org.w3c.dom.Attr attr = element.getOwnerDocument().createAttributeNS(XMLNS_ATTRIBUTE_NS_URI, qname);
+ attr.setValue(ns);
+ element.setAttributeNodeNS(attr);
+ }
+ }
+
+ /**
+ * Load a property value specification from an StAX stream into a DOM
+ * Document. Only elements, text and attributes are processed; all comments
+ * and other whitespace are ignored.
+ *
+ * @param reader the stream to read from
+ * @param root the DOM node to load
+ * @throws javax.xml.stream.XMLStreamException
+ */
+ private void loadElement(XMLStreamReader reader, Element root) throws XMLStreamException {
+ Document document = root.getOwnerDocument();
+ Node current = root;
+ while (true) {
+ switch (reader.next()) {
+ case XMLStreamConstants.START_ELEMENT:
+ QName name = reader.getName();
+ Element child = createElement(document, name);
+
+ // push the new element and make it the current one
+ current.appendChild(child);
+ current = child;
+
+ declareNamespace(child, name.getPrefix(), name.getNamespaceURI());
+
+ int count = reader.getNamespaceCount();
+ for (int i = 0; i < count; i++) {
+ String prefix = reader.getNamespacePrefix(i);
+ String ns = reader.getNamespaceURI(i);
+ declareNamespace(child, prefix, ns);
+ }
+
+ // add the attributes for this element
+ count = reader.getAttributeCount();
+ for (int i = 0; i < count; i++) {
+ String ns = reader.getAttributeNamespace(i);
+ String prefix = reader.getAttributePrefix(i);
+ String localPart = reader.getAttributeLocalName(i);
+ String value = reader.getAttributeValue(i);
+ child.setAttributeNS(ns, localPart, value);
+ declareNamespace(child, prefix, ns);
+ }
+
+ break;
+ case XMLStreamConstants.CDATA:
+ current.appendChild(document.createCDATASection(reader.getText()));
+ break;
+ case XMLStreamConstants.CHARACTERS:
+ current.appendChild(document.createTextNode(reader.getText()));
+ break;
+ case XMLStreamConstants.END_ELEMENT:
+ // if we are back at the root then we are done
+ if (current == root) {
+ return;
+ }
+
+ // pop the element off the stack
+ current = current.getParentNode();
+ }
+ }
+ }
}
Modified: incubator/tuscany/java/sca/modules/assembly-xml/src/main/java/org/apache/tuscany/assembly/xml/impl/ComponentTypeProcessor.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/assembly-xml/src/main/java/org/apache/tuscany/assembly/xml/impl/ComponentTypeProcessor.java?view=diff&rev=526343&r1=526342&r2=526343
==============================================================================
--- incubator/tuscany/java/sca/modules/assembly-xml/src/main/java/org/apache/tuscany/assembly/xml/impl/ComponentTypeProcessor.java (original)
+++ incubator/tuscany/java/sca/modules/assembly-xml/src/main/java/org/apache/tuscany/assembly/xml/impl/ComponentTypeProcessor.java Fri Apr 6 18:55:54 2007
@@ -208,10 +208,10 @@
try {
writeStartDocument(writer, COMPONENT_TYPE,
- new Attr(Constants.CONSTRAINING_TYPE, getConstrainingTypeAttr(componentType)));
+ new XAttr(Constants.CONSTRAINING_TYPE, getConstrainingTypeAttr(componentType)));
for (Service service : componentType.getServices()) {
- writeStart(writer, SERVICE, new Attr(NAME, service.getName()));
+ writeStart(writer, SERVICE, new XAttr(NAME, service.getName()));
if (service.getCallback() != null) {
writeStart(writer, CALLBACK);
writeEnd(writer);
@@ -223,8 +223,8 @@
// TODO handle multivalued target attribute
String target = reference.getTargets().isEmpty() ? null : reference.getTargets().get(0).getName();
writeStart(writer, REFERENCE,
- new Attr(NAME, reference.getName()),
- new Attr(TARGET, target));
+ new XAttr(NAME, reference.getName()),
+ new XAttr(TARGET, target));
if (reference.getCallback() != null) {
writeStart(writer, CALLBACK);
writeEnd(writer);
@@ -233,7 +233,7 @@
}
for (Property property : componentType.getProperties()) {
- writeStart(writer, PROPERTY, new Attr(NAME, property.getName()));
+ writeStart(writer, PROPERTY, new XAttr(NAME, property.getName()));
writeEnd(writer);
}
Modified: incubator/tuscany/java/sca/modules/assembly-xml/src/main/java/org/apache/tuscany/assembly/xml/impl/CompositeProcessor.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/assembly-xml/src/main/java/org/apache/tuscany/assembly/xml/impl/CompositeProcessor.java?view=diff&rev=526343&r1=526342&r2=526343
==============================================================================
--- incubator/tuscany/java/sca/modules/assembly-xml/src/main/java/org/apache/tuscany/assembly/xml/impl/CompositeProcessor.java (original)
+++ incubator/tuscany/java/sca/modules/assembly-xml/src/main/java/org/apache/tuscany/assembly/xml/impl/CompositeProcessor.java Fri Apr 6 18:55:54 2007
@@ -333,13 +333,13 @@
public void write(Composite composite, XMLStreamWriter writer) throws ContributionWriteException {
try {
- writeStartDocument(writer, COMPOSITE, new Attr(CONSTRAINING_TYPE, getConstrainingTypeAttr(composite)));
+ writeStartDocument(writer, COMPOSITE, new XAttr(CONSTRAINING_TYPE, getConstrainingTypeAttr(composite)));
for (Service service : composite.getServices()) {
CompositeService compositeService = (CompositeService)service;
ComponentService promotedService = compositeService.getPromotedService();
String promote = promotedService != null ? promotedService.getName() : null;
- writeStart(writer, SERVICE, new Attr(NAME, service.getName()), new Attr(PROMOTE, promote));
+ writeStart(writer, SERVICE, new XAttr(NAME, service.getName()), new XAttr(PROMOTE, promote));
if (service.getCallback() != null) {
writeStart(writer, CALLBACK);
writeEnd(writer);
@@ -348,10 +348,10 @@
}
for (Component component : composite.getComponents()) {
- writeStart(writer, COMPONENT, new Attr(NAME, component.getName()));
+ writeStart(writer, COMPONENT, new XAttr(NAME, component.getName()));
for (ComponentService service : component.getServices()) {
- writeStart(writer, SERVICE, new Attr(NAME, service.getName()));
+ writeStart(writer, SERVICE, new XAttr(NAME, service.getName()));
writeEnd(writer);
if (service.getCallback() != null) {
writeStart(writer, CALLBACK);
@@ -363,8 +363,8 @@
// TODO handle multivalued target attribute
String target = reference.getTargets().isEmpty() ? null : reference.getTargets().get(0).getName();
writeStart(writer, REFERENCE,
- new Attr(NAME, reference.getName()),
- new Attr(TARGET,target));
+ new XAttr(NAME, reference.getName()),
+ new XAttr(TARGET,target));
if (reference.getCallback() != null) {
writeStart(writer, CALLBACK);
writeEnd(writer);
@@ -373,7 +373,7 @@
}
for (ComponentProperty property : component.getProperties()) {
- writeStart(writer, PROPERTY, new Attr(NAME, property.getName()));
+ writeStart(writer, PROPERTY, new XAttr(NAME, property.getName()));
writeEnd(writer);
}
@@ -389,8 +389,8 @@
else
promote = null;
writeStart(writer, REFERENCE,
- new Attr(NAME, reference.getName()),
- new Attr(PROMOTE, promote));
+ new XAttr(NAME, reference.getName()),
+ new XAttr(PROMOTE, promote));
if (reference.getCallback() != null) {
writeStart(writer, CALLBACK);
writeEnd(writer);
@@ -399,7 +399,7 @@
}
for (Property property : composite.getProperties()) {
- writeStart(writer, PROPERTY, new Attr(NAME, property.getName()));
+ writeStart(writer, PROPERTY, new XAttr(NAME, property.getName()));
writeEnd(writer);
}
Modified: incubator/tuscany/java/sca/modules/assembly-xml/src/main/java/org/apache/tuscany/assembly/xml/impl/ConstrainingTypeProcessor.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/assembly-xml/src/main/java/org/apache/tuscany/assembly/xml/impl/ConstrainingTypeProcessor.java?view=diff&rev=526343&r1=526342&r2=526343
==============================================================================
--- incubator/tuscany/java/sca/modules/assembly-xml/src/main/java/org/apache/tuscany/assembly/xml/impl/ConstrainingTypeProcessor.java (original)
+++ incubator/tuscany/java/sca/modules/assembly-xml/src/main/java/org/apache/tuscany/assembly/xml/impl/ConstrainingTypeProcessor.java Fri Apr 6 18:55:54 2007
@@ -178,18 +178,18 @@
writeStartDocument(writer, CONSTRAINING_TYPE);
for (AbstractService service : constrainingType.getServices()) {
- writeStart(writer, SERVICE, new Attr(NAME, service.getName()));
+ writeStart(writer, SERVICE, new XAttr(NAME, service.getName()));
writeEnd(writer);
}
for (AbstractReference reference : constrainingType.getReferences()) {
writeStart(writer, REFERENCE,
- new Attr(NAME, reference.getName()));
+ new XAttr(NAME, reference.getName()));
writeEnd(writer);
}
for (AbstractProperty property : constrainingType.getProperties()) {
- writeStart(writer, PROPERTY, new Attr(NAME, property.getName()));
+ writeStart(writer, PROPERTY, new XAttr(NAME, property.getName()));
writeEnd(writer);
}
Added: incubator/tuscany/java/sca/modules/assembly-xml/src/main/java/org/apache/tuscany/assembly/xml/impl/XAttr.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/assembly-xml/src/main/java/org/apache/tuscany/assembly/xml/impl/XAttr.java?view=auto&rev=526343
==============================================================================
--- incubator/tuscany/java/sca/modules/assembly-xml/src/main/java/org/apache/tuscany/assembly/xml/impl/XAttr.java (added)
+++ incubator/tuscany/java/sca/modules/assembly-xml/src/main/java/org/apache/tuscany/assembly/xml/impl/XAttr.java Fri Apr 6 18:55:54 2007
@@ -0,0 +1,123 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.tuscany.assembly.xml.impl;
+
+import javax.xml.namespace.NamespaceContext;
+import javax.xml.namespace.QName;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamWriter;
+
+import org.apache.tuscany.assembly.xml.Constants;
+
+/**
+ * Represents an XML attribute that needs to be written to a document.
+ *
+ * @version $Rev$ $Date$
+ */
+class XAttr {
+
+ String uri = Constants.SCA10_NS;
+ String name;
+ Object value;
+
+ public XAttr(String uri, String name, String value) {
+ this.uri = uri;
+ this.name = name;
+ this.value = value;
+ }
+
+ public XAttr(String name, String value) {
+ this.name = name;
+ this.value = value;
+ }
+
+ public XAttr(String uri, String name, boolean value) {
+ this.uri = uri;
+ this.name = name;
+ this.value = value;
+ }
+
+ public XAttr(String name, boolean value) {
+ this.name = name;
+ this.value = value;
+ }
+
+ public XAttr(String uri, String name, QName value) {
+ this.uri = uri;
+ this.name = name;
+ this.value = value;
+ }
+
+ public XAttr(String name, QName value) {
+ this.name = name;
+ this.value = value;
+ }
+
+ /**
+ * Writes a string from a qname and registers a prefix for its namespace.
+ * @param reader
+ * @param value
+ * @return
+ */
+ protected String writeQNameValue(XMLStreamWriter writer, QName qname) throws XMLStreamException {
+ if (qname != null) {
+ String prefix = qname.getPrefix();
+ String uri = qname.getNamespaceURI();
+ prefix = writer.getPrefix(uri);
+ if (prefix != null) {
+
+ // Use the prefix already bound to the given uri
+ return prefix + ":" + qname.getLocalPart();
+ } else {
+
+ // Find an available prefix and bind it to the given uri
+ NamespaceContext nsc = writer.getNamespaceContext();
+ for (int i=1; ; i++) {
+ prefix = "ns" + i;
+ if (nsc.getNamespaceURI(prefix) == null) {
+ break;
+ }
+ }
+ writer.setPrefix(prefix, uri);
+ writer.writeNamespace(prefix, uri);
+ return prefix + ":" + qname.getLocalPart();
+ }
+ } else {
+ return null;
+ }
+ }
+
+ void write(XMLStreamWriter writer) throws XMLStreamException {
+ if (value != null) {
+ String str;
+ if (value instanceof QName) {
+ str = writeQNameValue(writer, (QName)value);
+ } else {
+ str = String.valueOf(value);
+ }
+ if (uri != null && !uri.equals(Constants.SCA10_NS)) {
+ writer.writeAttribute(uri, name, str);
+ } else {
+ writer.writeAttribute(name,str);
+ }
+ }
+ }
+
+}
Propchange: incubator/tuscany/java/sca/modules/assembly-xml/src/main/java/org/apache/tuscany/assembly/xml/impl/XAttr.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/tuscany/java/sca/modules/assembly-xml/src/main/java/org/apache/tuscany/assembly/xml/impl/XAttr.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
---------------------------------------------------------------------
To unsubscribe, e-mail: tuscany-commits-unsubscribe@ws.apache.org
For additional commands, e-mail: tuscany-commits-help@ws.apache.org