Class ExploreXML

java.lang.Object
  extended by ExploreXML

public class ExploreXML
extends java.lang.Object


Field Summary
private static java.lang.StringBuffer blanks
          Lazy buffer string containing a given number of spaces
private static org.w3c.dom.Node bottom
          Bottom node of the skeleton document
private static int count
          count the number of successful extractions
static boolean debug
          Global option (-d): will show debugging information during execution
private static org.w3c.dom.Document doc
          To keep the skeleton document
private static long fileLength
          number of bytes in the input file (does not take into account included files)
private static java.lang.String inputFileName
          Last parameter: Name of the input file, if it is null then standard input is used
private static int maxRes
          Extract option (-max): stop reading the file after a given number of results (infinite if not specified)
private static java.lang.String moreHelp
           
private static int nbTests
          count the number of effective tests on the skeleton documents during execution
private static NamespaceContext nsContext
          keep track of the namespaces used in the input document
private static boolean onlyCount
          Extract option (-c): return only the number of extracted nodes
private static java.io.PrintStream out
          print stream corresponding to the output file
private static boolean outAttr
          Explore option (-a): output attribute names in XPath expression
private static boolean outAttrVal
          Explore option (-av): output attribute names and values in XPath expression
private static java.io.OutputStream output
          corresponding to the output file
private static java.lang.String outputFileName
          Global option (-o): Name of the output file, if it is null then standard output is used
private static boolean outVal
          Explore option (-v): output element values in XPath expression
private static java.util.SortedMap<java.lang.String,PathCount> pathCounts
          To keep the count of each XPath expression
private static boolean prettyPrint
          Extract option (-pp): output a prettyprinted version of the extracted node
private static java.lang.String rootName
          Extract option (-r): name of the root element (root if not given)
private static boolean showNamespaces
          Explore option (-ns): show the values of the namespaces used in XPath expressions
private static boolean sortAlpha
          Explore option (-sa): sort the XPath expressions alphabetically
private static boolean sortFreq
          Explore option (-sf): sort the XPath expression by decreasing frequency
private static boolean statistics
          Global option (-st): will show statistics information at the end of execution
private static javax.xml.transform.Transformer trans
          Tranformer of each output element
private static javax.xml.transform.TransformerFactory transFact
          Factory for the transformation of each output element
private static javax.xml.transform.Result transResult
          Output of the transformation, is linked to the output file
private static java.lang.String usageStr
          display of usage of the program with the list of possible command line arguments
private static java.lang.String XML_NS
           
private static javax.xml.stream.XMLStreamReader xmlsr
          Pull parser
private static java.lang.String[] XMLStreamConstantsNames
           
private static javax.xml.stream.XMLStreamWriter xmlsw
          To create the output with a stream writer
private static java.lang.String xpathStr
          Extract option (-xp): XPath expression used to select nodes to extract
private static java.lang.String xsltFile
          Extract option (-xsl): XSLT stylesheet to apply to each extracted node
private static javax.xml.transform.Source xsltSource
          Source of the transformation : will be linked to the skeleton document
 
Constructor Summary
ExploreXML()
           
 
Method Summary
private static void addBottom(boolean full)
          Read the next node (full or not) and insert it at the bottom of the spine document Initialize the spine document if necessary Modify the bottom global variable
private static java.lang.String blanks(int n)
          returns a string of spaces with a lazy StringBuffer
private static java.lang.String buildXPath(org.w3c.dom.Node node)
          Creates a string representation of the skeleton document starting from a given node
static void compact(org.w3c.dom.Node node, java.lang.String indent)
          print a compact form of a Node (used here only for debugging)
static void dumpElem(java.lang.String mess, org.w3c.dom.Node elem)
          Prints a message followed by the compact form of a document Node Only used for debugging purpose
private static void ignore(javax.xml.stream.XMLStreamReader xmlsr)
          ignore the token (if debugging is enabled, it prints the token skipped)
static void main(java.lang.String[] argv)
          Main program that parses arguments, sets up the input and output files and prints the results
private static void parseArgs(java.lang.String[] args)
          Parse program arguments and set appropriate options will stop on -h : prints usage of the program -xpt: prints the xpath expression as it is seen by the program will stop on an unrecognized arguments
private static org.w3c.dom.Node pullFullNode(javax.xml.stream.XMLStreamReader xmlsr, org.w3c.dom.Document doc)
          read a Node (created with the doc) from pull parser reader read a complete element (including embedded elements if necessary)
