Java Code Examples for javax.xml.bind.JAXBElement#getName()

The following examples show how to use javax.xml.bind.JAXBElement#getName() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MapperUtils.java    From proarc with GNU General Public License v3.0 5 votes vote down vote up
public static <T> List<T> find(List<JAXBElement<?>> elms, Class<T> type, QName name) {
    List<T> result = new ArrayList<>();
    for (JAXBElement<?> elm : elms) {
        QName elmName = elm.getName();
        if (elmName.equals(name)) {
            result.add(type.cast(elm.getValue()));
        }
    }
    return result;
}
 
Example 2
Source File: SimpleGMLParser.java    From importer-exporter with Apache License 2.0 5 votes vote down vote up
public GeometryObject parseGeometry(JAXBElement<?> geometry) throws GeometryParseException, SrsParseException {
	try {
		ModelObject object = unmarshaller.unmarshal(geometry);
		if (object instanceof GML)
			return parseGeometry((GML)object, geometry.getName());
	} catch (MissingADESchemaException e) {
		throw new GeometryParseException("Failed to parse GML geometry.", e);
	}

	throw new GeometryParseException("Failed to parse the geometry element '" + geometry.getName() + "'.");
}
 
Example 3
Source File: ReferenceParametersAssertingHandler.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Override
public boolean handleMessage(SOAPMessageContext context) {
    if ((Boolean)context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY)) {
        return true;
    }
    if (params == null) {
        return true;
    }
    try {
        // every element in the ReferenceParametersType should be present somewhere in the headers
        for (Object exp : params.getAny()) {
            JAXBElement<?> expectedElement = (JAXBElement<?>)exp;
            boolean found = false;
            Element actualHeaderelement = DOMUtils.getFirstElement(context.getMessage().getSOAPHeader());
            while (actualHeaderelement != null) {
                if (expectedElement.getName().getLocalPart().equals(actualHeaderelement.getLocalName())
                        && expectedElement.getName().getNamespaceURI()
                        .equals(actualHeaderelement.getNamespaceURI())) {
                    found = true;
                    break;
                }
                actualHeaderelement = DOMUtils.getNextElement(actualHeaderelement);
            }
            if (!found) {
                throw new RuntimeException("Event sink should have received Reference parameter: "
                    + expectedElement.getName());
            }
        }
    } catch (SOAPException e) {
        throw new RuntimeException(e);
    }
    return true;
}
 
Example 4
Source File: X509Utils.java    From cxf with Apache License 2.0 5 votes vote down vote up
private static X509Certificate parseX509Item(JAXBElement<?> x509Item) throws CertificateException {
    QName x509ItemName = x509Item.getName();
    if (X509_CERTIFICATE.equals(x509ItemName)) {
        X509Certificate certificate;
        certificate = extractCertificate(x509Item);
        LOG.fine("Extracted " + certificate.getSubjectX500Principal().getName());
        return certificate;
    }
    return null;
}
 
Example 5
Source File: WSDiscoveryClient.java    From cxf with Apache License 2.0 5 votes vote down vote up
private void addAddressing(BindingProvider p, boolean addSeq, String action) {
    AddressingProperties addrProperties = new AddressingProperties();
    if (action != null) {
        AttributedURIType act = new AttributedURIType();
        act.setValue(action);
        addrProperties.setAction(act);
    }
    if (adHoc) {
        EndpointReferenceType to = new EndpointReferenceType();
        addrProperties.exposeAs(version.getAddressingNamespace());
        AttributedURIType epr = new AttributedURIType();
        epr.setValue(version.getToAddress());
        to.setAddress(epr);
        addrProperties.setTo(to);

        if (addSeq) {
            AppSequenceType s = new AppSequenceType();
            s.setInstanceId(instanceId);
            s.setMessageNumber(msgId.getAndIncrement());
            JAXBElement<AppSequenceType> seq = new ObjectFactory().createAppSequence(s);
            Header h = new Header(seq.getName(),
                                  seq,
                                  new JAXBDataBinding(getJAXBContext()));
            List<Header> headers = new ArrayList<>();
            headers.add(h);
            p.getRequestContext()
                .put(Header.HEADER_LIST, headers);
        }
    } else {
        addrProperties.exposeAs(version.getAddressingNamespace());
    }
    p.getRequestContext().put(JAXWSAConstants.CLIENT_ADDRESSING_PROPERTIES, addrProperties);
}
 
Example 6
Source File: TokenProviderUtils.java    From cxf with Apache License 2.0 5 votes vote down vote up
/**
 * Extract an address from a Participants EPR DOM element
 */
