Showing posts with label XPath. Show all posts
Showing posts with label XPath. Show all posts

Saturday, September 15, 2012

Quick ways of doing code reviews and sanity checks in OSB

I have been asked to check that all log statements are done with "debug" level, as someone does it with "error" and then you end up with a lot of garbage in Production.

The stupid way is to click around.

The smart way is:

- export a sbconfig.jar with all the projects
- unzip the sbconfig.jar in a directory. You will notice that each Proxy is a XML file with .ProxyService extension
- open XMLSpy and open one of those .ProxyService xml file
- menu XML, Evaluate XPath
- enter //*:log[*:logLevel != 'debug' and *:expr/*:xqueryText != '$faultVar']
(normally we log with "error" level only the faultVar
- if you find NOTHING, then cool
- you can run the XPath on ALL files in a given folder (see "where" button in XMLSpy)

for more info: http://www.altova.com/xmlspy/xpath-analyzer.html


The way I ahve done it, use Everything Search to identify all .ProxyService files, copy them into a folder, drag and drop them in XmlSpy so that they are all open.

Select "Open Files" in the XPath Evaluation tab, and run the little "evaluate XPath" button.




Thursday, August 4, 2011

Java XPath evaluation

I have put together a simple XPath parser.

Everything is very easy as long as you don't use Namespaces.

Here is a very nice trick to extract a NamespaceContext from the document you are parsing.

The alternative is providing your own implementation of NamespaceContext, either this way (see "public class MyNamespaceContext") or as shown in this post with NamespaceContextMap class

My first instance was using:

Document xmlDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(xmlFile);

XPath xPath = XPathFactory.newInstance().newXPath();
NamespaceContext nsContext = new NamespaceContextMap("foo", "http://foo","bar", "http://bar", "def", "http://def");

xPath.setNamespaceContext(nsContext);
String expression = ".//con3:xqueryTransform/con3:resource[1]/@ref";
XPathExpression xPathExpression = xPath.compile(expression);
return xPathExpression.evaluate(xmlDocument, XPathConstants.STRING);




Well, it doesn' work. Using "Document xmlDocument" in xPathExpression.evaluate fails to interpret namespaces.

I had to convert it to:
InputSource inputSource = new InputSource(xmlFile);
...
return xPathExpression.evaluate(inputSource, XPathConstants.STRING);


to make it work.

Amazing.

2 hours wasted.

I love XML, Xpaths and so on. What a solid technology.

Thursday, July 14, 2011

OSB Custom XPath: XQuery expression validation failed: {err}FONS0003: : prefix not defined in static context

I have created a Custom XPath function, and the entry in osb-built-in.xml is:


            initTSSArray
            Fills the TSScache with all elements, using Id as a key
            http://acme.com/Nis
            com.acme.osb.CachesService
            java.lang.String initTSSArray(java.lang.String, [Lorg.apache.xmlbeans.XmlObject;)
            true
            Pipeline
            SplitJoin
        



When using the new XPath function, I have to precede it with the Namespace prefix generated by OSB :

nisnx:initTSSArray(...)


Problem is, in Eclipse this nisnx is not defined. So I add nisnx as a static prefix in the Proxy Workflow, and next thing I get is:

XQuery expression validation failed: {err}XQ0017: unknown function (or number of arguments (0) is wrong)

The problem now is how to register the custom function in Eclipse.

I will find out one day....


The official documentation says nothing on the topic:

http://download.oracle.com/docs/cd/E17904_01/doc.1111/e15866/custom_xpath.htm

It turns out that, if Eclipse is installed in
C:\Oracle4\Middleware\oepe_11gR1PS3

you must copy the XML and JAR file to
C:\Oracle4\Middleware\Oracle_OSB1\config\xpath-functions

at this point Eclipse will recognize the Custom XPath functions (after restart and clean).

My problem was that I had deployed the JAR and XML only to my remote installation, not to the local OSB installation. So Eclipse could not see the definition of the XPath Functions.


see also:

http://www.xenta.nl/blog/2010/07/14/oracle-service-bus-custom-xpath-functions-prefix-not-defined-in-static-context/

http://forums.oracle.com/forums/thread.jspa?threadID=1118538&tstart=30

http://www.xenta.nl/blog/2010/05/10/oracle-service-bus-11g-using-custom-xpath-functions/


and watch this cool video


Sunday, December 5, 2010

Indirectly referenced variables in BPEL

or "Dynamically specified XPaths in BPEL"

Let's assume I have a String variable "counter",
and I want to do a lookup on a collection MyCollection accessing its MyElement number "counter".

In Java it would be Element element = myCollection.get(counter).

In BPEL you have to toil a lot more even to do the simplest thing:



                    
                        
                        
                    
                    
                        
                        
                    
                


If Counter contains 4,
Temp_Variable will contain a string "/ns2:MyCollection/ns2:MyElement[4]"
which will be evaluated as a XPath by the second "copy" statement.

One can - more effectively - use dynamic indexing:

http://download.oracle.com/docs/cd/E12483_01/integrate.1013/b28981/manipdoc.htm#CIHGBJCE




Why do simple things in Java, when you can do overly complicated programming in XML?

Sunday, May 2, 2010

How to extract a fault stacktrace from the SOAP fault using Java

It's..... COMPLICATED!
ps.txt contains the entire fault.... you can adapt to read from a String...

package com.integration.xpath;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;

import javax.xml.XMLConstants;
import javax.xml.namespace.NamespaceContext;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class XPathRunner {
    public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, XPathExpressionException {
        doFault();       
    }

    private static void doFault() throws ParserConfigurationException,
    SAXException, IOException, XPathExpressionException {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true); // never forget this!
        DocumentBuilder builder = factory.newDocumentBuilder();
        File f = new File("st.txt");
        Document doc = builder.parse(f);
        XPathFactory xpathFactory = XPathFactory.newInstance();
        XPath xpath = xpathFactory.newXPath();
        xpath.setNamespaceContext(new PersonalNamespaceContext());
       
        XPathExpression expr = xpath.compile("//bea_fault:stacktrace/text()");
        Object result = expr.evaluate(doc, XPathConstants.NODESET);
        NodeList nodes = (NodeList) result;
        for (int i = 0; i < nodes.getLength(); i++) {
            Node item = nodes.item(i);
            System.out.println(item.getNodeValue());
        }
    }

  
   
    public static class PersonalNamespaceContext implements NamespaceContext {

        public String getNamespaceURI(String prefix) {
            if (prefix == null) throw new NullPointerException("Null prefix");
            else if ("env".equals(prefix)) return "http://schemas.xmlsoap.org/soap/envelope/";
            else if ("bea_fault".equals(prefix)) return "http://www.bea.com/servers/wls70/webservice/fault/1.0.0";
            else if ("xml".equals(prefix)) return XMLConstants.XML_NS_URI;
            return XMLConstants.NULL_NS_URI;
        }

        // This method isn't necessary for XPath processing.
        public String getPrefix(String uri) {
            throw new UnsupportedOperationException();
        }

        // This method isn't necessary for XPath processing either.
        public Iterator getPrefixes(String uri) {
            throw new UnsupportedOperationException();
        }

    }   
}

thank you to http://www.ibm.com/developerworks/library/x-javaxpathapi.html