Example usage for javax.xml.namespace QName getPrefix

List of usage examples for javax.xml.namespace QName getPrefix

Introduction

In this page you can find the example usage for javax.xml.namespace QName getPrefix.

Prototype

public String getPrefix() 

Source Link

Document

Get the prefix of this QName.

The prefix assigned to a QName might NOT be valid in a different context.

Usage

From source file:Main.java

/**
 * @param qname/*w w w.  j av  a 2 s.  com*/
 *            The QName to serialize into prefix:localName form.
 * @return This method returns a different value than QName.toString(). The
 *         QName.toString() returns a string of the format: <br>
 * <br>
 *         <em>{namespace URI}:name</em> <br>
 * <br>
 *         whereas this method returns: <br>
 * <br>
 *         <em>prefix:name</em> <br>
 * <br>
 *         which is a valid representation to put into XML documents. If
 *         the QName has no prefix, the local name is returned.
 */
public static String toString(final QName qname) {
    final String prefix = qname.getPrefix();
    final String name = qname.getLocalPart();

    if (prefix == null || prefix.length() == 0)
        return name;

    return prefix + ':' + name;
}

From source file:com.evolveum.midpoint.util.QNameUtil.java

public static QName nullNamespace(QName qname) {
    return new QName(null, qname.getLocalPart(), qname.getPrefix());
}

From source file:Main.java

/**
 * Creates a new Element having the specified qualified name. The element
 * must be {@link Document#adoptNode(Node) adopted} when inserted into
 * another Document.// ww w. j  a v a2s .  c o  m
 * 
 * @param qName
 *            A QName object.
 * @return An Element node (with a Document owner but no parent).
 */
public static Element createElement(QName qName) {
    Document doc = null;
    try {
        doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
    } catch (ParserConfigurationException e) {
        throw new RuntimeException(e);
    }
    StringBuilder nodeName = new StringBuilder();
    if (qName.getPrefix().isEmpty()) {
        nodeName.append(qName.getLocalPart());
    } else {
        nodeName.append(qName.getPrefix()).append(':').append(qName.getLocalPart());
    }
    Element elem = doc.createElementNS(qName.getNamespaceURI(), nodeName.toString());
    return elem;
}

From source file:io.cloudslang.content.xml.utils.XmlUtils.java

/**
 * Returns the Namespaces context from an xml.
 *
 * @param xmlString   xml as string// w  w w. j a v a 2 s . c o m
 * @param xmlFilePath path to xml file
 * @return the Namespaces context from an xml.
 * @throws IOException        file reading exception
 * @throws XMLStreamException parsing exception
 */
public static NamespaceContext getNamespaceContext(String xmlString, String xmlFilePath) throws Exception {
    InputStream inputXML = getStream(xmlString, xmlFilePath);
    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    XMLStreamReader reader = inputFactory.createXMLStreamReader(inputXML);
    Map<String, String> namespaces = new HashMap<>();
    while (reader.hasNext()) {
        int evt = reader.next();
        if (evt == XMLStreamConstants.START_ELEMENT) {
            QName qName = reader.getName();
            if (qName != null) {
                if (qName.getPrefix() != null && qName.getPrefix().compareTo("") != 0)
                    namespaces.put(qName.getPrefix(), qName.getNamespaceURI());
            }
        }
    }
    return new SimpleNamespaceContext(namespaces);
}

From source file:eu.esdihumboldt.hale.io.wfs.ui.KVPUtil.java

/**
 * Add typename and namespace parameters for a WFS request to the given URI
 * builder./* www .  j  a  v a  2s.  c o  m*/
 * 
 * @param builder the builder for the WFS request URI
 * @param selected the type names to include
 * @param version the targeted WFS version
 */