public static String extractAddressFromParticipantsEPR(Object participants) {
    if (participants instanceof Element) {
        String localName = ((Element)participants).getLocalName();
        String namespace = ((Element)participants).getNamespaceURI();

        if (STSConstants.WSA_NS_05.equals(namespace) && "EndpointReference".equals(localName)) {
            LOG.fine("Found EndpointReference element");
            Element address =
                DOMUtils.getFirstChildWithName((Element)participants,
                        STSConstants.WSA_NS_05, "Address");
            if (address != null) {
                LOG.fine("Found address element");
                return address.getTextContent();
            }
        } else if ((STSConstants.WSP_NS.equals(namespace) || STSConstants.WSP_NS_04.equals(namespace)
            || STSConstants.WSP_NS_06.equals(namespace))
            && "URI".equals(localName)) {
            return ((Element)participants).getTextContent();
        }
        LOG.fine("Participants element does not exist or could not be parsed");
        return null;
    } else if (participants instanceof JAXBElement<?>) {
        JAXBElement<?> jaxbElement = (JAXBElement<?>) participants;
        QName participantsName = jaxbElement.getName();
        if (STSConstants.WSA_NS_05.equals(participantsName.getNamespaceURI())
            && "EndpointReference".equals(participantsName.getLocalPart())) {
            LOG.fine("Found EndpointReference element");
            EndpointReferenceType endpointReference = (EndpointReferenceType)jaxbElement.getValue();
            if (endpointReference.getAddress() != null) {
                LOG.fine("Found address element");
                return endpointReference.getAddress().getValue();
            }
        }
        LOG.fine("Participants element does not exist or could not be parsed");
    }

    return null;
}
 
Example 7
Source File: FilterHandler.java    From web-feature-service with Apache License 2.0 5 votes vote down vote up
public SelectionFilter getSelection(JAXBElement<?> selectionClauseElement, String handle) throws WFSException {
	if (!(selectionClauseElement.getValue() instanceof FilterType))
		throw new WFSException(WFSExceptionCode.INVALID_PARAMETER_VALUE, "The element '" + selectionClauseElement.getName() + "' is not supported as selection clause of queries.", handle); 

	FilterType filter = (FilterType)selectionClauseElement.getValue();		
	Predicate predicate;

	if (filter.get_Id() != null)
		predicate = resourceIdFilterBuilder.buildIdOperator(filter.get_Id(), handle);
	else
		throw new WFSException(WFSExceptionCode.OPTION_NOT_SUPPORTED, "The filer expression '" + selectionClauseElement.getName() + "' is not supported." , handle);

	return predicate != null ? new SelectionFilter(predicate) : null;
}
 
Example 8
Source File: JAXBHelper.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Nullable
public static <DATATYPE> JAXBElement <DATATYPE> getClonedJAXBElement (@Nullable final JAXBElement <DATATYPE> aObj)
{
  if (aObj == null)
    return null;

  final DATATYPE aClonedValue = CloneHelper.getClonedValue (aObj.getValue ());
  final JAXBElement <DATATYPE> ret = new JAXBElement <> (aObj.getName (),
                                                         aObj.getDeclaredType (),
                                                         aObj.getScope (),
                                                         aClonedValue);
  ret.setNil (aObj.isNil ());
  return ret;
}
 
Example 9
Source File: JAXBCopyStrategy.java    From jaxb2-basics with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@SuppressWarnings("unchecked")
protected Object copyInternal(ObjectLocator locator,
		@SuppressWarnings("rawtypes") final JAXBElement jaxbElement) {
	final Object sourceObject = jaxbElement.getValue();
	final Object copyObject = copy(
			property(locator, "value", sourceObject), sourceObject);
	@SuppressWarnings("rawtypes")
	final JAXBElement copyElement = new JAXBElement(jaxbElement.getName(),
			jaxbElement.getDeclaredType(), jaxbElement.getScope(),
			copyObject);
	return copyElement;
}
 
Example 10
Source File: TimedTextVerifier.java    From ttt with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public QName getBindingElementName(Object value) {
    Node xmlNode = binder.getXMLNode(value);
    if (xmlNode != null) {
        Object jaxbBinding = binder.getJAXBNode(xmlNode);
        if (jaxbBinding instanceof JAXBElement<?>) {
            JAXBElement<?> jaxbNode = (JAXBElement<?>) jaxbBinding;
            return jaxbNode.getName();
        } else
            return new QName(xmlNode.getNamespaceURI(), xmlNode.getLocalName());
    }
    return emptyName;
}
 
