001: /*
002: * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025: package com.sun.xml.internal.txw2;
026:
027: import com.sun.xml.internal.txw2.output.XmlSerializer;
028: import com.sun.xml.internal.txw2.annotation.XmlElement;
029: import com.sun.xml.internal.txw2.annotation.XmlNamespace;
030:
031: import javax.xml.namespace.QName;
032:
033: /**
034: * Entry point to TXW.
035: *
036: * @author Kohsuke Kawaguchi
037: */
038: public abstract class TXW {
039: private TXW() {
040: } // no instanciation please
041:
042: /*package*/static QName getTagName(Class<?> c) {
043: String localName = "";
044: String nsUri = "##default";
045:
046: XmlElement xe = c.getAnnotation(XmlElement.class);
047: if (xe != null) {
048: localName = xe.value();
049: nsUri = xe.ns();
050: }
051:
052: if (localName.length() == 0) {
053: localName = c.getName();
054: int idx = localName.lastIndexOf('.');
055: if (idx >= 0)
056: localName = localName.substring(idx + 1);
057:
058: localName = Character.toLowerCase(localName.charAt(0))
059: + localName.substring(1);
060: }
061:
062: if (nsUri.equals("##default")) {
063: Package pkg = c.getPackage();
064: if (pkg != null) {
065: XmlNamespace xn = pkg.getAnnotation(XmlNamespace.class);
066: if (xn != null)
067: nsUri = xn.value();
068: }
069: }
070: if (nsUri.equals("##default"))
071: nsUri = "";
072:
073: return new QName(nsUri, localName);
074: }
075:
076: /**
077: * Creates a new {@link TypedXmlWriter} to write a new instance of a document.
078: *
079: * @param rootElement
080: * The {@link TypedXmlWriter} interface that declares the content model of the root element.
081: * This interface must have {@link XmlElement} annotation on it to designate the tag name
082: * of the root element.
083: * @param out
084: * The target of the writing.
085: */
086: public static <T extends TypedXmlWriter> T create(
087: Class<T> rootElement, XmlSerializer out) {
088: Document doc = new Document(out);
089: QName n = getTagName(rootElement);
090: return new ContainerElement(doc, null, n.getNamespaceURI(), n
091: .getLocalPart())._cast(rootElement);
092: }
093:
094: /**
095: * Creates a new {@link TypedXmlWriter} to write a new instance of a document.
096: *
097: * <p>
098: * Similar to the other method, but this version allows the caller to set the
099: * tag name at the run-time.
100: *
101: * @param tagName
102: * The tag name of the root document.
103: *
104: * @see #create(Class,XmlSerializer)
105: */
106: public static <T extends TypedXmlWriter> T create(QName tagName,
107: Class<T> rootElement, XmlSerializer out) {
108: return new ContainerElement(new Document(out), null, tagName
109: .getNamespaceURI(), tagName.getLocalPart())
110: ._cast(rootElement);
111: }
112: }
|