javax.xml.namespace.NamespaceContext Java Examples

The following examples show how to use javax.xml.namespace.NamespaceContext. 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: Internalizer.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
private NodeList getWSDLDefintionNode(Node bindings, Node target) {
    return evaluateXPathMultiNode(bindings, target, "wsdl:definitions",
            new NamespaceContext() {
                @Override
                public String getNamespaceURI(String prefix) {
                    return "http://schemas.xmlsoap.org/wsdl/";
                }

                @Override
                public String getPrefix(String nsURI) {
                    throw new UnsupportedOperationException();
                }

                @Override
                public Iterator getPrefixes(String namespaceURI) {
                    throw new UnsupportedOperationException();
                }
            });
}
 
Example #2
Source File: UnmarshallingContext.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void startDocument(LocatorEx locator, NamespaceContext nsContext) throws SAXException {
    if(locator!=null)
        this.locator = locator;
    this.environmentNamespaceContext = nsContext;
    // reset the object
    result = null;
    current = root;

    patchersLen=0;
    aborted = false;
    isUnmarshalInProgress = true;
    nsLen=0;

    if(expectedType!=null)
        root.loader = EXPECTED_TYPE_ROOT_LOADER;
    else
        root.loader = DEFAULT_ROOT_LOADER;

    idResolver.startDocument(this);
}
 
Example #3
Source File: StAXPostInitAction.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public void run() {
    NamespaceContext ns = nsc;
    if(xsw!=null)   ns = xsw.getNamespaceContext();
    if(xew!=null)   ns = xew.getNamespaceContext();

    // StAX javadoc isn't very clear on the behavior,
    // so work defensively in anticipation of broken implementations.
    if(ns==null)
        return;

    // we can't enumerate all the in-scope namespace bindings in StAX,
    // so we only look for the known static namespace URIs.
    // this is less than ideal, but better than nothing.
    for( String nsUri : serializer.grammar.nameList.namespaceURIs ) {
        String p = ns.getPrefix(nsUri);
        if(p!=null)
            serializer.addInscopeBinding(nsUri,p);
    }
}
 
Example #4
Source File: UnmarshallingContext.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void startDocument(LocatorEx locator, NamespaceContext nsContext) throws SAXException {
    if(locator!=null)
        this.locator = locator;
    this.environmentNamespaceContext = nsContext;
    // reset the object
    result = null;
    current = root;

    patchersLen=0;
    aborted = false;
    isUnmarshalInProgress = true;
    nsLen=0;

    if(expectedType!=null)
        root.loader = EXPECTED_TYPE_ROOT_LOADER;
    else
        root.loader = DEFAULT_ROOT_LOADER;

    idResolver.startDocument(this);
}
 
Example #5
Source File: XPathExpressionWrapper.java    From XACML with MIT License 6 votes vote down vote up
public synchronized XPathExpression getXpathExpressionWrapped() {
	if (this.xpathExpressionWrapped == null && (this.getStatus() == null || this.getStatus().isOk())) {
		String thisPath	= this.getPath();
		if (thisPath != null) {
			XPath xPath	= XPathFactory.newInstance().newXPath();
			NamespaceContext namespaceContextThis	= this.getNamespaceContext();
			if (namespaceContextThis != null) {
				xPath.setNamespaceContext(namespaceContextThis);
			}
			try {
				this.xpathExpressionWrapped	= xPath.compile(thisPath);
			} catch (XPathExpressionException ex) {
				this.status	= new StdStatus(StdStatusCode.STATUS_CODE_SYNTAX_ERROR, "Error compiling XPath " + thisPath + ": " + ex.getMessage());
			}
		}
	}
	return this.xpathExpressionWrapped;
}
 
Example #6
Source File: XPathHelper.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
/**
 * Create a new {@link XPath} without any special settings using the default
 * {@link XPathFactory}.
 *
 * @return The created non-<code>null</code> {@link XPath} object
 */
@Nonnull
public static XPath createNewXPath ()
{
  return createNewXPath (s_aXPathFactory,
                         (XPathVariableResolver) null,
                         (XPathFunctionResolver) null,
                         (NamespaceContext) null);
}
 
Example #7
Source File: SchemaSetImpl.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public Collection<XSComponent> select(String scd, NamespaceContext nsContext) {
    try {
        return SCD.create(scd,nsContext).select(this);
    } catch (ParseException e) {
        throw new IllegalArgumentException(e);
    }
}
 
Example #8
Source File: SchemaImpl.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
public Collection<XSComponent> select(String scd, NamespaceContext nsContext) {
    try {
        return SCD.create(scd,nsContext).select(this);
    } catch (ParseException e) {
        throw new IllegalArgumentException(e);
    }
}
 
