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:com.googlecode.ddom.frontend.saaj.SOAPElementTest.java

@Validated
@Test//from   www .j  a  va2  s  .c o m
public void testCreateQNameWithAddNamespaceDeclaration() throws Exception {
    SOAPElement element = saajUtil.createSOAPElement("urn:ns", "test", null);
    element.addNamespaceDeclaration("p", "urn:test");
    QName qname = element.createQName("local", "p");
    assertEquals("p", qname.getPrefix());
    assertEquals("local", qname.getLocalPart());
    assertEquals("urn:test", qname.getNamespaceURI());
}

From source file:com.evolveum.midpoint.prism.marshaller.XPathHolder.java

public Map<String, String> getNamespaceMap() {

    Map<String, String> namespaceMap = new HashMap<String, String>();
    Iterator<XPathSegment> iter = segments.iterator();
    while (iter.hasNext()) {
        XPathSegment seg = iter.next();/*www .  j a  va  2 s  .co  m*/
        QName qname = seg.getQName();
        if (qname != null) {
            if (qname.getPrefix() != null && !qname.getPrefix().isEmpty()) {
                namespaceMap.put(qname.getPrefix(), qname.getNamespaceURI());
            }
            // this code seems to be currently of no use
            //                else {
            //                    // Default namespace
            //                    // HACK. See addPureXpath method
            //                    namespaceMap.put(DEFAULT_PREFIX, qname.getNamespaceURI());
            //                }
        }
    }

    return namespaceMap;
}

From source file:de.uzk.hki.da.cb.CreatePremisAction.java

/**
 * Accepts a premis.xml file and creates a new xml file for each jhove section  
 * /* ww  w  .ja v a 2 s.  co m*/
 * @author Thomas Kleinke
 * Extract jhove data.
 *
 * @param premisFilePath the premis file path
 * @param outputFolder the output folder
 * @throws XMLStreamException the xML stream exception
 */
public void extractJhoveData(String premisFilePath, String outputFolder) throws XMLStreamException {

    outputFolder += "/premis_output/";

    FileInputStream inputStream = null;

    try {
        inputStream = new FileInputStream(premisFilePath);
    } catch (FileNotFoundException e) {
        throw new RuntimeException("Couldn't find file " + premisFilePath, e);
    }

    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    XMLStreamReader streamReader = inputFactory.createXMLStreamReader(inputStream);

    boolean textElement = false;
    boolean jhoveSection = false;
    boolean objectIdentifierValue = false;
    int tab = 0;
    String fileId = "";

    while (streamReader.hasNext()) {
        int event = streamReader.next();

        switch (event) {

        case XMLStreamConstants.START_ELEMENT:

            if (streamReader.getLocalName().equals("jhove")) {
                jhoveSection = true;
                String outputFilePath = outputFolder + fileId.replace('/', '_').replace('.', '_') + ".xml";
                if (!new File(outputFolder).exists())
                    new File(outputFolder).mkdirs();
                writer = startNewDocument(outputFilePath);
            }

            if (streamReader.getLocalName().equals("objectIdentifierValue"))
                objectIdentifierValue = true;

            if (jhoveSection) {
                writer.writeDTD("\n");
                indent(tab);
                tab++;

                String prefix = streamReader.getPrefix();

                if (prefix != null && !prefix.equals("")) {
                    writer.setPrefix(prefix, streamReader.getNamespaceURI());
                    writer.writeStartElement(streamReader.getNamespaceURI(), streamReader.getLocalName());
                } else
                    writer.writeStartElement(streamReader.getLocalName());

                for (int i = 0; i < streamReader.getNamespaceCount(); i++)
                    writer.writeNamespace(streamReader.getNamespacePrefix(i), streamReader.getNamespaceURI(i));

                for (int i = 0; i < streamReader.getAttributeCount(); i++) {
                    QName qname = streamReader.getAttributeName(i);
                    String attributeName = qname.getLocalPart();
                    String attributePrefix = qname.getPrefix();
                    if (attributePrefix != null && !attributePrefix.equals(""))
                        attributeName = attributePrefix + ":" + attributeName;

                    writer.writeAttribute(attributeName, streamReader.getAttributeValue(i));
                }
            }

            break;

        case XMLStreamConstants.CHARACTERS:
            if (objectIdentifierValue) {
                fileId = streamReader.getText();
                objectIdentifierValue = false;
            }

            if (jhoveSection && !streamReader.isWhiteSpace()) {
                writer.writeCharacters(streamReader.getText());
                textElement = true;
            }
            break;

        case XMLStreamConstants.END_ELEMENT:
            if (jhoveSection) {
                tab--;

                if (!textElement) {
                    writer.writeDTD("\n");
                    indent(tab);
                }

                writer.writeEndElement();
                textElement = false;

                if (streamReader.getLocalName().equals("jhove")) {
                    jhoveSection = false;
                    finalizeDocument();
                }
            }
            break;

        case XMLStreamConstants.END_DOCUMENT:
            streamReader.close();
            try {
                inputStream.close();
            } catch (IOException e) {
                throw new RuntimeException("Failed to close input stream", e);
            }
            break;

        default:
            break;
        }
    }
}