public static void addTypeNameParameter(URIBuilder builder, Iterable<QName> selected, WFSVersion version) {
    // namespaces mapped to prefixes
    Map<String, String> namespaces = new HashMap<>();
    // type names with updated prefix
    Set<QName> typeNames = new HashSet<>();

    for (QName type : selected) {
        String prefix;
        if (type.getNamespaceURI() != null && !type.getNamespaceURI().isEmpty()) {
            prefix = namespaces.get(type.getNamespaceURI());
            if (prefix == null) {
                // no mapping yet for namespace
                String candidate = type.getPrefix();
                prefix = addPrefix(candidate, type.getNamespaceURI(), namespaces);
            }
        } else {
            // default namespace
            prefix = XMLConstants.DEFAULT_NS_PREFIX;
        }

        // add updated type
        typeNames.add(new QName(type.getNamespaceURI(), type.getLocalPart(), prefix));
    }

    final String paramNamespaces;
    final String paramTypenames;
    final String prefixNamespaceDelim;
    switch (version) {
    case V1_1_0:
        paramNamespaces = "NAMESPACE";
        paramTypenames = "TYPENAME";
        prefixNamespaceDelim = "=";
        break;
    case V2_0_0:
    case V2_0_2:
        /*
         * XXX below are the values as defined in the WFS 2 specification.
         * There have been problems with some GeoServer instances if used in
         * that manner.
         */
        paramNamespaces = "NAMESPACES";
        paramTypenames = "TYPENAMES";
        prefixNamespaceDelim = ",";
        break;
    default:
        // fall-back to WFS 1.1
        paramNamespaces = "NAMESPACE";
        paramTypenames = "TYPENAME";
        prefixNamespaceDelim = "=";
    }

    // add namespace prefix definitions
    if (!namespaces.isEmpty()) {
        builder.addParameter(paramNamespaces, Joiner.on(',')
                .join(Maps.transformEntries(namespaces, new EntryTransformer<String, String, String>() {

                    @Override
                    public String transformEntry(String namespace, String prefix) {
                        StringBuilder sb = new StringBuilder();
                        sb.append("xmlns(");
                        sb.append(prefix);
                        sb.append(prefixNamespaceDelim);
                        sb.append(namespace);
                        sb.append(")");
                        return sb.toString();
                    }

                }).values()));
    }
    // add type names
    if (!typeNames.isEmpty()) {
        builder.addParameter(paramTypenames,
                Joiner.on(',').join(Iterables.transform(typeNames, new Function<QName, String>() {

                    @Override
                    public String apply(QName typeName) {
                        String prefix = typeName.getPrefix();
                        if (prefix == null || prefix.isEmpty()) {
                            return typeName.getLocalPart();
                        }
                        return prefix + ":" + typeName.getLocalPart();
                    }
                })));
    }
}

From source file:ee.ria.xroad.common.message.SoapUtils.java

/**
 * Converts a SOAP request to SOAP response, by adding "Response" suffix
 * to the service name in the body./* ww w  .  ja v a  2s  . c  o m*/
 * @param request SOAP request message to be converted
 * @param callback function to call when the response SOAP object has been created
 * @return the response SoapMessageImpl
 * @throws Exception if errors occur during response message creation
 */
public static SoapMessageImpl toResponse(SoapMessageImpl request, SOAPCallback callback) throws Exception {
    String charset = request.getCharset();

    SOAPMessage soap = createSOAPMessage(new ByteArrayInputStream(request.getBytes()), charset);

    List<SOAPElement> bodyChildren = getChildElements(soap.getSOAPBody());
    if (bodyChildren.size() == 0) {
        return null;
    }

    QName requestElementQName = bodyChildren.get(0).getElementQName();
    String serviceCode = getServiceCode(soap, requestElementQName);

    QName newName = new QName(requestElementQName.getNamespaceURI(), serviceCode + SOAP_SUFFIX_RESPONSE,
            requestElementQName.getPrefix());
    bodyChildren.get(0).setElementQName(newName);

    if (callback != null) {
        callback.call(soap);
    }

    byte[] xml = getBytes(soap);
    return (SoapMessageImpl) new SoapParserImpl().parseMessage(xml, soap, charset, request.getContentType());
}

From source file:eu.eidas.auth.commons.attribute.AttributeSetPropertiesConverter.java

/**
 * Converts the given Iterable of <code>AttributeDefinition</code>s to an immutable sorted Map of Strings.
 *
 * @param properties the Iterable of <code>AttributeDefinition</code>s
 * @return an immutable sorted Map of Strings.
 *//* ww w. j a v a 2 s .  co m*/
