001: /*
002: * The contents of this file are subject to the terms
003: * of the Common Development and Distribution License
004: * (the "License"). You may not use this file except
005: * in compliance with the License.
006: *
007: * You can obtain a copy of the license at
008: * https://jwsdp.dev.java.net/CDDLv1.0.html
009: * See the License for the specific language governing
010: * permissions and limitations under the License.
011: *
012: * When distributing Covered Code, include this CDDL
013: * HEADER in each file and include the License file at
014: * https://jwsdp.dev.java.net/CDDLv1.0.html If applicable,
015: * add the following below this CDDL HEADER, with the
016: * fields enclosed by brackets "[]" replaced with your
017: * own identifying information: Portions Copyright [yyyy]
018: * [name of copyright owner]
019: */
020:
021: package com.sun.xml.txw2;
022:
023: import com.sun.xml.txw2.output.XmlSerializer;
024: import com.sun.xml.txw2.annotation.XmlElement;
025: import com.sun.xml.txw2.annotation.XmlNamespace;
026:
027: import javax.xml.namespace.QName;
028:
029: /**
030: * Entry point to TXW.
031: *
032: * @author Kohsuke Kawaguchi
033: */
034: public abstract class TXW {
035: private TXW() {
036: } // no instanciation please
037:
038: /*package*/static QName getTagName(Class<?> c) {
039: String localName = "";
040: String nsUri = "##default";
041:
042: XmlElement xe = c.getAnnotation(XmlElement.class);
043: if (xe != null) {
044: localName = xe.value();
045: nsUri = xe.ns();
046: }
047:
048: if (localName.length() == 0) {
049: localName = c.getName();
050: int idx = localName.lastIndexOf('.');
051: if (idx >= 0)
052: localName = localName.substring(idx + 1);
053:
054: localName = Character.toLowerCase(localName.charAt(0))
055: + localName.substring(1);
056: }
057:
058: if (nsUri.equals("##default")) {
059: Package pkg = c.getPackage();
060: if (pkg != null) {
061: XmlNamespace xn = pkg.getAnnotation(XmlNamespace.class);
062: if (xn != null)
063: nsUri = xn.value();
064: }
065: }
066: if (nsUri.equals("##default"))
067: nsUri = "";
068:
069: return new QName(nsUri, localName);
070: }
071:
072: /**
073: * Creates a new {@link TypedXmlWriter} to write a new instance of a document.
074: *
075: * @param rootElement
076: * The {@link TypedXmlWriter} interface that declares the content model of the root element.
077: * This interface must have {@link XmlElement} annotation on it to designate the tag name
078: * of the root element.
079: * @param out
080: * The target of the writing.
081: */
082: public static <T extends TypedXmlWriter> T create(
083: Class<T> rootElement, XmlSerializer out) {
084: Document doc = new Document(out);
085: QName n = getTagName(rootElement);
086: return new ContainerElement(doc, null, n.getNamespaceURI(), n
087: .getLocalPart())._cast(rootElement);
088: }
089:
090: /**
091: * Creates a new {@link TypedXmlWriter} to write a new instance of a document.
092: *
093: * <p>
094: * Similar to the other method, but this version allows the caller to set the
095: * tag name at the run-time.
096: *
097: * @param tagName
098: * The tag name of the root document.
099: *
100: * @see #create(Class,XmlSerializer)
101: */
102: public static <T extends TypedXmlWriter> T create(QName tagName,
103: Class<T> rootElement, XmlSerializer out) {
104: return new ContainerElement(new Document(out), null, tagName
105: .getNamespaceURI(), tagName.getLocalPart())
106: ._cast(rootElement);
107: }
108: }
|