From source file:com.legstar.proxy.invoke.jaxws.WebServiceInvoker.java

/**
 * Try to extract something meaningful from a SOAP Fault.
 * //  www.ja v a  2s .c o  m
 * @param e the SOAP Fault exception
 * @return a fault description
 */
@SuppressWarnings("rawtypes")
public String getFaultReasonText(final SOAPFaultException e) {
    if (_log.isDebugEnabled()) {
        SOAPFault fault = e.getFault();
        if (fault != null) {
            QName code = fault.getFaultCodeAsQName();
            String string = fault.getFaultString();
            String actor = fault.getFaultActor();
            _log.debug("SOAP fault contains: ");
            _log.debug("  Fault code = " + code.toString());
            _log.debug("  Local name = " + code.getLocalPart());
            _log.debug("  Namespace prefix = " + code.getPrefix() + ", bound to " + code.getNamespaceURI());
            _log.debug("  Fault string = " + string);
            if (actor != null) {
                _log.debug("  Fault actor = " + actor);
            }
            Detail detail = fault.getDetail();
            if (detail != null) {
                Iterator entries = detail.getDetailEntries();
                while (entries.hasNext()) {
                    DetailEntry newEntry = (DetailEntry) entries.next();
                    String value = newEntry.getValue();
                    _log.debug("  Detail entry = " + value);
                }
            }
        } else {
            _log.debug(e);
        }
    }
    SOAPFault fault = e.getFault();
    if (fault != null) {
        StringBuffer faultMessage = new StringBuffer(e.getFault().getFaultString());
        Detail detail = fault.getDetail();
        if (detail != null) {
            Iterator entries = detail.getDetailEntries();
            while (entries.hasNext()) {
                DetailEntry newEntry = (DetailEntry) entries.next();
                faultMessage.append(" [" + newEntry.getValue() + "]");
            }
        }
        return faultMessage.toString();
    } else {
        return e.getMessage();
    }

}

From source file:com.evolveum.midpoint.prism.marshaller.ItemPathHolder.java

