List of usage examples for javax.xml.bind JAXBElement JAXBElement
public JAXBElement(QName name, Class<T> declaredType, T value)
From source file:io.github.jeddict.jpa.modeler.initializer.JPAModelerUtil.java
public <T extends Object> T cloneElement(T element) { T clonedElement = null;//from w w w .j a va 2 s . c o m try { if (MODELER_MARSHALLER == null) { MODELER_MARSHALLER = MODELER_CONTEXT.createMarshaller(); MODELER_MARSHALLER.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); MODELER_MARSHALLER.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, "http://java.sun.com/xml/ns/persistence/orm orm_2_1.xsd"); MODELER_MARSHALLER.setEventHandler(new ValidateJAXB()); } StringWriter sw = new StringWriter(); QName qName = new QName(element.getClass().getSimpleName()); JAXBElement<T> root = new JAXBElement<>(qName, (Class<T>) element.getClass(), (T) element); MODELER_MARSHALLER.marshal(root, sw); if (MODELER_UNMARSHALLER == null) { MODELER_UNMARSHALLER = MODELER_CONTEXT.createUnmarshaller(); } StringReader reader = new StringReader(sw.toString()); clonedElement = (T) MODELER_UNMARSHALLER .unmarshal(new StreamSource(reader), (Class<T>) element.getClass()).getValue(); MODELER_UNMARSHALLER = null; } catch (JAXBException ex) { ExceptionUtils.printStackTrace(ex); } return clonedElement; }
From source file:org.apache.cxf.systest.jaxrs.JAXRSClientServerBookTest.java
@Test public void testEchoBookElement() throws Exception { BookStore store = JAXRSClientFactory.create("http://localhost:" + PORT, BookStore.class); JAXBElement<Book> element = store .echoBookElement(new JAXBElement<Book>(new QName("", "Book"), Book.class, new Book("CXF", 123L))); Book book = element.getValue(); assertEquals(123L, book.getId());//from ww w . ja va2 s . c o m assertEquals("CXF", book.getName()); }
From source file:org.apache.cxf.systest.jaxrs.JAXRSClientServerBookTest.java
@Test public void testEchoBookElementWildcard() throws Exception { BookStore store = JAXRSClientFactory.create("http://localhost:" + PORT, BookStore.class); JAXBElement<? super Book> element = store.echoBookElementWildcard( new JAXBElement<Book>(new QName("", "Book"), Book.class, new Book("CXF", 123L))); Book book = (Book) element.getValue(); assertEquals(123L, book.getId());/*from w ww . jav a 2 s . c om*/ assertEquals("CXF", book.getName()); }
From source file:nl.b3p.viewer.stripes.ArcQueryUtilActionBean.java
@DefaultHandler public Resolution arcXML() throws JSONException { JSONObject json = new JSONObject(); try {//from w ww .j a va 2 s. c o m AxlSpatialQuery aq = new AxlSpatialQuery(); FilterToArcXMLSQL visitor = new FilterToArcXMLSQL(aq); Filter filter = CQL.toFilter(cql); String where = visitor.encodeToString(filter); if (whereOnly) { json.put("where", where); } else { if (where.trim().length() > 0 && !where.trim().equals("1=1")) { aq.setWhere(where); } StringWriter sw = new StringWriter(); Marshaller m = ArcXML.getJaxbContext().createMarshaller(); m.setProperty(javax.xml.bind.Marshaller.JAXB_ENCODING, "UTF-8"); m.setProperty(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); m.setProperty(javax.xml.bind.Marshaller.JAXB_FRAGMENT, Boolean.TRUE); m.marshal(new JAXBElement(new QName(null, "SPATIALQUERY"), AxlSpatialQuery.class, aq), sw); sw.close(); json.put("SPATIALQUERY", sw.toString()); } json.put("success", true); } catch (Exception e) { json.put("success", false); String message = "Fout bij maken spatial query: " + e.toString(); Throwable cause = e.getCause(); while (cause != null) { message += "; " + cause.toString(); cause = cause.getCause(); } json.put("error", message); } return new StreamingResolution("application/json", new StringReader(json.toString(4))); }
From source file:nl.ordina.bag.etl.xml.XMLMessageBuilder.java
@SuppressWarnings("unchecked") public static <T> T deepCopy(T object) throws JAXBException { if (object == null) return null; JAXBContext jaxbContext = JAXBContext.newInstance(object.getClass()); JAXBElement<T> contentObject = new JAXBElement<T>(new QName(object.getClass().getSimpleName()), (Class<T>) object.getClass(), object); JAXBSource source = new JAXBSource(jaxbContext, contentObject); return jaxbContext.createUnmarshaller().unmarshal(source, (Class<T>) object.getClass()).getValue(); }
From source file:org.accada.epcis.repository.query.QueryOperationsBackendSQL.java
/** * Fetches the qualified XML elements representing extensions for event * fields from the given result set and populates the given List. * //from w w w . ja v a2 s .c o m * @param rs * The result of the SQL query. * @throws SQLException * If a database access error occurred. */ private void readExtensionsFromResult(final ResultSet rs, final List<Object> extensions) throws SQLException { while (rs.next()) { String fieldname = rs.getString(1); String[] parts = fieldname.split("#"); if (parts.length != 2) { throw new SQLException( "Fieldname extension has invalid format: required 'namespace#localname' but was " + fieldname); } String namespace = parts[0]; String localPart = parts[1]; String prefix = rs.getString(2); String value = rs.getString(3); if (value == null) { value = rs.getString(4); if (value == null) { value = rs.getString(5); if (value == null) { value = rs.getString(6); if (value == null) { throw new SQLException("No valid extension value found"); } } } } JAXBElement<String> elem = new JAXBElement<String>(new QName(namespace, localPart, prefix), String.class, value); extensions.add(elem); } }
From source file:org.apache.axis2.datasource.jaxb.JAXBDSContext.java
private static Object unmarshalArray(final XMLStreamReader reader, final Unmarshaller u, Class type) throws Exception { try {//from w w w . ja v a 2 s .c om if (DEBUG_ENABLED) { log.debug("Invoking unmarshalArray"); } Object jaxb = AccessController.doPrivileged(new PrivilegedAction() { public Object run() { try { return u.unmarshal(reader, String[].class); } catch (OMException e) { throw e; } catch (Throwable t) { throw new OMException(t); } } }); Object typeObj = getTypeEnabledObject(jaxb); // Now convert String Array in to the required Type Array. if (typeObj instanceof String[]) { String[] strArray = (String[]) typeObj; Object obj = XSDListUtils.fromStringArray(strArray, type); QName qName = XMLRootElementUtil.getXmlRootElementQNameFromObject(jaxb); jaxb = new JAXBElement(qName, type, obj); } return jaxb; } catch (OMException e) { throw e; } catch (Throwable t) { throw new OMException(t); } }
From source file:org.apache.axis2.datasource.jaxb.JAXBDSContext.java
/** * convert the String into a list or array * @param <T>/*from w ww.ja v a 2 s .c o m*/ * @param jaxb * @param type * @return * @throws IllegalAccessException * @throws ParseException * @throws NoSuchMethodException * @throws InstantiationException * @throws DatatypeConfigurationException * @throws InvocationTargetException */ public static Object unmarshalAsListOrArray(final XMLStreamReader reader, final Unmarshaller u, Class type) throws IllegalAccessException, ParseException, NoSuchMethodException, InstantiationException, DatatypeConfigurationException, InvocationTargetException, JAXBException { if (DEBUG_ENABLED) { log.debug("Invoking unmarshalAsListOrArray"); } // If this is an xsd:list, we need to return the appropriate // list or array (see NOTE above) // First unmarshal as a String Object jaxb = null; try { jaxb = AccessController.doPrivileged(new PrivilegedAction() { public Object run() { try { return u.unmarshal(reader, String.class); } catch (OMException e) { throw e; } catch (Throwable t) { throw new OMException(t); } } }); } catch (OMException e) { throw e; } catch (Throwable t) { throw new OMException(t); } //Second convert the String into a list or array if (getTypeEnabledObject(jaxb) instanceof String) { QName qName = XMLRootElementUtil.getXmlRootElementQNameFromObject(jaxb); Object obj = XSDListUtils.fromXSDListString((String) getTypeEnabledObject(jaxb), type); return new JAXBElement(qName, type, obj); } else { return jaxb; } }
From source file:org.apache.axis2.datasource.jaxb.JAXBDSContext.java
/** * Marshal objects by type// w w w . j av a 2 s .co m * * @param b Object that can be rendered as an element, but the element name is not known to the * schema (i.e. rpc) * @param m Marshaller * @param writer XMLStreamWriter * @param type Class * @param isList true if this is an XmlList * @param ctype CONSTRUCTION_TYPE * @param optimize boolean set to true if optimization directly to * outputstream should be attempted. */ private void marshalByType(final Object b, final Marshaller m, final XMLStreamWriter writer, final Class type, final boolean isList, final JAXBUtils.CONSTRUCTION_TYPE ctype, final boolean optimize) throws WebServiceException { if (log.isDebugEnabled()) { log.debug("Enter marshalByType b=" + getDebugName(b) + " type=" + type + " marshaller=" + m + " writer=" + writer + " isList=" + isList + " ctype=" + ctype + " optimize=" + optimize); } if (isOccurrenceArray(b)) { marshalOccurrenceArray((JAXBElement) b, m, writer); return; } AccessController.doPrivileged(new PrivilegedAction() { public Object run() { try { // NOTE // Example: // <xsd:simpleType name="LongList"> // <xsd:list> // <xsd:simpleType> // <xsd:restriction base="xsd:unsignedInt"/> // </xsd:simpleType> // </xsd:list> // </xsd:simpleType> // <element name="myLong" nillable="true" type="impl:LongList"/> // // LongList will be represented as an int[] // On the wire myLong will be represented as a list of integers // with intervening whitespace // <myLong>1 2 3</myLong> // // Unfortunately, we are trying to marshal by type. Therefore // we want to marshal an element (foo) that is unknown to schema. // If we use the normal marshal code, the wire will look like // this (which is incorrect): // <foo><item>1</item><item>2</item><item>3</item></foo> // // The solution is to detect this situation and marshal the // String instead. Then we get the correct wire format: // <foo>1 2 3</foo> Object jbo = b; if (DEBUG_ENABLED) { log.debug("check if marshalling list or array object, type = " + ((b != null) ? b.getClass().getName() : "null")); } if (isList) { if (DEBUG_ENABLED) { log.debug("marshalling type which is a List"); } // This code assumes that the JAXBContext does not understand // the array or list. In such cases, the contents are converted // to a String and passed directly. if (ctype == JAXBUtils.CONSTRUCTION_TYPE.BY_CONTEXT_PATH) { QName qName = XMLRootElementUtil.getXmlRootElementQNameFromObject(b); String text = XSDListUtils.toXSDListString(getTypeEnabledObject(b)); if (DEBUG_ENABLED) { log.debug("marshalling [context path approach] " + "with xmllist text = " + text); } jbo = new JAXBElement(qName, String.class, text); } else if (ctype == JAXBUtils.CONSTRUCTION_TYPE.BY_CLASS_ARRAY) { // Some versions of JAXB have array/list processing built in. // This code is a safeguard because apparently some versions // of JAXB don't. QName qName = XMLRootElementUtil.getXmlRootElementQNameFromObject(b); String text = XSDListUtils.toXSDListString(getTypeEnabledObject(b)); if (DEBUG_ENABLED) { log.debug("marshalling [class array approach] " + "with xmllist text = " + text); } jbo = new JAXBElement(qName, String.class, text); } } // When JAXBContext is created using a context path, it will not include Enum // classes. // These classes have @XmlEnum annotation but not @XmlType/@XmlElement, so the // user will see MarshallingEx, class not known to ctxt. // // This is a jax-b defect, for now this fix is in place to pass CTS. This only // fixes the // situation where the enum is the top-level object (e.g., message-part in // rpc-lit scenario) // // Sample of what enum looks like: // @XmlEnum public enum EnumString { // @XmlEnumValue("String1") STRING_1("String1"), // @XmlEnumValue("String2") STRING_2("String2"); // ... } if (type.isEnum()) { if (b != null) { if (DEBUG_ENABLED) { log.debug("marshalByType. Marshaling " + type.getName() + " as Enum"); } JAXBElement jbe = (JAXBElement) b; String value = XMLRootElementUtil.getEnumValue((Enum) jbe.getValue()); jbo = new JAXBElement(jbe.getName(), String.class, value); } } // If the output stream is available, marshal directly to it OutputStream os = (optimize) ? getOutputStream(writer, m) : null; if (os == null) { if (DEBUG_ENABLED) { log.debug("Invoking marshalByType. " + "Marshaling to an XMLStreamWriter. Object is " + getDebugName(jbo)); } m.marshal(jbo, writer); } else { if (DEBUG_ENABLED) { log.debug("Invoking marshalByType. " + "Marshaling to an OutputStream. Object is " + getDebugName(jbo)); } m.marshal(jbo, os); } } catch (OMException e) { throw e; } catch (Throwable t) { throw new OMException(t); } return null; } }); }
From source file:org.apache.axis2.datasource.jaxb.JAXBDSContext.java
/** * Marshal array objects by type/*from w w w .j a v a 2 s .c o m*/ * * Invoke marshalByType for each element in the array * * @param jaxb_in JAXBElement containing a value that is a List or array * @param m_in Marshaller * @param writer_in XMLStreamWriter */ private void marshalOccurrenceArray(final JAXBElement jbe_in, final Marshaller m_in, final XMLStreamWriter writer_in) { if (log.isDebugEnabled()) { log.debug("Enter marshalOccurrenceArray"); log.debug(" Marshaller = " + JavaUtils.getObjectIdentity(m_in)); } AccessController.doPrivileged(new PrivilegedAction() { public Object run() { try { Marshaller m = m_in; JAXBContext newJBC = null; if (getConstructionType() != JAXBUtils.CONSTRUCTION_TYPE.BY_CLASS_ARRAY_PLUS_ARRAYS) { // Rebuild JAXBContext // There may be a preferred classloader that should be used if (log.isDebugEnabled()) { log.debug("Building a JAXBContext with array capability"); } ClassLoader cl = getClassLoader(); newJBC = getJAXBContext(cl, true); m = JAXBUtils.getJAXBMarshaller(newJBC); if (log.isDebugEnabled()) { log.debug("The new JAXBContext was constructed with " + getConstructionType()); } } OccurrenceArray occurArray = (OccurrenceArray) jbe_in.getValue(); // Create a new JAXBElement. // The name is the name of the individual occurence elements // Type type is Object[] // The value is the array of Object[] representing each element JAXBElement jbe = new JAXBElement(jbe_in.getName(), Object[].class, occurArray.getAsArray()); // The jaxb marshal command cannot write out a list/array // of occurence elements. So we marshal it as a single // element containing items...and then put a filter on the // writer to transform it into a stream of occurence elements XMLStreamWriterArrayFilter writer = new XMLStreamWriterArrayFilter(writer_in); m.marshal(jbe, writer); if (newJBC != null) { JAXBUtils.releaseJAXBMarshaller(newJBC, m); } return null; } catch (OMException e) { throw e; } catch (Throwable t) { throw new OMException(t); } } }); if (log.isDebugEnabled()) { log.debug("Exit marshalOccurrenceArray"); } }