Example 11
Source File: MapperUtils.java    From proarc with GNU General Public License v3.0 5 votes vote down vote up
public static <T> List<JAXBElement<T>> find(List<JAXBElement<T>> elms, QName... names) {
    List<JAXBElement<T>> result = new ArrayList<>();
    for (JAXBElement<T> elm : elms) {
        QName elmName = elm.getName();
        for (QName name : names) {
            if (elmName.equals(name)) {
                result.add(elm);
            }
        }
    }
    return result;
}
 
Example 12
Source File: MapperUtils.java    From proarc with GNU General Public License v3.0 5 votes vote down vote up
public static List<JAXBElement<?>> findAny(List<JAXBElement<?>> elms, QName... names) {
    List<JAXBElement<?>> result = new ArrayList<>();
    for (JAXBElement<?> elm : elms) {
        QName elmName = elm.getName();
        for (QName name : names) {
            if (elmName.equals(name)) {
                result.add(elm);
            }
        }
    }
    return result;
}
 
Example 13
Source File: MapperUtils.java    From proarc with GNU General Public License v3.0 5 votes vote down vote up
public static JAXBElement<?> findFirst(List<JAXBElement<?>> elms, QName... names) {
    for (JAXBElement<?> elm : elms) {
        QName elmName = elm.getName();
        for (QName name : names) {
            if (elmName.equals(name)) {
                return elm;
            }
        }
    }
    return null;
}
 
Example 14
Source File: RoundtripTest.java    From hyperjaxb3 with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
	protected void checkSample(File sample) throws Exception {
		// TODO Auto-generated method stub
		final JAXBContext context = createContext();
		logger.debug("Unmarshalling.");
		final Unmarshaller unmarshaller = context.createUnmarshaller();
		// Unmarshall the document
		final JAXBElement element = (JAXBElement) unmarshaller
				.unmarshal(sample);
		final Object object = element.getValue();
		logger.debug("Opening session.");
		// Open the session, save object into the database
		logger.debug("Saving the object.");
		final PersistenceManager saveManager = createPersistenceManager();
//		saveManager.setDetachAllOnCommit(true);
		final Transaction saveTransaction = saveManager.currentTransaction();
		saveTransaction.setNontransactionalRead(true);
		saveTransaction.begin();
		// final Object merged = saveSession.merge(object);
		// saveSession.replicate(object, ReplicationMode.OVERWRITE);
		// saveSession.get
		// final Serializable id =
		final Object mergedObject = saveManager.makePersistent(object);
		
//		final Object asd = saveManager.detachCopy(object);
		saveTransaction.commit();
//		final Object id = saveManager.getObjectId(mergedObject);
		final Object identity = JDOHelper.getObjectId(object);
		final Object id = identity instanceof SingleFieldIdentity ? ((SingleFieldIdentity) identity).getKeyAsObject() : identity;
		// Close the session
		saveManager.close();

		logger.debug("Opening session.");
		// Open the session, load the object
		final PersistenceManager loadManager = createPersistenceManager();
		final Transaction loadTransaction = loadManager.currentTransaction();
		loadTransaction.setNontransactionalRead(true);
		logger.debug("Loading the object.");
		final Object loadedObject = loadManager.getObjectById(mergedObject.getClass(), id);
		logger.debug("Closing the session.");

		final JAXBElement mergedElement = new JAXBElement(element.getName(),
				element.getDeclaredType(), object);

		final JAXBElement loadedElement = new JAXBElement(element.getName(),
				element.getDeclaredType(), loadedObject);

		logger.debug("Checking the document identity.");

		logger.debug("Source object:\n"
				+ ContextUtils.toString(context, mergedElement));
		logger.debug("Result object:\n"
				+ ContextUtils.toString(context, loadedElement));

		checkObjects(mergedObject, loadedObject);
		loadManager.close();

	}
 
