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:org.apache.synapse.commons.json.XmlReaderDelegate.java

public QName getName() {
    QName qName = super.getName();
    String localName = qName.getLocalPart();
    QName newName = qName;/*from  w  ww. j  a  v  a 2s .  c om*/
    if (localName == null || "".equals(localName)) {
        return qName;
    }
    boolean checked = false;
    String subStr;
    if (localName.charAt(0) == Constants.C_USOCRE) {
        if (localName.startsWith(Constants.PRECEDING_DIGIT)) {
            subStr = localName.substring(Constants.PRECEDING_DIGIT.length(), localName.length());
            if (processNCNames) {
                localName = toOrigJsonKey(subStr);
                checked = true;
            } else {
                localName = subStr;
            }
            newName = new QName(qName.getNamespaceURI(), localName, qName.getPrefix());
        } else if (localName.startsWith(Constants.PRECEDING_DOLLOR)) {
            subStr = localName.substring(Constants.PRECEDING_DOLLOR.length(), localName.length());
            if (processNCNames) {
                localName = (char) Constants.C_DOLLOR + toOrigJsonKey(subStr);
                checked = true;
            } else {
                localName = (char) Constants.C_DOLLOR + subStr;
            }
            newName = new QName(qName.getNamespaceURI(), localName, qName.getPrefix());
        }
    }
    if (!checked && processNCNames) {
        String newNameP = toOrigJsonKey(localName);
        if (!localName.equals(newNameP)) {
            newName = new QName(qName.getNamespaceURI(), newNameP, qName.getPrefix());
        }
    }
    if (logger.isDebugEnabled()) {
        logger.debug("#getName. old=" + localName + ", new=" + newName.getLocalPart());
    }
    return newName;
}

From source file:org.apache.synapse.config.xml.HeaderMediatorSerializer.java

public OMElement serializeMediator(OMElement parent, Mediator m) {

    if (!(m instanceof HeaderMediator)) {
        handleException("Unsupported mediator passed in for serialization : " + m.getType());
    }/*from www.  ja va 2  s. com*/

    HeaderMediator mediator = (HeaderMediator) m;
    OMElement header = fac.createOMElement("header", synNS);
    finalizeSerialization(header, mediator);

    QName qName = mediator.getQName();
    if (qName != null) {
        if (qName.getNamespaceURI() != null) {
            header.addAttribute(fac.createOMAttribute("name", nullNS,
                    (qName.getPrefix() != null && !"".equals(qName.getPrefix()) ? qName.getPrefix() + ":" : "")
                            + qName.getLocalPart()));
            header.declareNamespace(qName.getNamespaceURI(), qName.getPrefix());
        } else {
            header.addAttribute(fac.createOMAttribute("name", nullNS, qName.getLocalPart()));
        }
    }

    if (mediator.getAction() == HeaderMediator.ACTION_REMOVE) {
        header.addAttribute(fac.createOMAttribute("action", nullNS, "remove"));
    } else {
        if (mediator.getValue() != null) {
            header.addAttribute(fac.createOMAttribute("value", nullNS, mediator.getValue()));

        } else if (mediator.getExpression() != null) {
            header.addAttribute(
                    fac.createOMAttribute("expression", nullNS, mediator.getExpression().toString()));
            super.serializeNamespaces(header, mediator.getExpression());

        } else {
            handleException("Value or expression required for a set header mediator");
        }
    }

    if (parent != null) {
        parent.addChild(header);
    }
    return header;
}

From source file:org.apache.vxquery.xmlquery.translator.XMLQueryTranslator.java