Example #9
Source File: SchemaSetImpl.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
public Collection<XSComponent> select(String scd, NamespaceContext nsContext) {
    try {
        return SCD.create(scd,nsContext).select(this);
    } catch (ParseException e) {
        throw new IllegalArgumentException(e);
    }
}
 
Example #10
Source File: XPathHelper.java    From hsac-fitnesse-fixtures with Apache License 2.0 5 votes vote down vote up
protected XPathExpression createXPathExpression(NamespaceContext context, String xPathExpr) {
    XPath xpath = createXPath(context);
    try {
        return xpath.compile(xPathExpr);
    } catch (XPathExpressionException e) {
        String msg = getMessage(e);
        throw new FitFailureException("Unable to compile xpath: " + xPathExpr + "\n" + msg);
    }
}
 
Example #11
Source File: Bridge.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @since 2.0.2
 */
public void marshal(T object,OutputStream output, NamespaceContext nsContext, AttachmentMarshaller am) throws JAXBException {
    Marshaller m = context.marshallerPool.take();
    m.setAttachmentMarshaller(am);
    marshal(m,object,output,nsContext);
    m.setAttachmentMarshaller(null);
    context.marshallerPool.recycle(m);
}
 
Example #12
Source File: DatatypeConverterImpl.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public static String _printQName(QName val, NamespaceContext nsc) {
    // Double-check
    String qname;
    String prefix = nsc.getPrefix(val.getNamespaceURI());
    String localPart = val.getLocalPart();

    if (prefix == null || prefix.length() == 0) { // be defensive
        qname = localPart;
    } else {
        qname = prefix + ':' + localPart;
    }

    return qname;
}
 
Example #13
Source File: WebXmlValidator.java    From google-cloud-eclipse with Apache License 2.0 5 votes vote down vote up
/**
 * Adds all defined servlet names to a set, then adds a
 * {@link ServletMappingElement} to the problems list for all
 * <servlet-mapping> elements whose <servlet-name> is undefined.
 */
private void validateServletMapping() {
  try {
    XPath xPath = FACTORY.newXPath();
    NamespaceContext nsContext = new JavaContext();
    xPath.setNamespaceContext(nsContext);
    String selectServletNames = "//prefix:servlet/prefix:servlet-name";
    NodeList servletNameNodes = (NodeList) xPath
        .compile(selectServletNames)
        .evaluate(document, XPathConstants.NODESET);
    Set<String> servletNames = new HashSet<>();
    for (int i = 0; i < servletNameNodes.getLength(); i++) {
      String servletName = servletNameNodes.item(i).getTextContent();
      servletNames.add(servletName);
    }
    String selectServletMappings = "//prefix:servlet-mapping/prefix:servlet-name";
    NodeList servletMappings = (NodeList) xPath
        .compile(selectServletMappings)
        .evaluate(document, XPathConstants.NODESET);
    for (int i = 0; i < servletMappings.getLength(); i++) {
      Node servletMapping = servletMappings.item(i);
      String textContent = servletMapping.getTextContent();
      if (!servletNames.contains(textContent)) {
        DocumentLocation location = (DocumentLocation) servletMapping.getUserData("location");
        ElementProblem element =
            new ServletMappingElement(textContent, location, textContent.length());
        problems.add(element);
      }
    }
  } catch (XPathExpressionException ex) {
    throw new RuntimeException("Invalid XPath expression");
  }
}
 
Example #14
Source File: SchemaImpl.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public XSComponent selectSingle(String scd, NamespaceContext nsContext) {
    try {
        return SCD.create(scd,nsContext).selectSingle(this);
    } catch (ParseException e) {
        throw new IllegalArgumentException(e);
    }
}
 
Example #15
Source File: BaseNsStreamWriter.java    From woodstox with Apache License 2.0 5 votes vote down vote up
/**
 *<p>
 * Note: Root namespace context works best if automatic prefix
 * creation ("namespace/prefix repairing" in StAX lingo) is enabled.
 */
@Override
public void setNamespaceContext(NamespaceContext ctxt)
    throws XMLStreamException
{
    // This is only allowed before root element output:
    if (mState != STATE_PROLOG) {
        throwOutputError("Called setNamespaceContext() after having already output root element.");
    }

    mRootNsContext = ctxt;
    mCurrElem.setRootNsContext(ctxt);
}
 
Example #16
Source File: MarshallerBridge.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public void marshal(Marshaller m, Object object, OutputStream output, NamespaceContext nsContext) throws JAXBException {
    m.setProperty(Marshaller.JAXB_FRAGMENT,true);
    try {
        ((MarshallerImpl)m).marshal(object,output,nsContext);
    } finally {
        m.setProperty(Marshaller.JAXB_FRAGMENT,false);
    }
}
 