Example 15
Source File: JSONProvider.java    From entando-core with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
protected void marshalCollection(Class<?> originalCls, Object collection, 
                                    Type genericType, String encoding, 
                                    OutputStream os, MediaType m, Annotation[] anns) throws Exception {
       Class<?> actualClass = InjectionUtils.getActualType(genericType);
       actualClass = getActualType(actualClass, genericType, anns);
       Collection<?> c = originalCls.isArray() ? Arrays.asList((Object[]) collection) 
                                            : (Collection<?>) collection;
       Iterator<?> it = c.iterator();
       Object firstObj = it.hasNext() ? it.next() : null;
       String startTag = null;
       String endTag = null;
       if (!dropCollectionWrapperElement) {
           QName qname = null;
           if (firstObj instanceof JAXBElement) {
               JAXBElement<?> el = (JAXBElement<?>)firstObj;
               qname = el.getName();
               actualClass = el.getDeclaredType();
           } else {
               qname = getCollectionWrapperQName(actualClass, genericType, firstObj, false);
           }
           String prefix = "";
           if (!ignoreNamespaces) {
               prefix = namespaceMap.get(qname.getNamespaceURI());
               if (prefix != null) {
                   if (prefix.length() > 0) {
                       prefix += ".";
                   }
               } else if (qname.getNamespaceURI().length() > 0) {
                   prefix = "ns1.";
               }
           }
           prefix = (prefix == null) ? "" : prefix;
           startTag = "{\"" + prefix + qname.getLocalPart() + "\":[";
           endTag = "]}";
       } else if (serializeAsArray) {
           startTag = "[";
           endTag = "]";
       } else {
           startTag = "{";
           endTag = "}";
       }
       os.write(startTag.getBytes());
       if (firstObj != null) {
           XmlJavaTypeAdapter adapter = 
               org.apache.cxf.jaxrs.utils.JAXBUtils.getAdapter(firstObj.getClass(), anns);
           marshalCollectionMember(JAXBUtils.useAdapter(firstObj, adapter, true),
                                   actualClass, genericType, encoding, os);
           while (it.hasNext()) {
               os.write(",".getBytes());
               marshalCollectionMember(JAXBUtils.useAdapter(it.next(), adapter, true), 
                                       actualClass, genericType, encoding, os);
           }
       }
       os.write(endTag.getBytes());
   }
 
Example 16
Source File: JAXBElementProvider.java    From cxf with Apache License 2.0 4 votes vote down vote up
protected void marshalCollection(Class<?> originalCls, Object collection,
                                 Type genericType, String enc, OutputStream os,
                                 MediaType m, Annotation[] anns)
    throws Exception {

    Class<?> actualClass = InjectionUtils.getActualType(genericType);
    actualClass = getActualType(actualClass, genericType, anns);

    Collection<?> c = originalCls.isArray() ? Arrays.asList((Object[]) collection)
                                         : (Collection<?>) collection;

    Iterator<?> it = c.iterator();

    Object firstObj = it.hasNext() ? it.next() : null;

    QName qname = null;
    if (firstObj instanceof JAXBElement) {
        JAXBElement<?> el = (JAXBElement<?>)firstObj;
        qname = el.getName();
        actualClass = el.getDeclaredType();
    } else {
        qname = getCollectionWrapperQName(actualClass, genericType, firstObj, true);
    }
    if (qname == null) {
        String message = new org.apache.cxf.common.i18n.Message("NO_COLLECTION_ROOT",
                                                                BUNDLE).toString();
        throw new WebApplicationException(Response.serverError()
                                          .entity(message).build());
    }

    StringBuilder pi = new StringBuilder();
    pi.append(XML_PI_START + (enc == null ? StandardCharsets.UTF_8.name() : enc) + "\"?>");
    os.write(pi.toString().getBytes());
    String startTag = null;
    String endTag = null;

    if (qname.getNamespaceURI().length() > 0) {
        String prefix = nsPrefixes.get(qname.getNamespaceURI());
        if (prefix == null) {
            prefix = "ns1";
        }
        startTag = "<" + prefix + ":" + qname.getLocalPart() + " xmlns:" + prefix + "=\""
            + qname.getNamespaceURI() + "\">";
        endTag = "</" + prefix + ":" + qname.getLocalPart() + ">";
    } else {
        startTag = "<" + qname.getLocalPart() + ">";
        endTag = "</" + qname.getLocalPart() + ">";
    }
    os.write(startTag.getBytes());
    if (firstObj != null) {
        XmlJavaTypeAdapter adapter =
            org.apache.cxf.jaxrs.utils.JAXBUtils.getAdapter(firstObj.getClass(), anns);
        marshalCollectionMember(JAXBUtils.useAdapter(firstObj, adapter, true),
                                actualClass, genericType, enc, os, anns, m,
                                qname.getNamespaceURI());
        while (it.hasNext()) {
            marshalCollectionMember(JAXBUtils.useAdapter(it.next(), adapter, true), actualClass,
                                    genericType, enc, os, anns, m,
                                    qname.getNamespaceURI());
        }
    }
    os.write(endTag.getBytes());
}
 