private ILogicalExpression ce(SequenceType type, Object value) throws SystemException {
    try {/* ww  w.j a va  2 s . com*/
        ItemType it = type.getItemType();
        if (it.isAtomicType()) {
            AtomicType at = (AtomicType) it;
            byte[] bytes = null;
            switch (at.getTypeId()) {
            case BuiltinTypeConstants.XS_BOOLEAN_TYPE_ID: {
                baaos.reset();
                dOut.write((byte) ValueTag.XS_BOOLEAN_TAG);
                dOut.writeByte(((Boolean) value).booleanValue() ? 1 : 0);
                break;
            }
            case BuiltinTypeConstants.XS_INT_TYPE_ID: {
                baaos.reset();
                dOut.write((byte) ValueTag.XS_INT_TAG);
                dOut.writeInt(((Number) value).intValue());
                break;
            }
            case BuiltinTypeConstants.XS_INTEGER_TYPE_ID: {
                baaos.reset();
                dOut.write((byte) ValueTag.XS_INTEGER_TAG);
                dOut.writeLong(((Number) value).longValue());
                break;
            }
            case BuiltinTypeConstants.XS_DOUBLE_TYPE_ID: {
                baaos.reset();
                dOut.write((byte) ValueTag.XS_DOUBLE_TAG);
                dOut.writeDouble(((Number) value).doubleValue());
                break;
            }
            case BuiltinTypeConstants.XS_STRING_TYPE_ID: {
                baaos.reset();
                dOut.write((byte) ValueTag.XS_STRING_TAG);
                stringVB.write((CharSequence) value, dOut);
                break;
            }
            case BuiltinTypeConstants.XS_DECIMAL_TYPE_ID: {
                baaos.reset();
                // TODO Remove the creation of the separate byte array.
                DoublePointable doublep = (DoublePointable) DoublePointable.FACTORY.createPointable();
                doublep.set(new byte[DoublePointable.TYPE_TRAITS.getFixedLength()], 0,
                        DoublePointable.TYPE_TRAITS.getFixedLength());
                doublep.setDouble(((Number) value).doubleValue());
                CastToDecimalOperation castToDecimal = new CastToDecimalOperation();
                castToDecimal.convertDouble(doublep, dOut);
                break;
            }
            case BuiltinTypeConstants.XS_QNAME_TYPE_ID: {
                QName qname = (QName) value;
                baaos.reset();
                dOut.write((byte) ValueTag.XS_QNAME_TAG);
                stringVB.write(qname.getNamespaceURI(), dOut);
                stringVB.write(qname.getPrefix(), dOut);
                stringVB.write(qname.getLocalPart(), dOut);
                break;
            }
            case BuiltinTypeConstants.XS_UNTYPED_ATOMIC_TYPE_ID: {
                baaos.reset();
                dOut.write((byte) ValueTag.XS_UNTYPED_ATOMIC_TAG);
                stringVB.write((CharSequence) value, dOut);
                break;
            }
            default:
                throw new SystemException(ErrorCode.SYSE0001);
            }
            bytes = Arrays.copyOf(baaos.getByteArray(), baaos.size());
            return new ConstantExpression(new VXQueryConstantValue(type, bytes));
        }
        throw new UnsupportedOperationException();
    } catch (IOException e) {
        throw new SystemException(ErrorCode.SYSE0001, e);
    }
}

From source file:org.apache.ws.security.saml.ext.OpenSAMLUtil.java

/**
 * Method buildSignature ...//from   ww w  .  j av  a2  s .  c o m
 *
 * @return Signature
 */
@SuppressWarnings("unchecked")
public static Signature buildSignature() {
    QName qName = Signature.DEFAULT_ELEMENT_NAME;
    XMLObjectBuilder<Signature> builder = builderFactory.getBuilder(qName);
    if (builder == null) {
        LOG.error("Unable to retrieve builder for object QName " + qName);
        return null;
    }
    return (Signature) builder.buildObject(qName.getNamespaceURI(), qName.getLocalPart(), qName.getPrefix());
}

From source file:org.apereo.portal.portlet.registry.PortletWindowRegistryImpl.java

protected StartElement addPortletWindowId(StartElement element, IPortletWindowId portletWindowId) {
    final Attribute windowIdAttribute = xmlEventFactory.createAttribute(PORTLET_WINDOW_ID_ATTR_NAME,
            portletWindowId.getStringId());

    //Clone the start element to add the new attribute
    final QName name = element.getName();
    final String prefix = name.getPrefix();
    final String namespaceURI = name.getNamespaceURI();
    final String localPart = name.getLocalPart();
    @SuppressWarnings("unchecked")
    final Iterator<Attribute> attributes = element.getAttributes();
    @SuppressWarnings("unchecked")
    final Iterator<Namespace> namespaces = element.getNamespaces();
    final NamespaceContext namespaceContext = element.getNamespaceContext();

    //Create a new iterator of the existing attributes + the new window id attribute
    final Iterator<Attribute> newAttributes = Iterators.concat(attributes,
            Iterators.forArray(windowIdAttribute));

    return xmlEventFactory.createStartElement(prefix, namespaceURI, localPart, newAttributes, namespaces,
            namespaceContext);//from ww w  .j  av  a  2  s  . co m
}

