summaryrefslogtreecommitdiffstats
path: root/sca-java-1.x/branches/sca-java-0.91/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/XAttr.java
diff options
context:
space:
mode:
Diffstat (limited to 'sca-java-1.x/branches/sca-java-0.91/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/XAttr.java')
-rw-r--r--sca-java-1.x/branches/sca-java-0.91/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/XAttr.java122
1 files changed, 122 insertions, 0 deletions
diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/XAttr.java b/sca-java-1.x/branches/sca-java-0.91/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/XAttr.java
new file mode 100644
index 0000000000..3c991798de
--- /dev/null
+++ b/sca-java-1.x/branches/sca-java-0.91/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/XAttr.java
@@ -0,0 +1,122 @@
+/*
+ * 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.sca.assembly.xml;
+
+import javax.xml.namespace.NamespaceContext;
+import javax.xml.namespace.QName;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamWriter;
+
+
+/**
+ * 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);
+ }
+ }
+ }
+
+}