Example usage for javax.xml.bind JAXBElement JAXBElement

List of usage examples for javax.xml.bind JAXBElement JAXBElement

Introduction

In this page you can find the example usage for javax.xml.bind JAXBElement JAXBElement.

Prototype

public JAXBElement(QName name, Class<T> declaredType, T value) 

Source Link

Document

Construct an xml element instance.

Usage

From source file:com.evolveum.midpoint.testing.wstest.AbstractWebserviceTest.java

protected static <T> JAXBElement<T> toJaxbElement(QName name, T value) {
    return new JAXBElement<T>(name, (Class<T>) value.getClass(), value);
}

From source file:com.evolveum.midpoint.prism.util.JaxbTestUtil.java

public <T> Element marshalObjectToDom(T jaxbObject, QName elementQName, Document doc) throws JAXBException {
    if (doc == null) {
        doc = DOMUtil.getDocument();//  ww  w .  ja  v  a  2s.  c o m
    }

    JAXBElement<T> jaxbElement = new JAXBElement<T>(elementQName, (Class<T>) jaxbObject.getClass(), jaxbObject);
    Element element = doc.createElementNS(elementQName.getNamespaceURI(), elementQName.getLocalPart());
    marshalElementToDom(jaxbElement, element);

    return (Element) element.getFirstChild();
}

From source file:com.evolveum.midpoint.prism.util.JaxbTestUtil.java

public <T> void marshalObjectToDom(T jaxbObject, QName elementQName, Element parentElement)
        throws JAXBException {

    JAXBElement<T> jaxbElement = new JAXBElement<T>(elementQName, (Class<T>) jaxbObject.getClass(), jaxbObject);
    marshalElementToDom(jaxbElement, parentElement);
}

From source file:com.evolveum.midpoint.model.client.ModelClientUtil.java

public static <O extends ObjectType> String marshallToSting(O object, boolean formatted) throws JAXBException {
    Marshaller marshaller = jaxbContext.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
    if (formatted) {
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    }//  w ww.  j av a2  s. c om
    java.io.StringWriter sw = new StringWriter();
    Class<O> type = (Class<O>) object.getClass();
    JAXBElement<O> element = new JAXBElement<O>(getElementName(type), type, object);
    marshaller.marshal(element, sw);
    return sw.toString();
}

From source file:com.evolveum.midpoint.prism.parser.JaxbDomHack.java

/**
 * Serializes prism value to JAXB "any" format as returned by JAXB getAny() methods. 
 *///  ww  w.j a va2 s.  c  o m
public Object toAny(PrismValue value) throws SchemaException {
    Document document = DOMUtil.getDocument();
    if (value == null) {
        return value;
    }
    QName elementName = value.getParent().getElementName();
    Object xmlValue;
    if (value instanceof PrismPropertyValue) {
        PrismPropertyValue<Object> pval = (PrismPropertyValue) value;
        if (pval.isRaw() && (pval.getParent() == null || pval.getParent().getDefinition() == null)) {
            Object rawElement = pval.getRawElement();
            if (rawElement instanceof Element) {
                return ((Element) rawElement).cloneNode(true);
            } else if (rawElement instanceof MapXNode) {
                return domParser.serializeXMapToElement((MapXNode) rawElement, elementName);
            } else if (rawElement instanceof PrimitiveXNode<?>) {
                PrimitiveXNode<?> xprim = (PrimitiveXNode<?>) rawElement;
                String stringValue = xprim.getStringValue();
                Element element = DOMUtil.createElement(document, elementName);
                element.setTextContent(stringValue);
                DOMUtil.setNamespaceDeclarations(element, xprim.getRelevantNamespaceDeclarations());
                return element;
            } else {
                throw new IllegalArgumentException("Cannot convert raw element " + rawElement + " to xsd:any");
            }
        }
        Object realValue = pval.getValue();
        xmlValue = realValue;
        if (XmlTypeConverter.canConvert(realValue.getClass())) {
            // Always record xsi:type. This is FIXME, but should work OK for now (until we put definition into deltas)
            xmlValue = XmlTypeConverter.toXsdElement(realValue, elementName, document, true);
        }
    } else if (value instanceof PrismReferenceValue) {
        PrismReferenceValue rval = (PrismReferenceValue) value;
        xmlValue = prismContext.serializeValueToDom(rval, elementName, document);
    } else if (value instanceof PrismContainerValue<?>) {
        PrismContainerValue<?> pval = (PrismContainerValue<?>) value;
        if (pval.getParent().getCompileTimeClass() == null) {
            // This has to be runtime schema without a compile-time representation.
            // We need to convert it to DOM
            xmlValue = prismContext.serializeValueToDom(pval, elementName, document);
        } else {
            xmlValue = pval.asContainerable();
        }
    } else {
        throw new IllegalArgumentException("Unknown type " + value);
    }
    if (!(xmlValue instanceof Element) && !(xmlValue instanceof JAXBElement)) {
        xmlValue = new JAXBElement(elementName, xmlValue.getClass(), xmlValue);
    }
    return xmlValue;
}

