Search in sources :

Example 1 with W3CNamespaceContext

use of org.apache.cxf.staxutils.W3CNamespaceContext in project cxf by apache.

the class JAXBEncoderDecoder method findExtraNamespaces.

private static XMLStreamReader findExtraNamespaces(XMLStreamReader source) {
    // due to a deficiency in the Stax API, there isn't a way to get all
    // the namespace prefixes that are "valid" at this point.  Thus, JAXB
    // cannot set all the prefixes into the validator (which also doesn't allow
    // setting a NSContext, just allows declaring of prefixes) so resolving
    // prefixes and such will fail if they were declared on any of the parent
    // elements.
    // 
    // We'll use some reflection to grab the known namespaces from woodstox
    // or the xerces parser and fake extra namespace decls on the root elements.
    // slight performance penalty, but there already is a penalty if you are validating
    // anyway.
    NamespaceContext c = source.getNamespaceContext();
    final Map<String, String> nsMap = new TreeMap<String, String>();
    try {
        if (c instanceof W3CNamespaceContext) {
            Element element = ((W3CNamespaceContext) c).getElement();
            while (element != null) {
                NamedNodeMap namedNodeMap = element.getAttributes();
                for (int i = 0; i < namedNodeMap.getLength(); i++) {
                    Attr attr = (Attr) namedNodeMap.item(i);
                    if (attr.getPrefix() != null && attr.getPrefix().equals("xmlns")) {
                        nsMap.put(attr.getLocalName(), attr.getValue());
                    }
                }
                element = (Element) element.getParentNode();
            }
        } else {
            try {
                // Woodstox version
                c = (NamespaceContext) c.getClass().getMethod("createNonTransientNsContext", Location.class).invoke(c, new Object[1]);
            } catch (Throwable t) {
            // ignore
            }
            Field f = ReflectionUtil.getDeclaredField(c.getClass(), "mNamespaces");
            ReflectionUtil.setAccessible(f);
            String[] ns = (String[]) f.get(c);
            for (int x = 0; x < ns.length; x += 2) {
                if (ns[x] == null) {
                    nsMap.put("", ns[x + 1]);
                } else {
                    nsMap.put(ns[x], ns[x + 1]);
                }
            }
        }
    } catch (Throwable t) {
        // internal JDK/xerces version
        try {
            Field f = ReflectionUtil.getDeclaredField(c.getClass(), "fNamespaceContext");
            ReflectionUtil.setAccessible(f);
            Object c2 = f.get(c);
            Enumeration<?> enm = (Enumeration<?>) c2.getClass().getMethod("getAllPrefixes").invoke(c2);
            while (enm.hasMoreElements()) {
                String s = (String) enm.nextElement();
                if (s == null) {
                    nsMap.put("", c.getNamespaceURI(null));
                } else {
                    nsMap.put(s, c.getNamespaceURI(s));
                }
            }
        } catch (Throwable t2) {
        // ignore
        }
    }
    if (!nsMap.isEmpty()) {
        for (int x = 0; x < source.getNamespaceCount(); x++) {
            String pfx = source.getNamespacePrefix(x);
            if (pfx == null) {
                nsMap.remove("");
            } else {
                nsMap.remove(pfx);
            }
        }
        if (!nsMap.isEmpty()) {
            @SuppressWarnings("unchecked") final Map.Entry<String, String>[] namespaces = nsMap.entrySet().toArray(new Map.Entry[nsMap.size()]);
            // OK. we have extra namespaces.  We'll need to wrapper the reader
            // with a new one that will fake extra namespace events
            source = new DepthXMLStreamReader(source) {

                public int getNamespaceCount() {
                    if (getDepth() == 0 && isStartElement()) {
                        return super.getNamespaceCount() + nsMap.size();
                    }
                    return super.getNamespaceCount();
                }

                public String getNamespacePrefix(int arg0) {
                    if (getDepth() == 0 && isStartElement()) {
                        int i = super.getNamespaceCount();
                        if (arg0 >= i) {
                            arg0 -= i;
                            return namespaces[arg0].getKey();
                        }
                    }
                    return super.getNamespacePrefix(arg0);
                }

                public String getNamespaceURI(int arg0) {
                    if (getDepth() == 0 && isStartElement()) {
                        int i = super.getNamespaceCount();
                        if (arg0 >= i) {
                            arg0 -= i;
                            return namespaces[arg0].getValue();
                        }
                    }
                    return super.getNamespaceURI(arg0);
                }
            };
        }
    }
    return source;
}
Also used : NamedNodeMap(org.w3c.dom.NamedNodeMap) Enumeration(java.util.Enumeration) XmlSchemaElement(org.apache.ws.commons.schema.XmlSchemaElement) JAXBElement(javax.xml.bind.JAXBElement) Element(org.w3c.dom.Element) DepthXMLStreamReader(org.apache.cxf.staxutils.DepthXMLStreamReader) TreeMap(java.util.TreeMap) Attr(org.w3c.dom.Attr) Field(java.lang.reflect.Field) W3CNamespaceContext(org.apache.cxf.staxutils.W3CNamespaceContext) NamespaceContext(javax.xml.namespace.NamespaceContext) W3CNamespaceContext(org.apache.cxf.staxutils.W3CNamespaceContext) Map(java.util.Map) NamedNodeMap(org.w3c.dom.NamedNodeMap) TreeMap(java.util.TreeMap) Location(javax.xml.stream.Location)