public ItemPathHolder(ItemPath itemPath, boolean forceExplicitNamespaceDeclarations) {
    if (itemPath.getNamespaceMap() != null) {
        this.explicitNamespaceDeclarations.putAll(itemPath.getNamespaceMap());
    }//  ww  w  . ja v  a  2s. c  om
    this.segments = new ArrayList<>();
    for (ItemPathSegment segment : itemPath.getSegments()) {
        PathHolderSegment xsegment;
        if (segment instanceof NameItemPathSegment) {
            boolean variable = segment.isVariable();
            QName name = ((NameItemPathSegment) segment).getName();
            xsegment = new PathHolderSegment(name, variable);
            if (forceExplicitNamespaceDeclarations && StringUtils.isNotEmpty(name.getPrefix())) {
                this.explicitNamespaceDeclarations.put(name.getPrefix(), name.getNamespaceURI());
            }
        } else if (segment instanceof IdItemPathSegment) {
            xsegment = new PathHolderSegment(idToString(((IdItemPathSegment) segment).getId()));
        } else if (segment instanceof ObjectReferencePathSegment) {
            xsegment = new PathHolderSegment(PrismConstants.T_OBJECT_REFERENCE, false);
        } else if (segment instanceof ParentPathSegment) {
            xsegment = new PathHolderSegment(PrismConstants.T_PARENT, false);
        } else if (segment instanceof IdentifierPathSegment) {
            xsegment = new PathHolderSegment(PrismConstants.T_ID, false);
        } else {
            throw new IllegalStateException("Unknown segment: " + segment);
        }
        this.segments.add(xsegment);
    }
    this.absolute = false;
}

From source file:eu.playproject.platform.service.bootstrap.DSBTopicManager.java

@Override
public Subscription subscribe(String producer, QName topic, String subscriber) throws BootstrapFault {
    Subscription subscription = null;//from w  ww  .  j a  v  a 2 s .  c o m

    logger.info("Subscribe to topic '" + topic + "' on producer '" + producer + "' for subscriber '"
            + subscriber + "'");

    HTTPProducerClient client = new HTTPProducerClient(producer);
    try {
        String id = client.subscribe(topic, subscriber);
        logger.info("Subscribed to topic " + topic + " and ID is " + id);

        subscription = new Subscription();
        subscription.setDate(System.currentTimeMillis());
        subscription.setId(id);
        subscription.setProvider(producer);
        subscription.setSubscriber(subscriber);
        Topic t = new Topic();
        t.setName(topic.getLocalPart());
        t.setNs(topic.getNamespaceURI());
        t.setPrefix(topic.getPrefix());
        subscription.setTopic(t);

        this.subscriptionRegistry.addSubscription(subscription);

    } catch (NotificationException e) {
        logger.log(Level.SEVERE, "Problem while subscribing", e);
        throw new BootstrapFault(e);
    } catch (GovernanceExeption e) {
        logger.log(Level.WARNING, "Problem while saving subscription", e);
        //throw new BootstrapFault(e);
    }
    return subscription;
}

From source file:de.uzk.hki.da.model.ObjectPremisXmlWriter.java

/**
 * Integrate jhove data.//w  ww . j a  va 2s  . com
 *
 * @param jhoveFilePath the jhove file path
 * @param tab the tab
 * @throws XMLStreamException the xML stream exception
 * @author Thomas Kleinke
 * @throws FileNotFoundException 
 */
private void integrateJhoveData(String jhoveFilePath, int tab)
        throws XMLStreamException, FileNotFoundException {
    File jhoveFile = new File(jhoveFilePath);
    if (!jhoveFile.exists())
        throw new FileNotFoundException("file does not exist. " + jhoveFile);

    FileInputStream inputStream = null;

    inputStream = new FileInputStream(jhoveFile);

    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    XMLStreamReader streamReader = inputFactory.createXMLStreamReader(inputStream);

    boolean textElement = false;

    while (streamReader.hasNext()) {
        int event = streamReader.next();

        switch (event) {

        case XMLStreamConstants.START_ELEMENT:
            writer.writeDTD("\n");
            indent(tab);
            tab++;

            String prefix = streamReader.getPrefix();

            if (prefix != null && !prefix.equals("")) {
                writer.setPrefix(prefix, streamReader.getNamespaceURI());
                writer.writeStartElement(streamReader.getNamespaceURI(), streamReader.getLocalName());
            } else
                writer.writeStartElement(streamReader.getLocalName());

            for (int i = 0; i < streamReader.getNamespaceCount(); i++)
                writer.writeNamespace(streamReader.getNamespacePrefix(i), streamReader.getNamespaceURI(i));

            for (int i = 0; i < streamReader.getAttributeCount(); i++) {
                QName qname = streamReader.getAttributeName(i);
                String attributeName = qname.getLocalPart();
                String attributePrefix = qname.getPrefix();
                if (attributePrefix != null && !attributePrefix.equals(""))
                    attributeName = attributePrefix + ":" + attributeName;

                writer.writeAttribute(attributeName, streamReader.getAttributeValue(i));
            }

            break;

        case XMLStreamConstants.CHARACTERS:
            if (!streamReader.isWhiteSpace()) {
                writer.writeCharacters(streamReader.getText());
                textElement = true;
            }
            break;

        case XMLStreamConstants.END_ELEMENT:
            tab--;

            if (!textElement) {
                writer.writeDTD("\n");
                indent(tab);
            }

            writer.writeEndElement();
            textElement = false;
            break;

        default:
            break;
        }
    }

    streamReader.close();
    try {
        inputStream.close();
    } catch (IOException e) {
        throw new RuntimeException("Failed to close input stream", e);
    }
}

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

