jaxws/src/share/jaxws_classes/com/sun/xml/internal/txw2/TXW.java
changeset 12009 4abb694f273a
child 22678 ac1ea46be942
equal deleted inserted replaced
11943:16ba58282d11 12009:4abb694f273a
       
     1 /*
       
     2  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 
       
    26 package com.sun.xml.internal.txw2;
       
    27 
       
    28 import com.sun.xml.internal.txw2.output.XmlSerializer;
       
    29 import com.sun.xml.internal.txw2.output.TXWSerializer;
       
    30 import com.sun.xml.internal.txw2.annotation.XmlElement;
       
    31 import com.sun.xml.internal.txw2.annotation.XmlNamespace;
       
    32 
       
    33 import javax.xml.namespace.QName;
       
    34 
       
    35 /**
       
    36  * Entry point to TXW.
       
    37  *
       
    38  * @author Kohsuke Kawaguchi
       
    39  */
       
    40 public abstract class TXW {
       
    41     private TXW() {}    // no instanciation please
       
    42 
       
    43 
       
    44     /*package*/ static QName getTagName( Class<?> c ) {
       
    45         String localName="";
       
    46         String nsUri="##default";
       
    47 
       
    48         XmlElement xe = c.getAnnotation(XmlElement.class);
       
    49         if(xe!=null) {
       
    50             localName = xe.value();
       
    51             nsUri = xe.ns();
       
    52         }
       
    53 
       
    54         if(localName.length()==0) {
       
    55             localName = c.getName();
       
    56             int idx = localName.lastIndexOf('.');
       
    57             if(idx>=0)
       
    58                 localName = localName.substring(idx+1);
       
    59 
       
    60             localName = Character.toLowerCase(localName.charAt(0))+localName.substring(1);
       
    61         }
       
    62 
       
    63         if(nsUri.equals("##default")) {
       
    64             Package pkg = c.getPackage();
       
    65             if(pkg!=null) {
       
    66                 XmlNamespace xn = pkg.getAnnotation(XmlNamespace.class);
       
    67                 if(xn!=null)
       
    68                     nsUri = xn.value();
       
    69             }
       
    70         }
       
    71         if(nsUri.equals("##default"))
       
    72             nsUri = "";
       
    73 
       
    74         return new QName(nsUri,localName);
       
    75     }
       
    76 
       
    77     /**
       
    78      * Creates a new {@link TypedXmlWriter} to write a new instance of a document.
       
    79      *
       
    80      * @param rootElement
       
    81      *      The {@link TypedXmlWriter} interface that declares the content model of the root element.
       
    82      *      This interface must have {@link XmlElement} annotation on it to designate the tag name
       
    83      *      of the root element.
       
    84      * @param out
       
    85      *      The target of the writing.
       
    86      */
       
    87     public static <T extends TypedXmlWriter> T create( Class<T> rootElement, XmlSerializer out ) {
       
    88         if (out instanceof TXWSerializer) {
       
    89             TXWSerializer txws = (TXWSerializer) out;
       
    90             return txws.txw._element(rootElement);
       
    91         }
       
    92 
       
    93         Document doc = new Document(out);
       
    94         QName n = getTagName(rootElement);
       
    95         return new ContainerElement(doc,null,n.getNamespaceURI(),n.getLocalPart())._cast(rootElement);
       
    96     }
       
    97 
       
    98     /**
       
    99      * Creates a new {@link TypedXmlWriter} to write a new instance of a document.
       
   100      *
       
   101      * <p>
       
   102      * Similar to the other method, but this version allows the caller to set the
       
   103      * tag name at the run-time.
       
   104      *
       
   105      * @param tagName
       
   106      *      The tag name of the root document.
       
   107      *
       
   108      * @see #create(Class,XmlSerializer)
       
   109      */
       
   110     public static <T extends TypedXmlWriter> T create( QName tagName, Class<T> rootElement, XmlSerializer out ) {
       
   111         if (out instanceof TXWSerializer) {
       
   112             TXWSerializer txws = (TXWSerializer) out;
       
   113             return txws.txw._element(tagName,rootElement);
       
   114         }
       
   115         return new ContainerElement(new Document(out),null,tagName.getNamespaceURI(),tagName.getLocalPart())._cast(rootElement);
       
   116     }
       
   117 }