private static org.w3c.dom.Node pullNode(javax.xml.stream.XMLStreamReader reader, org.w3c.dom.Document doc)
          Read a Node (created with the doc) from pull parser reader We read only the start of an element or a single text/comment/processing-intruction node
private static java.lang.String quotedString(java.lang.String s)
          Create a properly quoted string taking into account the fact that it can contain either single or double quote or both
private static void removeBottom()
          remove the bottom node (and its children) by modifying the bottom global variable
private static void skip(javax.xml.stream.XMLStreamReader xmlsr, int level)
          Skip the rest of an element, taking into account the possibility of embedded elements
private static void usage()
          Prints a short help about the use of the program called with the -h program argument
private static void xplore()
          Explore an XML file by generating a series of Path expression describing leafs of the skeleton document; called when no -xp argument is given
private static void xtract()
          Create a new XML document for each node matching the XPath expression given by the -xp parameter in order to save on memory each node is generated one by one as the input file is read
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

XML_NS

private static final java.lang.String XML_NS
See Also:
Constant Field Values

debug

public static boolean debug
Global option (-d): will show debugging information during execution


statistics

private static boolean statistics
Global option (-st): will show statistics information at the end of execution


inputFileName

private static java.lang.String inputFileName
Last parameter: Name of the input file, if it is null then standard input is used


outputFileName

private static java.lang.String outputFileName
Global option (-o): Name of the output file, if it is null then standard output is used


outAttr

private static boolean outAttr
Explore option (-a): output attribute names in XPath expression


outAttrVal

private static boolean outAttrVal
Explore option (-av): output attribute names and values in XPath expression


outVal

private static boolean outVal
Explore option (-v): output element values in XPath expression


sortAlpha

private static boolean sortAlpha
Explore option (-sa): sort the XPath expressions alphabetically


sortFreq

private static boolean sortFreq
Explore option (-sf): sort the XPath expression by decreasing frequency


showNamespaces

private static boolean showNamespaces
Explore option (-ns): show the values of the namespaces used in XPath expressions


xpathStr

private static java.lang.String xpathStr
Extract option (-xp): XPath expression used to select nodes to extract


xsltFile

private static java.lang.String xsltFile
Extract option (-xsl): XSLT stylesheet to apply to each extracted node


rootName

private static java.lang.String rootName
Extract option (-r): name of the root element (root if not given)


onlyCount

private static boolean onlyCount
Extract option (-c): return only the number of extracted nodes


prettyPrint

private static boolean prettyPrint
Extract option (-pp): output a prettyprinted version of the extracted node


maxRes

private static int maxRes
Extract option (-max): stop reading the file after a given number of results (infinite if not specified)


usageStr

private static java.lang.String usageStr
display of usage of the program with the list of possible command line arguments


moreHelp

private static java.lang.String moreHelp

count

private static int count
count the number of successful extractions


nbTests

private static int nbTests
count the number of effective tests on the skeleton documents during execution


fileLength

private static long fileLength
number of bytes in the input file (does not take into account included files)


pathCounts

private static java.util.SortedMap<java.lang.String,PathCount> pathCounts
To keep the count of each XPath expression


xmlsr

private static javax.xml.stream.XMLStreamReader xmlsr
Pull parser


xmlsw

private static javax.xml.stream.XMLStreamWriter xmlsw
To create the output with a stream writer


out

private static java.io.PrintStream out
print stream corresponding to the output file


output

private static java.io.OutputStream output
corresponding to the output file


xsltSource

private static javax.xml.transform.Source xsltSource
Source of the transformation : will be linked to the skeleton document


transFact

private static javax.xml.transform.TransformerFactory transFact
Factory for the transformation of each output element


trans

private static javax.xml.transform.Transformer trans
Tranformer of each output element


transResult

private static javax.xml.transform.Result transResult
Output of the transformation, is linked to the output file


nsContext

private static NamespaceContext nsContext
keep track of the namespaces used in the input document


doc

private static org.w3c.dom.Document doc
To keep the skeleton document


bottom

private static org.w3c.dom.Node bottom
Bottom node of the skeleton document


XMLStreamConstantsNames

private static java.lang.String[] XMLStreamConstantsNames

blanks

private static java.lang.StringBuffer blanks
Lazy buffer string containing a given number of spaces

Constructor Detail

ExploreXML