Example #17
Source File: Bridge.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @since 2.0.2
 */
public void marshal(T object,OutputStream output, NamespaceContext nsContext, AttachmentMarshaller am) throws JAXBException {
    Marshaller m = context.marshallerPool.take();
    m.setAttachmentMarshaller(am);
    marshal(m,object,output,nsContext);
    m.setAttachmentMarshaller(null);
    context.marshallerPool.recycle(m);
}
 
Example #18
Source File: XPathExpressionHelper.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Nullable
public static Boolean evalXPathToBoolean (@Nullable final XPathVariableResolver aVariableResolver,
                                          @Nullable final XPathFunctionResolver aFunctionResolver,
                                          @Nullable final NamespaceContext aNamespaceContext,
                                          @Nonnull final String sXPath,
                                          @Nonnull final Document aDoc)
{
  return evalXPathToBoolean (XPathHelper.createNewXPath (aVariableResolver, aFunctionResolver, aNamespaceContext),
                             sXPath,
                             aDoc);
}
 
Example #19
Source File: NamespaceContextWrapper.java    From JDKSourceCode1.8 with MIT License 4 votes vote down vote up
/**
 * This method supports all functions in the NamespaceContext utility class
 */
public com.sun.org.apache.xerces.internal.xni.NamespaceContext getNamespaceContext() {
    return fNamespaceContext;
}
 
Example #20
Source File: WrapperBridge.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
@Override
    public void marshal(T object, OutputStream output, NamespaceContext nsContext, AttachmentMarshaller am) throws JAXBException {
//      bridge.marshal((T) convert(object), output, nsContext, am);
    }
 
Example #21
Source File: RepeatedElementBridge.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void marshal(T object, OutputStream output, NamespaceContext nsContext, AttachmentMarshaller am) throws JAXBException {
    delegate.marshal(object, output, nsContext, am);
}
 
Example #22
Source File: XmlEntryConsumer.java    From cloud-odata-java with Apache License 2.0 4 votes vote down vote up
private void readCustomElement(final XMLStreamReader reader, final String tagName, final EntityInfoAggregator eia) throws EdmException, EntityProviderException, XMLStreamException {
  EntityPropertyInfo targetPathInfo = eia.getTargetPathInfo(tagName);
  NamespaceContext nsctx = reader.getNamespaceContext();

  boolean skipTag = true;
  if (!Edm.NAMESPACE_ATOM_2005.equals(reader.getName().getNamespaceURI())) {

    if (targetPathInfo != null) {
      final String customPrefix = targetPathInfo.getCustomMapping().getFcNsPrefix();
      final String customNamespaceURI = targetPathInfo.getCustomMapping().getFcNsUri();

      if (customPrefix != null && customNamespaceURI != null) {
        String xmlPrefix = nsctx.getPrefix(customNamespaceURI);
        String xmlNamespaceUri = reader.getNamespaceURI(customPrefix);

        if (customNamespaceURI.equals(xmlNamespaceUri) && customPrefix.equals(xmlPrefix)) {
          skipTag = false;
          reader.require(XMLStreamConstants.START_ELEMENT, customNamespaceURI, tagName);
          reader.next();
          reader.require(XMLStreamConstants.CHARACTERS, null, null);
          final String text = reader.getText();
          reader.nextTag();
          reader.require(XMLStreamConstants.END_ELEMENT, customNamespaceURI, tagName);

          final EntityPropertyInfo propertyInfo = getValidatedPropertyInfo(eia, tagName);
          final Class<?> typeMapping = typeMappings.getMappingClass(propertyInfo.getName());
          final EdmSimpleType type = (EdmSimpleType) propertyInfo.getType();
          final Object value = type.valueOfString(text, EdmLiteralKind.DEFAULT, propertyInfo.getFacets(),
              typeMapping == null ? type.getDefaultType() : typeMapping);
          properties.put(tagName, value);
        }
      }
    } else {
      throw new EntityProviderException(EntityProviderException.INVALID_PROPERTY.addContent(tagName));
    }
  }

  if (skipTag) {
    skipStartedTag(reader);
  }
}
 
Example #23
Source File: CMElementInfo.java    From jaxb2-basics with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public NamespaceContext getDefaultValueNamespaceContext() {
	return defaultValueNamespaceContext;
}
 
Example #24
Source File: DOMStreamReader.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
public NamespaceContext getNamespaceContext() {
    return this;
}
 