From source file:org.codice.ddf.spatial.ogc.csw.catalog.converter.CswMarshallHelper.java

static void writeValue(HierarchicalStreamWriter writer, MarshallingContext context,
        AttributeDescriptor attributeDescriptor, QName field, Serializable value) {

    String xmlValue = null;/*from  w  w w  .ja v a 2  s.  com*/
    AttributeType.AttributeFormat attrFormat = null;

    if (attributeDescriptor != null && attributeDescriptor.getType() != null) {
        attrFormat = attributeDescriptor.getType().getAttributeFormat();
    }

    if (attrFormat == null) {
        attrFormat = AttributeType.AttributeFormat.STRING;
    }

    String name = null;

    if (!StringUtils.isBlank(field.getNamespaceURI())) {
        if (!StringUtils.isBlank(field.getPrefix())) {
            name = field.getPrefix() + CswConstants.NAMESPACE_DELIMITER + field.getLocalPart();
        } else {
            name = field.getLocalPart();
        }
    } else {
        name = field.getLocalPart();
    }

    switch (attrFormat) {
    case BINARY:
        xmlValue = Base64.getEncoder().encodeToString((byte[]) value);
        break;
    case DATE:
        GregorianCalendar cal = new GregorianCalendar();
        cal.setTime((Date) value);
        xmlValue = XSD_FACTORY.newXMLGregorianCalendar(cal).toXMLFormat();
        break;
    case OBJECT:
        break;
    case GEOMETRY:
    case XML:
    default:
        xmlValue = value.toString();
        break;
    }

    // Write the node if we were able to convert it.
    if (xmlValue != null) {
        writer.startNode(name);

        if (!StringUtils.isBlank(field.getNamespaceURI())) {
            if (StringUtils.isBlank(field.getPrefix())) {
                writer.addAttribute(XMLConstants.XMLNS_ATTRIBUTE, field.getNamespaceURI());
            }
        }

        writer.setValue(xmlValue);
        writer.endNode();
    }
}

From source file:org.codice.ddf.spatial.ogc.csw.catalog.converter.impl.CswRecordConverter.java

private void writeValue(HierarchicalStreamWriter writer, MarshallingContext context,
        AttributeDescriptor attributeDescriptor, QName field, Serializable value) {

    String xmlValue = null;//from  www.  j  ava 2s . co m

    AttributeFormat attrFormat = null;
    if (attributeDescriptor != null && attributeDescriptor.getType() != null) {
        attrFormat = attributeDescriptor.getType().getAttributeFormat();
    }
    if (attrFormat == null) {
        attrFormat = AttributeFormat.STRING;
    }
    String name = null;
    if (!StringUtils.isBlank(field.getNamespaceURI())) {
        if (!StringUtils.isBlank(field.getPrefix())) {
            name = field.getPrefix() + CswConstants.NAMESPACE_DELIMITER + field.getLocalPart();
        } else {
            name = field.getLocalPart();
        }
    } else {
        name = field.getLocalPart();
    }
    switch (attrFormat) {
    case BINARY:
        xmlValue = Base64.encodeBase64String((byte[]) value);
        break;
    case DATE:
        GregorianCalendar cal = new GregorianCalendar();
        cal.setTime((Date) value);
        xmlValue = XSD_FACTORY.newXMLGregorianCalendar(cal).toXMLFormat();
        break;
    case OBJECT:
        break;
    case GEOMETRY:
    case XML:
    default:
        xmlValue = value.toString();
        break;
    }
    // Write the node if we were able to convert it.
    if (xmlValue != null) {
        writer.startNode(name);
        if (!StringUtils.isBlank(field.getNamespaceURI())) {
            if (!StringUtils.isBlank(field.getPrefix())) {
                writeNamespace(writer, field);
            } else {
                writer.addAttribute(XMLConstants.XMLNS_ATTRIBUTE, field.getNamespaceURI());
            }
        }

        writer.setValue(xmlValue);
        writer.endNode();
    }
}

From source file:org.codice.ddf.spatial.ogc.csw.catalog.converter.impl.CswRecordConverter.java