public ExploreXML()
Method Detail

parseArgs

private static void parseArgs(java.lang.String[] args)
Parse program arguments and set appropriate options will stop on -h : prints usage of the program -xpt: prints the xpath expression as it is seen by the program will stop on an unrecognized arguments

Parameters:
args - String array as given to the main program

main

public static void main(java.lang.String[] argv)
Main program that parses arguments, sets up the input and output files and prints the results

Parameters:
argv - arguments as given the system

usage

private static void usage()
Prints a short help about the use of the program called with the -h program argument


xplore

private static void xplore()
                    throws org.w3c.dom.DOMException,
                           javax.xml.stream.XMLStreamException
Explore an XML file by generating a series of Path expression describing leafs of the skeleton document; called when no -xp argument is given

Throws:
org.w3c.dom.DOMException
javax.xml.stream.XMLStreamException

buildXPath

private static java.lang.String buildXPath(org.w3c.dom.Node node)
Creates a string representation of the skeleton document starting from a given node

Parameters:
node - node of the skeleton document
Returns:
String showing the path expression of the leaf of the skeleton document

quotedString

private static java.lang.String quotedString(java.lang.String s)
Create a properly quoted string taking into account the fact that it can contain either single or double quote or both

Parameters:
s - input string
Returns:
quoted input string

xtract

private static void xtract()
                    throws javax.xml.stream.XMLStreamException,
                           javax.xml.transform.TransformerException
Create a new XML document for each node matching the XPath expression given by the -xp parameter in order to save on memory each node is generated one by one as the input file is read

Throws:
javax.xml.stream.XMLStreamException
javax.xml.transform.TransformerException

pullNode

private static org.w3c.dom.Node pullNode(javax.xml.stream.XMLStreamReader reader,
                                         org.w3c.dom.Document doc)
                                  throws javax.xml.stream.XMLStreamException
Read a Node (created with the doc) from pull parser reader We read only the start of an element or a single text/comment/processing-intruction node

Parameters:
reader - pull parser
doc - document used to create new nodes
Returns:
the Document node that was read from the input
Throws:
javax.xml.stream.XMLStreamException

pullFullNode

private static org.w3c.dom.Node pullFullNode(javax.xml.stream.XMLStreamReader xmlsr,
                                             org.w3c.dom.Document doc)
                                      throws javax.xml.stream.XMLStreamException
read a Node (created with the doc) from pull parser reader read a complete element (including embedded elements if necessary)

Parameters:
xmlsr - the pull-parser
doc - the document to create new nodes
Returns:
the Document node created by the pull-parser
Throws:
javax.xml.stream.XMLStreamException

addBottom

private static void addBottom(boolean full)
                       throws org.w3c.dom.DOMException,
                              javax.xml.stream.XMLStreamException
Read the next node (full or not) and insert it at the bottom of the spine document Initialize the spine document if necessary Modify the bottom global variable

Parameters:
full - boolean indicating if the full node must be read or only the start
Throws:
org.w3c.dom.DOMException
javax.xml.stream.XMLStreamException

removeBottom

private static void removeBottom()
remove the bottom node (and its children) by modifying the bottom global variable


skip

private static void skip(javax.xml.stream.XMLStreamReader xmlsr,
                         int level)
                  throws javax.xml.stream.XMLStreamException
Skip the rest of an element, taking into account the possibility of embedded elements

Parameters:
xmlsr - pull-parser
level - indicates how many starting elements have been encountered without closing
Throws:
javax.xml.stream.XMLStreamException

ignore

private static void ignore(javax.xml.stream.XMLStreamReader xmlsr)
                    throws javax.xml.stream.XMLStreamException
ignore the token (if debugging is enabled, it prints the token skipped)

Parameters:
xmlsr -
Throws:
javax.xml.stream.XMLStreamException

dumpElem

public static void dumpElem(java.lang.String mess,
                            org.w3c.dom.Node elem)
Prints a message followed by the compact form of a document Node Only used for debugging purpose

Parameters:
mess - String giving the message to show
elem - Node to print in compact form

compact

public static void compact(org.w3c.dom.Node node,
                           java.lang.String indent)
print a compact form of a Node (used here only for debugging)

Parameters:
node - starting node to compact
indent - string added at the start of each new line

blanks

private static java.lang.String blanks(int n)
returns a string of spaces with a lazy StringBuffer

Parameters:
n - number of spaces to return
Returns:
string of n spaces