From source file:com.evolveum.midpoint.model.client.ModelClientUtil.java

public static <T> String marshallToSting(QName elementName, T object, boolean formatted) throws JAXBException {
    Marshaller marshaller = jaxbContext.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
    if (formatted) {
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    }//from   w w  w  .  j a  v a  2 s.  c o m
    java.io.StringWriter sw = new StringWriter();
    JAXBElement<T> element = new JAXBElement<T>(elementName, (Class<T>) object.getClass(), object);
    marshaller.marshal(element, sw);
    return sw.toString();
}

From source file:com.evolveum.midpoint.web.util.ExpressionUtil.java

public static void createShadowRefEvaluatorValue(ExpressionType expression, String oid,
        PrismContext prismContext) {//w  w  w. ja v a 2s .  co  m
    if (expression == null) {
        expression = new ExpressionType();
    }
    JAXBElement element = new JAXBElement(SchemaConstants.C_VALUE, RawType.class, new RawType(prismContext));

    MapXNode shadowRefNode = new MapXNode();
    shadowRefNode.put(SHADOW_OID_KEY, new PrimitiveXNode<>(oid));
    shadowRefNode.put(SHADOW_TYPE_KEY, new PrimitiveXNode<>(ShadowType.COMPLEX_TYPE.getLocalPart()));

    MapXNode valueNode = new MapXNode();
    valueNode.put(SHADOW_REF_KEY, shadowRefNode);

    RawType expressionValue = new RawType(valueNode, prismContext);
    element.setValue(expressionValue);

    removeEvaluatorByName(expression, SchemaConstants.C_VALUE);
    expression.getExpressionEvaluator().add(element);
}

From source file:edu.duke.cabig.c3pr.webservice.helpers.WebServiceRelatedTestCase.java

public static void main(String[] args) throws JAXBException {
    JAXBContext jc = JAXBContext.newInstance(StudyProtocolVersion.class, CD.class);
    Marshaller m = jc.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    final StudyProtocolVersion study = new StudyUtilityImplTest().createStudy();
    JAXBElement<StudyProtocolVersion> jaxbElement = new JAXBElement<StudyProtocolVersion>(
            new QName("http://enterpriseservices.nci.nih.gov/Common", "study"), StudyProtocolVersion.class,
            study);/* w ww . j a v  a 2s. c  o m*/
    m.marshal(jaxbElement, System.out);
}

From source file:net.morphbank.mbsvc3.xml.XmlBaseObject.java

/**
 * Method to add a Darwin core field of type string to the xsi:any tag of
 * the specimen/*www . ja  v a 2  s  .co m*/
 * 
 * @param namespace
 * @param tagName
 * @param value
 */
public void addDarwinTag(QName tag, String value) {
    // TODO resolve namespace for various fields
    // strategy for adding a field that represents a Darwin Core attribute
    if (value != null && value.length() > 0) {
        JAXBElement<String> node = new JAXBElement<String>(tag, String.class, value);
        getAny().add(node);
    }
}

From source file:com.evolveum.midpoint.web.util.ExpressionUtil.java

public static void updateLiteralExpressionValue(ExpressionType expression, List<String> values,
        PrismContext prismContext) {/*from  w  w w.ja  v a  2s  .c  om*/
    if (expression == null) {
        expression = new ExpressionType();
    }
    removeEvaluatorByName(expression, SchemaConstantsGenerated.C_VALUE);
    for (String value : values) {
        PrimitiveXNode<String> newValueNode = new PrimitiveXNode<>(value);
        RawType raw = new RawType(newValueNode, prismContext);
        JAXBElement element = new JAXBElement(SchemaConstantsGenerated.C_VALUE, RawType.class, raw);
        expression.expressionEvaluator(element);
    }
}