📄 loaderxincluder.java
字号:
/*
Loader - tool for transfering data from one JDBC source to another and
doing transformations during copy.
Copyright (C) 2002 Together
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
LoaderXIncluder.java
Date: 22.09.2001.
@version 1.0
@author:
Milosevic Sinisa
*/
package org.webdocwf.util.loader;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import org.apache.xerces.parsers.SAXParser;
import org.webdocwf.util.loader.logging.Logger;
import org.xml.sax.AttributeList;
import org.xml.sax.HandlerBase;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
/**
* LoaderXIncluder.java
* This class parses a document (text or xml file) and writes the documents
* contents back to standard output.
*/
public class LoaderXIncluder
extends HandlerBase {
private Writer out;
private String encoding;
private int level = 0;
//counter for dtd file
private int counter1 = 1;
//counter for sql files
private int counter2 = 0;
//counter for xml (import definition) file
private int counter3 = 0;
//counter for line before dtd file
private int counter4 = 0;
//counter for LoaderJob.xml (part) file
private int counter5 = 0;
//for create and drop database
private int counter6 = 0;
private boolean firstPartXml = true;
private String loaderJobPath="";
/** Main program entry point.
* @param argv represents input parmeters
*/
public static void main(String argv[]) {
if (argv.length == 0) {
System.out.println("Usage: java LoaderXIncluder uri");
System.out.println(" where uri is the URI of your XML document.");
System.out.println(" Sample: java LoaderXIncluder demo.xml");
System.exit(1);
}
LoaderXIncluder s1 = new LoaderXIncluder();
s1.parseURI(argv[0]);
}
/**
* Constructor of class without parameters.
*/
public LoaderXIncluder() {
}
/**
* Construct object LoaderXIncluder with associated outputStream and encoding.
* @param out - OutputStream where will be written final XML.
* @param encoding - encoding String representation of encoding.
* @throws UnsupportedEncodingException
*/
public LoaderXIncluder(OutputStream out, String encoding) throws UnsupportedEncodingException {
this.out = new OutputStreamWriter(out, encoding);
this.encoding = encoding;
}
/**
* Construct object LoaderXIncluder with associated outputStream.
* Class uses default "UTF-8" encoding.
* @param out - OutputStream where will be written final XML.
*/
public LoaderXIncluder(OutputStream out) {
try {
this.out = new OutputStreamWriter(out, "UTF8");
this.encoding = "UTF-8";
}
catch (UnsupportedEncodingException e) {}
}
/**
* Method parseUri parses a file "uri" and writes the file
* contents back to standard output including contents of 'include' files .
* @param uri - Name of XML file.
*/
public void parseURI(String uri) {
try {
SAXParser parser = new SAXParser();
parser.setDocumentHandler(this);
parser.setEntityResolver(this);
parser.setFeature("http://xml.org/sax/features/validation", false);
parser.setErrorHandler(this);
// System.out.println("Loader loads XML file : "+ uri+"\n");
Logger.getCentralLogger().write("normal", "Loader loads XML file : " + uri);
File f = new File(uri);
this.loaderJobPath=f.getCanonicalFile().getParent();
URL xml = f.toURL();
InputStreamReader xmlStream = new InputStreamReader(xml.openStream());
BufferedReader reader = new BufferedReader(xmlStream);
String intro = "";
while ( (intro = reader.readLine()) != null) {
if (intro.indexOf("include") == -1)
counter6++;
else
break;
}
counter6 = counter6 - 4;
LocationOfException.setJdbcNumber(counter6);
reader.close();
xmlStream.close();
parser.parse(uri);
}
catch (Exception e) {
// System.err.println();
e.printStackTrace();
Logger.getCentralLogger().write("normal", e.getMessage());
}
}
boolean hasDTD = false;
/** Processing DOCTYPE deklaration
* @param publicId is public ID of document
* @param systemId is system ID of document
* @return null
*/
public InputSource resolveEntity(String publicId, String systemId) {
hasDTD = true;
try {
if (systemId != null) {
out.write("<!DOCTYPE loaderJob SYSTEM \"" + systemId + "\">" + "\n");
counter4++;
} else if (publicId != null) {
out.write("<!DOCTYPE loaderJob PUBLIC \"" + publicId + "\">" + "\n");
counter4++;
}
}
catch (Exception e) {
System.err.println(e);
}
return null;
}
/** Processing instruction.
*@param target is target
*@param data is target data
*/
public void processingInstruction(String target, String data) {
try {
out.write("<?");
out.write(target);
if (data != null && data.length() > 0) {
out.write(' ');
out.write(data);
}
out.write("?>");
counter4++;
}
catch (IOException e) {
System.err.println(e);
}
}
/** Start document. */
public void startDocument() {
if (level == 0) {
try {
out.write("<?xml version=\"1.0\"?>\r\n");
counter4++;
}
catch (IOException e) {
System.err.println(e);
}
LocationOfException.setIntroNumber(counter4);
}
}
/** Start element.
* @param name is name of the tag
* @param atts is attributes of the tag
*/
public void startElement(String name, AttributeList atts) {
try {
//if DTD or SCHEMA is not defined use default
if (name.equalsIgnoreCase("loaderJob")) {
/*
if (!hasDTD) {
String OCTOPUS_HOME = System.getProperty("OCTOPUS_HOME");
if (OCTOPUS_HOME != null) {
if (! (OCTOPUS_HOME.endsWith("\\") || OCTOPUS_HOME.endsWith("/")))
OCTOPUS_HOME += "/";
String url = "file:///" + OCTOPUS_HOME + "XmlTransform/xml/dtd/loaderJob.dtd";
out.write("<!DOCTYPE loaderJob SYSTEM \"" + url + "\">" + "\n");
} else {
URL dtdURL = this.getClass().getClassLoader().getResource("xml/dtd/loaderJob.dtd");
InputStreamReader dtdStream = new InputStreamReader(dtdURL.openStream());
BufferedReader reader = new BufferedReader(dtdStream);
StringBuffer sb = new StringBuffer();
String nextLine = "";
boolean firstLine = true;
while ( (nextLine = reader.readLine()) != null) {
if (!firstLine) {
sb.append(nextLine + "\n");
counter1++;
} else
firstLine = false;
}
out.write("<!DOCTYPE loaderJob [" + sb.toString() + "\n]>");
counter1++;
LocationOfException.setDtdNumber(counter1);
}
}
*/
}
if (name.equalsIgnoreCase("include")) {
String href = atts.getValue("href");
if (href == null) {
System.out.println("Missing href attribute in include Element");
System.exit(1);
}
File file=new File(href);
if(!file.isAbsolute()){
String tmp=this.loaderJobPath + System.getProperty("file.separator") + href;
File fileHref=new File(tmp);
href = fileHref.getCanonicalPath();
}
String parse = atts.getValue("parse");
if (parse == null)
parse = "xml";
if (parse.equals("text")) {
String encoding = atts.getValue("encoding");
includeTextDocument(href, encoding);
} else if (parse.equals("xml")) {
level++;
includeXMLDocument(href);
level--;
} else {
System.out.println("Illegal value for parse attribute: " + parse);
Logger.getCentralLogger().write("normal", "Illegal value for parse attribute: " + parse);
System.exit(1);
}
} else if (name.equalsIgnoreCase("definitionInclude")) {
} else {
out.write("<" + name);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -