package com.dbxml.xml.dtsm;
/*
* dbXML - Native XML Database
* Copyright (c) 1999-2006 The dbXML Group, L.L.C.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* $Id: DTSMHelper.java,v 1.6 2006/02/02 19:04:46 bradford Exp $
*/
import java.io.*;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.ContentHandler;
import com.dbxml.xml.QName;
import com.dbxml.xml.SymbolTable;
import com.dbxml.xml.dom.DOMProducer;
import com.dbxml.xml.dom.DOMTableGenerator;
import com.dbxml.xml.sax.SAXProducer;
import com.dbxml.xml.sax.SAXTableGenerator;
import com.dbxml.xml.text.TextProducer;
import com.dbxml.xml.text.TextTableGenerator;
/**
* DTSMHelper
*/
public final class DTSMHelper {
private DTSMHelper() {
}
public static Node tableToNode(DocumentTable table) throws DTSMException {
if ( table != null ) {
try {
DOMProducer producer = new DOMProducer(table);
producer.process();
return producer.getNode();
}
catch ( DTSMException e ) {
throw e;
}
catch ( Exception e ) {
throw new DTSMException(e);
}
}
else
throw new DTSMException("DocumentTable is null");
}
public static Document tableToDocument(DocumentTable table) throws DTSMException {
if ( table != null ) {
try {
DOMProducer producer = new DOMProducer(table);
producer.process();
Node node = producer.getNode();
if ( node.getNodeType() == Node.DOCUMENT_NODE )
return (Document)node;
else
return node.getOwnerDocument();
}
catch ( DTSMException e ) {
throw e;
}
catch ( Exception e ) {
throw new DTSMException(e);
}
}
else
throw new DTSMException("DocumentTable is null");
}
public static void tableToWriter(DocumentTable table, Writer writer) throws DTSMException {
if ( table != null ) {
try {
TextProducer producer = new TextProducer(table);
producer.setWriter(writer);
producer.process();
}
catch ( DTSMException e ) {
throw e;
}
catch ( Exception e ) {
throw new DTSMException(e);
}
}
}
public static void tableToWriter(DocumentTable table, Writer writer, boolean omitPrefix) throws DTSMException {
if ( table != null ) {
try {
TextProducer producer = new TextProducer(table, omitPrefix);
producer.setWriter(writer);
producer.process();
}
catch ( DTSMException e ) {
throw e;
}
catch ( Exception e ) {
throw new DTSMException(e);
}
}
}
public static void tableToWriter(DocumentTable table, PrintWriter writer) throws DTSMException {
if ( table != null ) {
try {
TextProducer producer = new TextProducer(table);
producer.setWriter(writer);
producer.process();
}
catch ( DTSMException e ) {
throw e;
}
catch ( Exception e ) {
throw new DTSMException(e);
}
}
}
public static void tableToWriter(DocumentTable table, PrintWriter writer, boolean omitPrefix) throws DTSMException {
if ( table != null ) {
try {
TextProducer producer = new TextProducer(table, omitPrefix);
producer.setWriter(writer);
producer.process();
}
catch ( DTSMException e ) {
throw e;
}
catch ( Exception e ) {
throw new DTSMException(e);
}
}
}
public static String tableToText(DocumentTable table) throws DTSMException {
if ( table != null ) {
StringWriter writer = new StringWriter(4096);
tableToWriter(table, writer);
return writer.toString();
}
else
throw new DTSMException("DocumentTable is null");
}
public static String tableToText(DocumentTable table, boolean omitPrefix) throws DTSMException {
if ( table != null ) {
StringWriter writer = new StringWriter(4096);
tableToWriter(table, writer, omitPrefix);
return writer.toString();
}
else
throw new DTSMException("DocumentTable is null");
}
public static void tableToSAX(DocumentTable table, ContentHandler handler) throws DTSMException {
if ( table != null ) {
try {
SAXProducer sp = new SAXProducer(table, handler);
sp.process();
}
catch ( DTSMException e ) {
throw e;
}
catch ( Exception e ) {
throw new DTSMException(e);
}
}
}
public static DocumentTable nodeToTable(Node node, SymbolTable symbols) throws DTSMException {
if ( node != null ) {
DOMTableGenerator dtm = new DOMTableGenerator(node);
dtm.setSymbolTable(symbols);
return dtm.process();
}
else
throw new DTSMException("Node is null");
}
public static DocumentTable documentToTable(Document doc, SymbolTable symbols) throws DTSMException {
return nodeToTable(doc, symbols);
}
public static DocumentTable textToTable(String text, SymbolTable symbols) throws DTSMException {
if ( text != null ) {
TextTableGenerator ttg = new TextTableGenerator(text);
ttg.setSymbolTable(symbols);
return ttg.process();
}
else
throw new DTSMException("String is null");
}
public static DocumentTable bytesToTable(byte[] b, SymbolTable symbols) throws DTSMException {
if ( b != null ) {
TextTableGenerator ttg = new TextTableGenerator(b);
ttg.setSymbolTable(symbols);
return ttg.process();
}
else
throw new DTSMException("Byte array is null");
}
public static DocumentTable charsToTable(char[] ch, SymbolTable symbols) throws DTSMException {
if ( ch != null ) {
TextTableGenerator ttg = new TextTableGenerator(ch);
ttg.setSymbolTable(symbols);
return ttg.process();
}
else
throw new DTSMException("Char array is null");
}
public static DocumentTable inputStreamToTable(InputStream is, SymbolTable symbols) throws DTSMException {
if ( is != null ) {
SAXTableGenerator stg = new SAXTableGenerator(is);
stg.setSymbolTable(symbols);
return stg.process();
}
else
throw new DTSMException("InputStream is null");
}
public static DocumentTable readerToTable(Reader reader, SymbolTable symbols) throws DTSMException {
if ( reader != null ) {
SAXTableGenerator stg = new SAXTableGenerator(reader);
stg.setSymbolTable(symbols);
return stg.process();
}
else
throw new DTSMException("Reader is null");
}
public static DocumentTable fileToTable(File f, SymbolTable symbols) throws DTSMException {
if ( f != null ) {
try {
SAXTableGenerator stg = new SAXTableGenerator(f);
stg.setSymbolTable(symbols);
return stg.process();
}
catch ( IOException e ) {
throw new DTSMException("Couldn't convert file '"+f.getName()+"' to table", e);
}
}
else
throw new DTSMException("File is null");
}
public static CompressedTable compressTable(DocumentTable table) throws DTSMException {
if ( table != null ) {
TableCompressor c = new TableCompressor(table);
return c.buildCompressedTable();
}
else
throw new DTSMException("DocumentTable is null");
}
public static CompressedTable compressTable(TableBuilder builder) throws DTSMException {
if ( builder != null ) {
TableCompressor c = new TableCompressor(builder);
return c.buildCompressedTable();
}
else
throw new DTSMException("TableBuilder is null");
}
public static TableBuilder copyTable(DocumentTable table, SymbolTable symbols) throws DTSMException {
if ( table != null ) {
TableBuilder builder = new TableBuilder(symbols);
PullProcessor proc = new PullProcessor(table);
while ( proc.hasNext() ) {
DocumentTableEntry entry = proc.next();
byte typeID = entry.getTypeID();
int valID = entry.getValID();
if ( Constants.isPayloadSymbol(typeID) ) {
if ( valID != -1 ) {
QName qname = symbols.getQName(valID);
builder.addEntry(typeID, qname);
}
else
builder.addEntry(typeID);
}
else {
byte[] b = table.getValue(valID).getData();
builder.addEntry(typeID, b);
}
}
return builder;
}
else
throw new DTSMException("DocumentTable is null");
}
public static TableBuilder copyTable(DocumentTable table) throws DTSMException {
return copyTable(table, table.getSymbolTable());
}
}