Tuesday, May 29, 2012

Some handy utility methods for XPath

Some handy utility methods for XPath:

import com.collaxa.cube.xml.xpath.SimpleNamespaceContext;
import java.util.*;
import javax.xml.namespace.NamespaceContext;
import javax.xml.xpath.*;
import oracle.bpel.services.common.util.XMLUtil;
import oracle.tip.pc.infra.exception.PCException;

import oracle.xml.xpath.JXPathFactory;
import org.w3c.dom.*;

    public class XPathUtils
{

    public XPathUtils()
    {
    }

    public static NodeList selectNodes(Element pElement, String xpathExpression)
        throws Exception
    {
        XPath xPath = createXPath();
        Document doc = null;
        if(pElement instanceof Document)
            doc = (Document)pElement;
        else
            doc = pElement != null ? pElement.getOwnerDocument() : null;
        NodeList value = (NodeList)xPath.evaluate(xpathExpression, doc, XPathConstants.NODESET);
        return value;
    }

    public static NodeList selectNodes(Element pElement, String xpathExpression, Map prefixNamespaceMapping)
        throws Exception
    {
        XPath xPath = createXPath(prefixNamespaceMapping);
        Document doc = null;
        if(pElement instanceof Document)
            doc = (Document)pElement;
        else
            doc = pElement != null ? pElement.getOwnerDocument() : null;
        NodeList value = (NodeList)xPath.evaluate(xpathExpression, doc, XPathConstants.NODESET);
        return value;
    }

    public static NodeList selectNodes(Element pElement, String xpathExpression, NamespaceContext namespaceContext)
        throws Exception
    {
        XPath xPath = createXPath(namespaceContext);
        Document doc = null;
        if(pElement instanceof Document)
            doc = (Document)pElement;
        else
            doc = pElement != null ? pElement.getOwnerDocument() : null;
        NodeList value = (NodeList)xPath.evaluate(xpathExpression, doc, XPathConstants.NODESET);
        return value;
    }

    public static Object evaluate(Element pElement, String xpathExpression)
        throws Exception
    {
        XPath xPath = createXPath();
        Document doc = null;
        if(pElement instanceof Document)
            doc = (Document)pElement;
        else
            doc = pElement != null ? pElement.getOwnerDocument() : null;
        Object value = xPath.evaluate(xpathExpression, doc, XPathConstants.NODESET);
        return value;
    }

    public static Object evaluate(Element pElement, String xpathExpression, Map prefixNamespaceMapping)
        throws Exception
    {
        XPath xPath = createXPath(prefixNamespaceMapping);
        Document doc = null;
        if(pElement instanceof Document)
            doc = (Document)pElement;
        else
            doc = pElement != null ? pElement.getOwnerDocument() : null;
        Object value = xPath.evaluate(xpathExpression, doc, XPathConstants.NODESET);
        return value;
    }

    public static Object selectSingleNode(Element pElement, String xpathExpression)
        throws Exception
    {
        XPath xPath = createXPath();
        Document doc = null;
        if(pElement instanceof Document)
            doc = (Document)pElement;
        else
            doc = pElement != null ? pElement.getOwnerDocument() : null;
        Node value = (Node)xPath.evaluate(xpathExpression, doc, XPathConstants.NODE);
        return value;
    }

    public static Object selectSingleNode(Element pElement, String xpathExpression, Map prefixNamespaceMapping)
        throws Exception
    {
        XPath xPath = createXPath(prefixNamespaceMapping);
        Document doc = null;
        if(pElement instanceof Document)
            doc = (Document)pElement;
        else
            doc = pElement != null ? pElement.getOwnerDocument() : null;
        Node value = (Node)xPath.evaluate(xpathExpression, doc, XPathConstants.NODE);
        return value;
    }

    public static String valueOf(Element pElement, String xpathExpression)
        throws Exception
    {
        XPath xPath = createXPath();
        Document doc = null;
        if(pElement instanceof Document)
            doc = (Document)pElement;
        else
            doc = pElement != null ? pElement.getOwnerDocument() : null;
           
        String value = (String)xPath.evaluate(xpathExpression, doc, XPathConstants.STRING);
        return value;
    }

    public static String valueOf(Element pElement, String xpathExpression, Map prefixNamespaceMapping)
        throws Exception
    {
        XPath xPath = createXPath(prefixNamespaceMapping);
        Document doc = null;
        if(pElement instanceof Document)
            doc = (Document)pElement;
        else
            doc = pElement != null ? pElement.getOwnerDocument() : null;
        String value = (String)xPath.evaluate(xpathExpression, doc, XPathConstants.STRING);
        return value;
    }