private void writeNamespace(HierarchicalStreamWriter writer, QName field) {
    if (prefixToUriMapping == null || !prefixToUriMapping.containsKey(field.getPrefix())
            || !prefixToUriMapping.get(field.getPrefix()).equals(field.getNamespaceURI())) {
        writer.addAttribute(XMLConstants.XMLNS_ATTRIBUTE + CswConstants.NAMESPACE_DELIMITER + field.getPrefix(),
                field.getNamespaceURI());
    }//from  w  w  w  .  j  a  v  a 2  s  .c  o m
}

From source file:org.codice.ddf.spatial.ogc.wfs.catalog.converter.impl.GenericFeatureConverter.java

/**
 * This method will convert a {@link Metacard} instance into xml that will validate against the
 * GML 2.1.2 AbstractFeatureType.//w  w  w  .j a v  a 2s.  c  o  m
 *
 * @param value
 *            the {@link Metacard} to convert
 * @param writer
 *            the stream writer responsible for writing this xml doc
 * @param context
 *            a reference back to the Xstream marshalling context. Allows you to call
 *            "convertAnother" which will lookup other registered converters.
 */
@Override
public void marshal(Object value, HierarchicalStreamWriter writer, MarshallingContext context) {
    Metacard metacard = (Metacard) value;

    // TODO when we have a reference to the MCT we can get the namespace too
    QName qname = WfsQnameBuilder.buildQName(metacard.getMetacardType().getName(),
            metacard.getContentTypeName());

    writer.startNode(qname.getPrefix() + ":" + qname.getLocalPart());

    // Add the "fid" attribute if we have an ID
    String fid = (String) metacard.getAttribute(Metacard.ID).getValue();
    if (fid != null) {
        writer.addAttribute(FID, fid);
    }

    if (null != metacard.getLocation()) {
        Geometry geo = XmlNode.readGeometry(metacard.getLocation());
        if (geo != null && !geo.isEmpty()) {
            XmlNode.writeEnvelope(WfsConstants.GML_PREFIX + ":" + "boundedBy", context, writer,
                    geo.getEnvelopeInternal());
        }
    }

    Set<AttributeDescriptor> descriptors = new TreeSet<AttributeDescriptor>(
            new AttributeDescriptorComparator());
    descriptors.addAll(metacard.getMetacardType().getAttributeDescriptors());

    for (AttributeDescriptor attributeDescriptor : descriptors) {
        Attribute attribute = metacard.getAttribute(attributeDescriptor.getName());
        if (attribute != null) {
            writeAttributeToXml(attribute, qname, attributeDescriptor.getType().getAttributeFormat(), context,
                    writer);
        }
    }
    writer.endNode();
}

From source file:org.codice.ddf.spatial.ogc.wfs.catalog.converter.impl.GenericFeatureConverter.java

private void writeAttributeToXml(Attribute attribute, QName qname, AttributeFormat format,
        MarshallingContext context, HierarchicalStreamWriter writer) {
    // Loop to handle multi-valued attributes

    String name = qname.getPrefix() + ":" + attribute.getName();
    for (Serializable value : attribute.getValues()) {
        String xmlValue = null;/*  ww  w .  ja v a  2s  . c  o m*/

        switch (format) {
        case XML:
            String cdata = (String) value;
            if (cdata != null && (writer.underlyingWriter() instanceof EnhancedStaxWriter)) {
                writer.startNode(name);
                EnhancedStaxWriter eWriter = (EnhancedStaxWriter) writer.underlyingWriter();
                eWriter.writeCdata(cdata);
                writer.endNode();
            }
            break;
        case GEOMETRY:
            XmlNode.writeGeometry(name, context, writer, XmlNode.readGeometry((String) value));
            break;
        case BINARY:
            xmlValue = Base64.getEncoder().encodeToString((byte[]) value);
            break;
        case DATE:
            Date date = (Date) value;
            xmlValue = DateFormatUtils.formatUTC(date,
                    DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.getPattern());
            break;
        case OBJECT:
            // Probably won't translate at all.
            break;
        default:
            xmlValue = value.toString();
            break;
        }
        // Write the node if we were able to convert it.
        if (xmlValue != null) {

            writer.startNode(name);
            writer.setValue(xmlValue);
            writer.endNode();
        }
    }
}