@Nonnull
public static ImmutableSortedMap<String, String> toMap(@Nonnull Iterable<AttributeDefinition<?>> attributes) {
    ImmutableSortedMap.Builder<String, String> builder = new ImmutableSortedMap.Builder<String, String>(
            Ordering.natural());
    int i = 0;
    for (AttributeDefinition<?> attributeDefinition : attributes) {
        String id = String.valueOf(++i);
        builder.put(Suffix.NAME_URI.computeEntry(id), attributeDefinition.getNameUri().toASCIIString());
        builder.put(Suffix.FRIENDLY_NAME.computeEntry(id), attributeDefinition.getFriendlyName());
        builder.put(Suffix.PERSON_TYPE.computeEntry(id), attributeDefinition.getPersonType().getValue());
        builder.put(Suffix.REQUIRED.computeEntry(id), String.valueOf(attributeDefinition.isRequired()));
        if (attributeDefinition.isTransliterationMandatory()) {
            builder.put(Suffix.TRANSLITERATION_MANDATORY.computeEntry(id), Boolean.TRUE.toString());
        }
        if (attributeDefinition.isUniqueIdentifier()) {
            builder.put(Suffix.UNIQUE_IDENTIFIER.computeEntry(id), Boolean.TRUE.toString());
        }
        QName xmlType = attributeDefinition.getXmlType();
        builder.put(Suffix.XML_TYPE_NAMESPACE_URI.computeEntry(id), String.valueOf(xmlType.getNamespaceURI()));
        builder.put(Suffix.XML_TYPE_LOCAL_PART.computeEntry(id), String.valueOf(xmlType.getLocalPart()));
        builder.put(Suffix.XML_TYPE_NAMESPACE_PREFIX.computeEntry(id), String.valueOf(xmlType.getPrefix()));
        builder.put(Suffix.ATTRIBUTE_VALUE_MARSHALLER.computeEntry(id),
                attributeDefinition.getAttributeValueMarshaller().getClass().getName());
    }
    return builder.build();
}

From source file:com.evolveum.midpoint.prism.path.NameItemPathSegment.java

private String normalizedPrefix(QName name) {
    if (name.getPrefix() == null) {
        return "";
    } else {//from ww w .j av  a  2  s.  c om
        return name.getPrefix();
    }
}

From source file:com.evolveum.midpoint.prism.LiteralEqualsStrategy.java

@Override
protected boolean equalsInternal(ObjectLocator leftLocator, ObjectLocator rightLocator, Object lhs,
        Object rhs) {//from w w  w  . j a v  a 2 s  . c  o  m
    //      System.out.println("DomAwareEqualsStrategy: "+PrettyPrinter.prettyPrint(lhs)+"<=>"+PrettyPrinter.prettyPrint(rhs));
    if (lhs instanceof String && rhs instanceof String) {
        // this is questionable (but seems ok)
        return DOMUtil.compareTextNodeValues((String) lhs, (String) rhs);
    } else if (lhs instanceof Element && rhs instanceof Element) {
        // this is perhaps obsolete
        final Element left = (Element) lhs;
        final Element right = (Element) rhs;
        boolean result = DOMUtil.compareElement(left, right, true);
        //         System.out.println("cmp: "+PrettyPrinter.prettyPrint(left)+"<=>"+PrettyPrinter.prettyPrint(right)+": "+result);
        return result;
    } else if (lhs instanceof QName && rhs instanceof QName) {
        QName l = (QName) lhs;
        QName r = (QName) rhs;
        if (!l.equals(r)) {
            return false;
        }
        return StringUtils.equals(l.getPrefix(), r.getPrefix());
    } else if (lhs instanceof ItemPathType && rhs instanceof ItemPathType) {
        // ItemPathType's equals is already working literally
        return ((ItemPathType) lhs).equals((ItemPathType) rhs);
    } else {
        return super.equalsInternal(leftLocator, rightLocator, lhs, rhs);
    }
}

From source file:microsoft.exchange.webservices.data.core.EwsXmlReader.java

/**
 * Check end element./*from ww w .  j av a2  s  . c  om*/
 *
 * @param startEvent the start event
 * @param endEvent   the end event
 * @return true, if successful
 */
public static boolean checkEndElement(XMLEvent startEvent, XMLEvent endEvent) {
    boolean isEndElement = false;
    if (endEvent.isEndElement()) {
        QName qEName = endEvent.asEndElement().getName();
        QName qSName = startEvent.asStartElement().getName();
        isEndElement = qEName.getLocalPart().equals(qSName.getLocalPart())
                && (qEName.getPrefix().equals(qSName.getPrefix())
                        || qEName.getNamespaceURI().equals(qSName.getNamespaceURI()));

    }
    return isEndElement;
}