com.ximpleware.extended.AutoPilotHuge Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of vtd-xml Show documentation
Show all versions of vtd-xml Show documentation
XimpleWare's VTD-XML is, far and away, the industry's most advanced and powerful XML processing model for SOA and Cloud Computing
/*
* Copyright (C) 2002-2012 XimpleWare, [email protected]
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package com.ximpleware.extended;
import com.ximpleware.extended.xpath.*;
import java.util.*;
import java.io.*;
/**
* XimpleWare's AutoPilotHuge implementation encapsulating node iterator
* and XPath.
* AutoPilotHuge is an extended VTD edition of AutoPilot supporting 256 GigByte
* XML file
*
*/
public class AutoPilotHuge {
private int depth;
// the depth of the element at the starting point will determine when to stop iteration
private int iter_type; // see selectElement
private VTDNavHuge vn; // the navigator object
private int index; // for iterAttr
private boolean ft; // a helper variable for
private boolean special; // This helps distinguish between
// the case of node() and * for preceding axis
// of xpath evaluation
private String name; // Store element name after selectElement
private String localName; // Store local name after selectElemntNS
private String URL; // Store URL name after selectElementNS
private int size; // for iterateAttr
private Expr xpe; // for evalXPath
private int[] contextCopy; //for preceding axis
private int stackSize; // the stack size for xpath evaluation
static private Hashtable nsHash;
//private parser p;
// defines the type of "iteration"
public final static int UNDEFINED = 0;
// set the mode corresponding to DOM's getElemetnbyName(string)
public final static int SIMPLE = 1;
// set the mode corresponding to DOM's getElementbyNameNS(string)
public final static int SIMPLE_NS = 2;
public final static int DESCENDANT = 3;
public final static int DESCENDANT_NS = 4;
public final static int FOLLOWING = 5;
public final static int FOLLOWING_NS=6;
public final static int PRECEDING = 7;
public final static int PRECEDING_NS=8;
public final static int ATTR = 9;
public final static int ATTR_NS = 10;
static private Hashtable symbolHash;
protected String getName(){
return name;
}
/**
* AutoPilotHuge constructor comment.
* @exception IllegalArgumentException If the VTDNav object is null
*/
public AutoPilotHuge(VTDNavHuge v) {
if (v == null)
throw new IllegalArgumentException(" instance of VTDNav can't be null ");
name = null;
vn = v;
//depth = v.getCurrentDepth();
iter_type = UNDEFINED; // not defined
ft = true;
size = 0;
special = false;
xpe = null;
symbolHash = new Hashtable();
//p = null;
}
/**
* Use this constructor for delayed binding to VTDNavHuge
* which allows the reuse of XPath expression
*
*/
public AutoPilotHuge(){
name = null;
//vn = v;
//depth = v.getCurrentDepth();
iter_type = UNDEFINED; // not defined
ft = true;
size = 0;
special = false;
xpe = null;
symbolHash = new Hashtable();
}
/** This function creates URL ns prefix
* and is intended to be called prior to selectXPath
* @param prefix
* @param URL
*/
public void declareXPathNameSpace(String prefix, String URL){
if (nsHash==null)
nsHash = new Hashtable();
nsHash.put(prefix, URL);
//System.out.println(ht);
}
/**
* Bind resets the internal state of AutoPilotHuge
* so one can attach a VTDNavHuge object to the autopilot
* @param vnv
*
*/
public void bind (VTDNavHuge vnv){
name = null;
if (vnv == null)
throw new IllegalArgumentException(" instance of VTDNav can't be null ");
vn = vnv;
//depth = v.getCurrentDepth();
iter_type = UNDEFINED; // not defined
ft = true;
size = 0;
special = false;
//resetXPath();
}
/**
* Register the binding between a variableExpr name and variableExpr expression
* @param varName
* @param varExpr
* @throws XPathParseException
*/
public void declareVariableExpr(String varName, String varExpr) throws XPathParseExceptionHuge {
try{
parser p = new parser(new StringReader(varExpr));
p.nsHash = nsHash;
p.symbolHash = symbolHash;
xpe = (com.ximpleware.extended.xpath.Expr) p.parse().value;
symbolHash.put(varName, xpe);
ft = true;
}catch(XPathParseExceptionHuge e){
System.out.println("Syntax error after ==>"+varExpr.substring(0,e.getOffset()));
throw e;
}catch(Exception e){
throw new XPathParseExceptionHuge("Error occurred");
}
}
/**
* Iterate over all the selected element nodes in document order.
* Null element name allowed, corresponding to node() in xpath
* Creation date: (12/4/03 5:25:42 PM)
* @return boolean
* @exception com.ximpleware.extended.NavExceptionHuge See description in method toElement() in VTDNavHuge class.
*/
public boolean iterate() throws PilotExceptionHuge, NavExceptionHuge {
switch (iter_type) {
case SIMPLE :
//System.out.println("iterating ---> "+name+ " depth ---> "+depth);
/*if (elementName == null)
throw new PilotException(" Element name not set ");*/
if (vn.atTerminal)
return false;
if (ft == false)
return vn.iterate(depth, name, special);
else {
ft = false;
if (special ||
vn.matchElement(name)) {
return true;
} else
return vn.iterate(depth, name, special);
}
case SIMPLE_NS :
if (vn.atTerminal)
return false;
if (ft == false)
return vn.iterateNS(depth, URL, localName);
else {
ft = false;
if (vn.matchElementNS(URL, localName)) {
return true;
} else
return vn.iterateNS(depth, URL, localName);
}
case DESCENDANT:
if (vn.atTerminal)
return false;
return vn.iterate(depth, name, special);
case DESCENDANT_NS:
if (vn.atTerminal)
return false;
return vn.iterateNS(depth, URL, localName);
case FOLLOWING:
if (vn.atTerminal)
return false;
if (ft == false)
return vn.iterate_following(name, special);
else {
ft = false;
// find the first next sibling of
while(true){
while (vn.toElement(VTDNavHuge.NS)){
if (special || vn.matchElement(name)) {
return true;
}
return vn.iterate_following(name, special);
}
if (vn.toElement(VTDNavHuge.P)==false){
//return vn.iterate_following(name, special);
return false;
}
}
}
case FOLLOWING_NS:
if (vn.atTerminal)
return false;
if (ft == false)
return vn.iterate_followingNS(URL,localName);
else {
ft = false;
// find the first next sibling of
while(true){
while (vn.toElement(VTDNavHuge.NS)){
if (vn.matchElementNS(URL,localName)) {
return true;
}
return vn.iterate_followingNS(URL,localName);
}
if (vn.toElement(VTDNavHuge.P)==false){
return false;
}
}
}
case PRECEDING:
if (vn.atTerminal)
return false;
return vn.iterate_preceding(name, contextCopy,special);
case PRECEDING_NS:
if (vn.atTerminal)
return false;
return vn.iterate_precedingNS(URL,localName,contextCopy);
default :
throw new PilotExceptionHuge(" iteration action type undefined");
}
}
/**
* This method implements the attribute axis for XPath
* @return the integer of the selected VTD index for attribute name
* @throws com.ximpleware.extended.PilotException
*/
protected int iterateAttr() throws PilotExceptionHuge,NavExceptionHuge{
switch(iter_type){
case ATTR:
if (name.compareTo("*")==0){
if (ft != false){
ft = false;
index = vn.getCurrentIndex2()+1;
} else
index +=2;
if (vn.ns == false){
while(index"+ s.substring(0,e.getOffset()));
throw e;
}catch(Exception e){
throw new XPathParseExceptionHuge("Error occurred");
}
}
/**
* Reset the XPath so the XPath Expression can
* be reused and revaluated in anther context position
*
*/
public void resetXPath(){
if (xpe!=null && vn!=null){
xpe.reset(vn);
ft = true;
vn.contextStack2.size = stackSize;
}
}
/**
* evalXPathToNumber() evaluates the xpath expression to a double
* @return double
*
*/
public double evalXPathToNumber(){
return xpe.evalNumber(vn);
}
/**
* evalXPathToString() evaluates the xpath expression to a String
* @return String
*
*/
public String evalXPathToString(){
return xpe.evalString(vn);
}
/**
* evalXPathToBoolean() evaluates the xpath expression to a boolean
* @return boolean
*
*/
public boolean evalXPathToBoolean(){
return xpe.evalBoolean(vn);
}
/**
* This method returns the next node in the nodeset
* it returns -1 if there is no more node
* Afer finishing evaluating, don't forget to reset the xpath
* @return int corresponding to the VTD index
*/
public int evalXPath() throws XPathEvalExceptionHuge, NavExceptionHuge{
if (xpe!=null){
if (ft == true){
if (vn != null){
stackSize = vn.contextStack2.size;
}
ft = false;
xpe.adjust(vn.getTokenCount());
}
return xpe.evalNodeSet(vn);
}
throw new PilotExceptionHuge(" Null XPath expression ");
}
/**
* Setspecial is used by XPath evaluator to distinguish between
* node() and *
* node() corresponding to b= true;
* @param b
*/
protected void setSpecial(boolean b ){
special = b;
}
/**
* Convert the expression to a string
* For debugging purpose
* @return String
*/
public String getExprString(){
return xpe.toString();
}
/**
* Remove all declared variable expressions
*/
public void clearVariableExprs(){
symbolHash.clear();
}
/**
* Remove all namespaces bindings
*/
public void clearXPathNameSpaces(){
nsHash.clear();
}
}