Example 2 with W3CNamespaceContext

use of org.apache.cxf.staxutils.W3CNamespaceContext in project cxf by apache.

the class JAXBDataBinding method addBindingFiles.

private void addBindingFiles(Options opts, List<InputSource> jaxbBindings, SchemaCollection schemas) {
    for (InputSource binding : jaxbBindings) {
        XMLStreamReader r = StaxUtils.createXMLStreamReader(binding);
        try {
            StaxUtils.toNextTag(r);
            String s = r.getAttributeValue(null, "schemaLocation");
            if (StringUtils.isEmpty(s)) {
                Document d = StaxUtils.read(r);
                XPath p = XPathFactory.newInstance().newXPath();
                p.setNamespaceContext(new W3CNamespaceContext(d.getDocumentElement()));
                XPathExpression xpe = p.compile(d.getDocumentElement().getAttribute("node"));
                for (XmlSchema schema : schemas.getXmlSchemas()) {
                    if (XMLConstants.W3C_XML_SCHEMA_NS_URI.equals(schema.getTargetNamespace())) {
                        continue;
                    }
                    Object src = getSchemaNode(schema, schemas);
                    NodeList nodes = (NodeList) xpe.evaluate(src, XPathConstants.NODESET);
                    if (nodes.getLength() > 0) {
                        String key = schema.getSourceURI();
                        binding = convertToTmpInputSource(d.getDocumentElement(), key);
                        opts.addBindFile(binding);
                        binding = null;
                    }
                }
            }
        } catch (Exception ex) {
        // ignore, just pass to jaxb
        } finally {
            try {
                r.close();
            } catch (Exception ex) {
            // ignore
            }
        }
        if (binding != null) {
            opts.addBindFile(binding);
        }
    }
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpression(javax.xml.xpath.XPathExpression) InputSource(org.xml.sax.InputSource) XMLStreamReader(javax.xml.stream.XMLStreamReader) W3CNamespaceContext(org.apache.cxf.staxutils.W3CNamespaceContext) XmlSchema(org.apache.ws.commons.schema.XmlSchema) NodeList(org.w3c.dom.NodeList) Document(org.w3c.dom.Document) XMLStreamException(javax.xml.stream.XMLStreamException) IOException(java.io.IOException) ToolException(org.apache.cxf.tools.common.ToolException) SAXException(org.xml.sax.SAXException) XmlSchemaSerializerException(org.apache.ws.commons.schema.XmlSchemaSerializer.XmlSchemaSerializerException) DOMException(org.w3c.dom.DOMException) BadCommandLineException(com.sun.tools.xjc.BadCommandLineException) SAXParseException(org.xml.sax.SAXParseException)

Example 3 with W3CNamespaceContext

use of org.apache.cxf.staxutils.W3CNamespaceContext in project cxf by apache.

the class ClientServerRPCLitTest method testDispatchClient.

@Test
public void testDispatchClient() throws Exception {
    SOAPServiceRPCLit service = new SOAPServiceRPCLit();
    Dispatch<Source> disp = service.createDispatch(portName, Source.class, javax.xml.ws.Service.Mode.PAYLOAD);
    updateAddressPort(disp, PORT);
    String req = "<ns1:sendReceiveData xmlns:ns1=\"http://apache.org/hello_world_rpclit\">" + "<in xmlns:ns2=\"http://apache.org/hello_world_rpclit/types\">" + "<ns2:elem1>elem1</ns2:elem1><ns2:elem2>elem2</ns2:elem2><ns2:elem3>45</ns2:elem3>" + "</in></ns1:sendReceiveData>";
    Source source = new StreamSource(new StringReader(req));
    Source resp = disp.invoke(source);
    assertNotNull(resp);
    Node nd = StaxUtils.read(resp);
    if (nd instanceof Document) {
        nd = ((Document) nd).getDocumentElement();
    }
    XPathUtils xpu = new XPathUtils(new W3CNamespaceContext((Element) nd));
    assertTrue(xpu.isExist("/ns1:sendReceiveDataResponse/out", nd, XPathConstants.NODE));
}
Also used : SOAPServiceRPCLit(org.apache.hello_world_rpclit.SOAPServiceRPCLit) W3CNamespaceContext(org.apache.cxf.staxutils.W3CNamespaceContext) XPathUtils(org.apache.cxf.helpers.XPathUtils) StreamSource(javax.xml.transform.stream.StreamSource) Node(org.w3c.dom.Node) XmlRootElement(javax.xml.bind.annotation.XmlRootElement) Element(org.w3c.dom.Element) StringReader(java.io.StringReader) Document(org.w3c.dom.Document) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) Test(org.junit.Test)