Example #25
Source File: SaajStaxWriter.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void setNamespaceContext(final NamespaceContext context)throws XMLStreamException {
    throw new UnsupportedOperationException();
}
 
Example #26
Source File: InterningXmlVisitor.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
public void startDocument(LocatorEx locator, NamespaceContext nsContext) throws SAXException {
    next.startDocument(locator,nsContext);
}
 
Example #27
Source File: WrapperBridge.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void marshal(T object, OutputStream output, NamespaceContext nsContext, AttachmentMarshaller am) throws JAXBException {
    bridge.marshal((T) convert(object), output, nsContext, am);
}
 
Example #28
Source File: AbstractPolicySecurityTest.java    From steady with Apache License 2.0 4 votes vote down vote up
protected void verifySignatureCoversAssertion(Document signedDoc, String assertionId) throws Exception {
    XPathFactory factory = XPathFactory.newInstance();
    XPath xpath = factory.newXPath();
    final NamespaceContext nsContext = this.getNamespaceContext();
    xpath.setNamespaceContext(nsContext);
    
    // Find the SecurityTokenReference for the assertion
    final XPathExpression strExpr = xpath.compile(
        "/s:Envelope/s:Header/wsse:Security/wsse:SecurityTokenReference/wsse:KeyIdentifier");
    
    final NodeList strKeyIdNodes = 
        (NodeList) strExpr.evaluate(signedDoc, XPathConstants.NODESET);
    
    String strId = null;
    for (int i = 0; i < strKeyIdNodes.getLength(); i++) {
        Node keyIdNode = (Node) strKeyIdNodes.item(i);
        String strKey = keyIdNode.getTextContent();
        if (strKey.equals(assertionId)) {
            Node strNode = (Node) keyIdNode.getParentNode();
            strId = strNode.getAttributes().
                getNamedItemNS(nsContext.getNamespaceURI("wsu"), "Id").getNodeValue();
            break;
        }
    }
    assertNotNull("SecurityTokenReference for " + assertionId + " not found in security header.", strId);
    
    // Verify STR is included in the signature references
    final XPathExpression sigRefExpr = xpath.compile(
        "/s:Envelope/s:Header/wsse:Security/ds:Signature/ds:SignedInfo/ds:Reference");
    
    final NodeList sigReferenceNodes = 
        (NodeList) sigRefExpr.evaluate(signedDoc, XPathConstants.NODESET);
    
    boolean foundStrReference = false;
    for (int i = 0; i < sigReferenceNodes.getLength(); i++) {
        Node sigRefNode = (Node) sigReferenceNodes.item(i);
        String sigRefURI = sigRefNode.getAttributes().getNamedItem("URI").getNodeValue();
        if (sigRefURI.equals("#" + strId)) {
            foundStrReference = true;
            break;
        }
    }
    
    assertTrue("SecurityTokenReference for " + assertionId + " is not signed.", foundStrReference);
}
 
Example #29
Source File: BadgerFishXMLStreamWriter.java    From jettison with Apache License 2.0 4 votes vote down vote up
public void setNamespaceContext(NamespaceContext context) throws XMLStreamException {
    this.ctx = context;
}
 
Example #30
Source File: DatatypeConverterImpl.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
/**
 * @return null if fails to convert.
 */
public static QName _parseQName(CharSequence text, NamespaceContext nsc) {
    int length = text.length();

    // trim whitespace
    int start = 0;
    while (start < length && WhiteSpaceProcessor.isWhiteSpace(text.charAt(start))) {
        start++;
    }

    int end = length;
    while (end > start && WhiteSpaceProcessor.isWhiteSpace(text.charAt(end - 1))) {
        end--;
    }

    if (end == start) {
        throw new IllegalArgumentException("input is empty");
    }


    String uri;
    String localPart;
    String prefix;

    // search ':'
    int idx = start + 1;    // no point in searching the first char. that's not valid.
    while (idx < end && text.charAt(idx) != ':') {
        idx++;
    }

    if (idx == end) {
        uri = nsc.getNamespaceURI("");
        localPart = text.subSequence(start, end).toString();
        prefix = "";
    } else {
        // Prefix exists, check everything
        prefix = text.subSequence(start, idx).toString();
        localPart = text.subSequence(idx + 1, end).toString();
        uri = nsc.getNamespaceURI(prefix);
        // uri can never be null according to javadoc,
        // but some users reported that there are implementations that return null.
        if (uri == null || uri.length() == 0) // crap. the NamespaceContext interface is broken.
        // error: unbound prefix
        {
            throw new IllegalArgumentException("prefix " + prefix + " is not bound to a namespace");
        }
    }

    return new QName(uri, localPart, prefix);
}