/**
 * Determines whether current element is a start element.
 *
 * @param namespacePrefix the namespace prefix
 * @param localName       the local name
 * @return boolean/*from ww  w  .  j  a  va  2s  . c  om*/
 */
public boolean isStartElement(String namespacePrefix, String localName) {
    boolean isStart = false;
    if (this.presentEvent.isStartElement()) {
        StartElement startElement = this.presentEvent.asStartElement();
        QName qName = startElement.getName();
        isStart = qName.getLocalPart().equals(localName) && qName.getPrefix().equals(namespacePrefix);
    }
    return isStart;
}

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

/**
 * Determines whether current element is a end element.
 *
 * @param namespacePrefix the namespace prefix
 * @param localName       the local name
 * @return boolean//from   w w  w  .j  a v a2  s . c  o  m
 */
public boolean isEndElement(String namespacePrefix, String localName) {
    boolean isEndElement = false;
    if (this.presentEvent.isEndElement()) {
        EndElement endElement = this.presentEvent.asEndElement();
        QName qName = endElement.getName();
        isEndElement = qName.getLocalPart().equals(localName) && qName.getPrefix().equals(namespacePrefix);

    }
    return isEndElement;
}

From source file:com.evolveum.midpoint.prism.marshaller.ItemPathHolder.java

private void addPureXpath(StringBuilder sb) {
    if (!absolute && segments.isEmpty()) {
        // Empty segment list gives a "local node" XPath
        sb.append(".");
        return;//from  w  ww. ja  v a 2s. c o  m
    }

    if (absolute) {
        sb.append("/");
    }

    boolean first = true;

    for (PathHolderSegment seg : segments) {

        if (seg.isIdValueFilter()) {

            sb.append("[");
            sb.append(seg.getValue());
            sb.append("]");

        } else {

            if (!first) {
                sb.append("/");
            } else {
                first = false;
            }

            if (seg.isVariable()) {
                sb.append("$");
            }
            QName qname = seg.getQName();

            if (ObjectReferencePathSegment.QNAME.equals(qname)) {
                sb.append(ObjectReferencePathSegment.SYMBOL);
            } else if (ParentPathSegment.QNAME.equals(qname)) {
                sb.append(ParentPathSegment.SYMBOL);
            } else if (IdentifierPathSegment.QNAME.equals(qname)) {
                sb.append(IdentifierPathSegment.SYMBOL);
            } else if (!StringUtils.isEmpty(qname.getPrefix())) {
                sb.append(qname.getPrefix()).append(':').append(qname.getLocalPart());
            } else {
                if (StringUtils.isNotEmpty(qname.getNamespaceURI())) {
                    String prefix = GlobalDynamicNamespacePrefixMapper
                            .getPreferredPrefix(qname.getNamespaceURI());
                    seg.setQNamePrefix(prefix); // hack - we modify the path segment here (only the form, not the meaning), but nevertheless it's ugly
                    sb.append(seg.getQName().getPrefix()).append(':').append(seg.getQName().getLocalPart());
                } else {
                    // no namespace, no prefix
                    sb.append(qname.getLocalPart());
                }
            }
        }
    }
}