Example 4 with W3CNamespaceContext

use of org.apache.cxf.staxutils.W3CNamespaceContext in project cxf by apache.

the class SAAJStreamWriter method getPrefix.

@Override
public String getPrefix(String nsuri) throws XMLStreamException {
    if (isOverlaid && part != null && getCurrentNode() == null) {
        Node nd = part.getFirstChild();
        while (nd != null) {
            if (nd instanceof Element) {
                Iterator<String> it = new W3CNamespaceContext((Element) nd).getPrefixes(nsuri);
                if (it.hasNext()) {
                    return it.next();
                }
                nd = null;
            } else {
                nd = nd.getNextSibling();
            }
        }
    }
    return super.getPrefix(nsuri);
}
Also used : W3CNamespaceContext(org.apache.cxf.staxutils.W3CNamespaceContext) Node(org.w3c.dom.Node) SOAPElement(javax.xml.soap.SOAPElement) Element(org.w3c.dom.Element)

Aggregations

W3CNamespaceContext (org.apache.cxf.staxutils.W3CNamespaceContext)4 Element (org.w3c.dom.Element)3 Document (org.w3c.dom.Document)2 Node (org.w3c.dom.Node)2 BadCommandLineException (com.sun.tools.xjc.BadCommandLineException)1 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 Field (java.lang.reflect.Field)1 Enumeration (java.util.Enumeration)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 JAXBElement (javax.xml.bind.JAXBElement)1 XmlRootElement (javax.xml.bind.annotation.XmlRootElement)1 NamespaceContext (javax.xml.namespace.NamespaceContext)1 SOAPElement (javax.xml.soap.SOAPElement)1 Location (javax.xml.stream.Location)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 XMLStreamReader (javax.xml.stream.XMLStreamReader)1 Source (javax.xml.transform.Source)1 StreamSource (javax.xml.transform.stream.StreamSource)1