    public static Number numberValueOf(Element pElement, String xpathExpression)
        throws Exception
    {
        XPath xPath = createXPath();
        Document doc = null;
        if(pElement instanceof Document)
            doc = (Document)pElement;
        else
            doc = pElement != null ? pElement.getOwnerDocument() : null;
        Number nValue = (Number)xPath.evaluate(xpathExpression, doc, XPathConstants.NUMBER);
        return nValue;
    }

    public static Number numberValueOf(Element pElement, String xpathExpression, Map prefixNamespaceMapping)
        throws Exception
    {
        XPath xPath = createXPath(prefixNamespaceMapping);
        Document doc = null;
        if(pElement instanceof Document)
            doc = (Document)pElement;
        else
            doc = pElement != null ? pElement.getOwnerDocument() : null;
        Number nValue = (Number)xPath.evaluate(xpathExpression, doc, XPathConstants.NUMBER);
        return nValue;
    }



Saturday, May 19, 2012

JAXB Mapping of XML Schema Built-in Data Types

JAXB Mapping of XML Schema Built-in Data Types  


XML Schema Type
Java Data Type
xsd:string
java.lang.String
xsd:integer
java.math.BigInteger
xsd:int
int
xsd.long
long
xsd:short
short
xsd:decimal
java.math.BigDecimal
xsd:float
float
xsd:double
double
xsd:boolean
boolean
xsd:byte
byte
xsd:QName
javax.xml.namespace.QName
xsd:dateTime
java.util.Calendar
xsd:base64Binary
byte[]
xsd:hexBinary
byte[]
xsd:unsignedInt
long
xsd:unsignedShort
int
xsd:unsignedByte
short
xsd:time
java.util.Calendar
xsd:date
java.util.Calendar
xsd:anySimpleType
java.lang.String




Programmatically compile Java class


Programmatically compile Java class:

import javax.tools.JavaCompiler;
import javax.tools.ToolProvider;

public class Main {
  public static void main(String[] args) {
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    int result = compiler.run(null, null, null, "Hello.java");

    System.out.println("Compile result code = " + result);
  }
}
 


Cascade type - EJB 3.0


Cascade type - EJB 3.0:

The cascade type specifies the set of operations that can be cascaded to a related entity. CascadeType can have different values: PERSIST, MERGE, REMOVE, REFRESH, and ALL.
PERSIST is similar to an insert command. Specifying a CascadeType of PERSIST implies that a persist operation will be cascaded from a parent entity to its child entity.

MERGE is similar to an update command. Specifying a CascadeType of MERGE implies that a merge operation will be cascaded from a parent entity to its child entity.

REMOVE is similar to a delete command. Specifying a CascadeType of REMOVE implies that a remove operation will be cascaded from a parent entity to its child entity.

REFRESH reloads the related entity from the database when the referring entity is refreshed.

If you do not specify a cascade type, no operations are cascaded.


Fetch Type - Lazy vs. Eager Loading in EJB 3.0

Fetch Type - Lazy vs. Eager Loading in EJB 3.0:

The fetch type is used to specify the data-fetching strategy that a persistence provider uses to fetch data from the database. FetchType is used on the @Basicannotation, @LOB annotation, and relationship annotations such as OneToMany, ManyToMany, ManyToOne, and OneToOne. The default for fetchTypeis EAGER, except for a ManyToMany and a OneToMany relationship, for which the default is LAZY. A fetchType of EAGER means that a persistence provider will load the attribute of an entity along with the entity, whereas a fetchType of LAZY is a hint to the provider that the attribute need not be fetched along with the entity, or we can say that In eager loading we will fetch all the values from the Persistent storage and cache it. It will make serious performance issues. There we use lazy loading to avoid that.

Keep in mind that a FetchType of EAGER is a requirement on the persistence provider, whereas a fetchType of LAZY is only a hint. So even though you may specify the fetchType to be LAZY, the persistence provider may choose to load the attribute eagerly.
A FetchType of lazy benefits Large Objects and Relationships in which the attributes are not accessed immediately when the entity is loaded.The attributes are loaded whenever required.

Example:

@Entity
public class Order {
@OneToMany(fetchType=FetchType.EAGER, ...)
public Collection<OrderLineItem> getLineItems(){
return lineItems;
}
}

@Entity
public class Order {
@OneToMany(fetchType=FetchType.LAZY, ...)
public Collection<OrderLineItem> getLineItems(){
return lineItems;
}
}