Example 17
Source File: JSONProvider.java    From cxf with Apache License 2.0 4 votes vote down vote up
protected void marshalCollection(Class<?> originalCls, Object collection,
                                 Type genericType, String encoding,
                                 OutputStream os, MediaType m, Annotation[] anns)
    throws Exception {

    Class<?> actualClass = InjectionUtils.getActualType(genericType);
    actualClass = getActualType(actualClass, genericType, anns);

    Collection<?> c = originalCls.isArray() ? Arrays.asList((Object[]) collection)
                                         : (Collection<?>) collection;

    Iterator<?> it = c.iterator();

    Object firstObj = it.hasNext() ? it.next() : null;

    String startTag = null;
    String endTag = null;
    if (!dropCollectionWrapperElement) {
        QName qname = null;
        if (firstObj instanceof JAXBElement) {
            JAXBElement<?> el = (JAXBElement<?>)firstObj;
            qname = el.getName();
            actualClass = el.getDeclaredType();
        } else {
            qname = getCollectionWrapperQName(actualClass, genericType, firstObj, false);
        }
        String prefix = "";
        if (!ignoreNamespaces) {
            prefix = namespaceMap.get(qname.getNamespaceURI());
            if (prefix != null) {
                if (prefix.length() > 0) {
                    prefix += ".";
                }
            } else if (qname.getNamespaceURI().length() > 0) {
                prefix = "ns1.";
            }
        }
        prefix = (prefix == null) ? "" : prefix;
        startTag = "{\"" + prefix + qname.getLocalPart() + "\":[";
        endTag = "]}";
    } else if (serializeAsArray) {
        startTag = "[";
        endTag = "]";
    } else {
        startTag = "{";
        endTag = "}";
    }

    os.write(startTag.getBytes());
    if (firstObj != null) {
        XmlJavaTypeAdapter adapter =
            org.apache.cxf.jaxrs.utils.JAXBUtils.getAdapter(firstObj.getClass(), anns);
        marshalCollectionMember(JAXBUtils.useAdapter(firstObj, adapter, true),
                                actualClass, genericType, encoding, os);
        while (it.hasNext()) {
            os.write(",".getBytes());
            marshalCollectionMember(JAXBUtils.useAdapter(it.next(), adapter, true),
                                    actualClass, genericType, encoding, os);
        }
    }
    os.write(endTag.getBytes());
}
 
Example 18
Source File: RsBuilder.java    From timbuctoo with GNU General Public License v3.0 4 votes vote down vote up
@SuppressWarnings ("unchecked")
public Optional<RsRoot> build() throws JAXBException {
  latestQName = null;
  urlset = null;
  sitemapindex = null;

  JAXBElement<RsRoot> je = null;
  RsRoot rsRoot = null;
  Unmarshaller unmarshaller = rsContext.createUnmarshaller();
  if (file != null) {
    je = (JAXBElement<RsRoot>) unmarshaller.unmarshal(file);
    file = null;
  } else if (inputSource != null) {
    je = (JAXBElement<RsRoot>) unmarshaller.unmarshal(inputSource);
    inputSource = null;
  } else if (inputStream != null) {
    je = (JAXBElement<RsRoot>) unmarshaller.unmarshal(inputStream);
    inputStream = null;
  } else if (node != null) {
    je = (JAXBElement<RsRoot>) unmarshaller.unmarshal(node);
    node = null;
  } else if (reader != null) {
    je = (JAXBElement<RsRoot>) unmarshaller.unmarshal(reader);
    reader = null;
  } else if (source != null) {
    je = (JAXBElement<RsRoot>) unmarshaller.unmarshal(source);
    source = null;
  } else if (url != null) {
    je = (JAXBElement<RsRoot>) unmarshaller.unmarshal(url);
    url = null;
  } else if (xmlEventReader != null) {
    je = (JAXBElement<RsRoot>) unmarshaller.unmarshal(xmlEventReader);
    xmlEventReader = null;
  } else if (xmlStreamReader != null) {
    je = (JAXBElement<RsRoot>) unmarshaller.unmarshal(xmlStreamReader);
    xmlStreamReader = null;
  } else if (xmlString != null) {
    je = (JAXBElement<RsRoot>) unmarshaller.unmarshal(IOUtils.toInputStream(xmlString));
    xmlString = null;
  }

  if (je != null) {
    latestQName = je.getName();
    rsRoot = je.getValue();
    if (latestQName.equals(Urlset.QNAME)) {
      urlset = (Urlset) rsRoot;
    } else if (latestQName.equals(Sitemapindex.QNAME)) {
      sitemapindex = (Sitemapindex) rsRoot;
    }
  }
  return Optional.ofNullable(rsRoot);
}