Search in sources :

Example 1 with XmlElement

use of com.sun.xml.txw2.annotation.XmlElement in project jaxb-ri by eclipse-ee4j.

the class TXW method getTagName.

/*package*/
static QName getTagName(Class<?> c) {
    String localName = "";
    String nsUri = "##default";
    XmlElement xe = c.getAnnotation(XmlElement.class);
    if (xe != null) {
        localName = xe.value();
        nsUri = xe.ns();
    }
    if (localName.length() == 0) {
        localName = c.getName();
        int idx = localName.lastIndexOf('.');
        if (idx >= 0)
            localName = localName.substring(idx + 1);
        localName = Character.toLowerCase(localName.charAt(0)) + localName.substring(1);
    }
    if (nsUri.equals("##default")) {
        Package pkg = c.getPackage();
        if (pkg != null) {
            XmlNamespace xn = pkg.getAnnotation(XmlNamespace.class);
            if (xn != null)
                nsUri = xn.value();
        }
    }
    if (nsUri.equals("##default"))
        nsUri = "";
    return new QName(nsUri, localName);
}
Also used : XmlNamespace(com.sun.xml.txw2.annotation.XmlNamespace) QName(javax.xml.namespace.QName) XmlElement(com.sun.xml.txw2.annotation.XmlElement)

Example 2 with XmlElement

use of com.sun.xml.txw2.annotation.XmlElement in project jaxb-ri by eclipse-ee4j.

the class TXW method create.

/**
 * Creates a new {@link TypedXmlWriter} to write a new instance of a document.
 *
 * @param <T> an instance of {@link TypedXmlWriter}
 * @param rootElement
 *      The {@link TypedXmlWriter} interface that declares the content model of the root element.
 *      This interface must have {@link XmlElement} annotation on it to designate the tag name
 *      of the root element.
 * @param out
 *      The target of the writing.
 * @return
 *      always return non-null {@link TypedXmlWriter} that can be used
 *      to write the contents of the root element.
 */
public static <T extends TypedXmlWriter> T create(Class<T> rootElement, XmlSerializer out) {
    if (out instanceof TXWSerializer) {
        TXWSerializer txws = (TXWSerializer) out;
        return txws.txw._element(rootElement);
    }
    Document doc = new Document(out);
    QName n = getTagName(rootElement);
    return new ContainerElement(doc, null, n.getNamespaceURI(), n.getLocalPart())._cast(rootElement);
}
Also used : TXWSerializer(com.sun.xml.txw2.output.TXWSerializer) QName(javax.xml.namespace.QName)

Example 3 with XmlElement

use of com.sun.xml.txw2.annotation.XmlElement in project jaxb-ri by eclipse-ee4j.

the class ContainerElement method addElement.

/**
 * Writes a new element.
 */
@SuppressWarnings("unchecked")
private Object addElement(XmlElement e, Method method, Object[] args) {
    Class<?> rt = method.getReturnType();
    // the last precedence: default name
    String nsUri = "##default";
    String localName = method.getName();
    if (e != null) {
        // then the annotation on this method
        if (e.value().length() != 0)
            localName = e.value();
        nsUri = e.ns();
    }
    if (nsUri.equals("##default")) {
        // look for the annotation on the declaring class
        Class<?> c = method.getDeclaringClass();
        XmlElement ce = c.getAnnotation(XmlElement.class);
        if (ce != null) {
            nsUri = ce.ns();
        }
        if (nsUri.equals("##default"))
            // then default to the XmlNamespace
            nsUri = getNamespace(c.getPackage());
    }
    if (rt == Void.TYPE) {
        // leaf element with just a value
        boolean isCDATA = method.getAnnotation(XmlCDATA.class) != null;
        StartTag st = new StartTag(document, nsUri, localName);
        addChild(st);
        for (Object arg : args) {
            Text text;
            if (isCDATA)
                text = new Cdata(document, st, arg);
            else
                text = new Pcdata(document, st, arg);
            addChild(text);
        }
        addChild(new EndTag());
        return null;
    }
    if (TypedXmlWriter.class.isAssignableFrom(rt)) {
        // sub writer
        return _element(nsUri, localName, (Class<? extends TypedXmlWriter>) rt);
    }
    throw new IllegalSignatureException("Illegal return type: " + rt);
}
Also used : XmlElement(com.sun.xml.txw2.annotation.XmlElement) XmlCDATA(com.sun.xml.txw2.annotation.XmlCDATA)

Example 4 with XmlElement

use of com.sun.xml.txw2.annotation.XmlElement in project jaxb-ri by eclipse-ee4j.

the class ContainerElement method invoke.

@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    if (method.getDeclaringClass() == TypedXmlWriter.class || method.getDeclaringClass() == Object.class) {
        // forward to myself
        try {
            return method.invoke(this, args);
        } catch (InvocationTargetException e) {
            throw e.getTargetException();
        }
    }
    XmlAttribute xa = method.getAnnotation(XmlAttribute.class);
    XmlValue xv = method.getAnnotation(XmlValue.class);
    XmlElement xe = method.getAnnotation(XmlElement.class);
    if (xa != null) {
        if (xv != null || xe != null)
            throw new IllegalAnnotationException(method.toString());
        addAttribute(xa, method, args);
        // allow method chaining
        return proxy;
    }
    if (xv != null) {
        if (xe != null)
            throw new IllegalAnnotationException(method.toString());
        _pcdata(args);
        // allow method chaining
        return proxy;
    }
    return addElement(xe, method, args);
}
Also used : XmlAttribute(com.sun.xml.txw2.annotation.XmlAttribute) XmlValue(com.sun.xml.txw2.annotation.XmlValue) XmlElement(com.sun.xml.txw2.annotation.XmlElement) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

XmlElement (com.sun.xml.txw2.annotation.XmlElement)3 QName (javax.xml.namespace.QName)2 XmlAttribute (com.sun.xml.txw2.annotation.XmlAttribute)1 XmlCDATA (com.sun.xml.txw2.annotation.XmlCDATA)1 XmlNamespace (com.sun.xml.txw2.annotation.XmlNamespace)1 XmlValue (com.sun.xml.txw2.annotation.XmlValue)1 TXWSerializer (com.sun.xml.txw2.output.TXWSerializer)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1