📄 loadvariable.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
LoadVariable.java
Date: 2.11.2001.
@version 1.0
@author:
Dusan Radeka
*/
package org.webdocwf.util.loader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.util.Vector;
import org.apache.xerces.parsers.SAXParser;
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;
/**
* LoadVariable.java
* This class parses a document (OutputStream) and writes the documents
* contents back to standard output. This class changes variables defined in variable
* tags.
*/
public class LoadVariable extends HandlerBase {
private Writer out;
private String encoding;
private int level = 0;
private Vector vecVariableName = new Vector();
private Vector vecVariableValue = new Vector();
private Vector vecVariablePrefix = new Vector();
private Vector vecVariableSufix = new Vector();
private Vector vecVariableOverride = new Vector();
private Vector vecReplaceInConstants = new Vector();
private Vector vecReplaceInSQL = new Vector();
private Vector vecReplaceInJDBC = new Vector();
private ByteArrayOutputStream streamToParse = new ByteArrayOutputStream();
private boolean bSQL = false;
private boolean bJDBC = false;
private boolean bVarInConstant = false;
/** Main program entry point.
*@param argv is input parameters
*/
public static void main (String argv[]) {
if (argv.length == 0) {
System.out.println("Usage: java LoadVariable uri");
System.out.println(" where uri is the URI of your XML document.");
System.out.println(" Sample: java LoadVariable demo.xml");
}
}
/**
* Construct object LoadVariable with associated outputStream and
* object class Loader. Class uses default "UTF-8" encoding.
* @param out - OutputStream where will be written final XML.
* @param l - object of Loader class. Sets values of variable tags attributes
* and sets POutpuStream witch will be parsed.
*/
public LoadVariable (OutputStream out, Loader l) {
this.vecVariableName = l.vecVariableName;
this.vecVariableValue = l.vecVariableValue;
this.vecVariablePrefix = l.vecVariablePrefix;
this.vecVariableSufix = l.vecVariableSufix;
this.vecVariableOverride = l.vecVariableOverride;
this.vecReplaceInConstants = l.vecReplaceInConstants;
this.vecReplaceInSQL = l.vecReplaceInSQL;
this.vecReplaceInJDBC = l.vecReplaceInJDBC;
this.streamToParse = l.foStreamTmp;
for (int i = 0; i < this.vecReplaceInConstants.size(); i++) {
if (this.vecReplaceInConstants.get(i).toString().equalsIgnoreCase("true"))
bVarInConstant = true;
}
for (int i = 0; i < this.vecReplaceInSQL.size(); i++) {
if (this.vecReplaceInSQL.get(i).toString().equalsIgnoreCase("true"))
bSQL = true;
}
for (int i = 0; i < this.vecReplaceInJDBC.size(); i++) {
if (this.vecReplaceInJDBC.get(i).toString().equalsIgnoreCase("true"))
bJDBC = true;
}
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 .
*/
public void parseURI () {
try {
SAXParser parser = new SAXParser();
parser.setDocumentHandler(this);
parser.setErrorHandler(this);
parser.parse(new InputSource(new ByteArrayInputStream(this.streamToParse.toByteArray())));
} catch (Exception e) {
System.err.println(e);
}
}
/** 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("?>");
} catch (IOException e) {
System.err.println(e);
}
}
/** Start document. */
public void startDocument () {
if (level == 0) {
try {
out.write("<?xml version=\"1.0\"?>\r\n");
} catch (IOException e) {
System.err.println(e);
}
}
}
/** Start element.
* @param name is the name of the tag
* @param atts is attributes if tag
*/
public void startElement (String name, AttributeList atts) {
try {
if (name.equalsIgnoreCase("constantColumn")) {
out.write("<" + name);
for (int i = 0; i < atts.getLength(); i++) {
out.write(" ");
out.write(atts.getName(i));
out.write("='");
String value = atts.getValue(i);
if (atts.getName(i).equalsIgnoreCase("constantValue")) {
// checking, and if Variable exists changing
if (bVarInConstant) {
// going throw vector
for (int k = 0; k < this.vecReplaceInConstants.size(); k++) {
// if this is to change
if (this.vecReplaceInConstants.get(k).toString().equalsIgnoreCase("true")) {
String sPreNameSu = this.vecVariablePrefix.get(k).toString()
+ this.vecVariableName.get(k).toString()
+ this.vecVariableSufix.get(k).toString();
// if costant is variable
if (value.equalsIgnoreCase(sPreNameSu)) {
value = this.vecVariableValue.get(k).toString();
}
}
}
}
}
// + 4 allows space for one entitiy reference.
// If there's more than that, then the StringBuffer
// will automatically expand
// Need to use character references if the encoding
// can't support the character
StringBuffer encodedValue = new StringBuffer(value.length()
+ 4);
for (int j = 0; j < value.length(); j++) {
char c = value.charAt(j);
if (c == '&')
encodedValue.append("&");
else if (c == '<')
encodedValue.append("<");
else if (c == '>')
encodedValue.append(">");
else if (c == '\'')
encodedValue.append("'");
else
encodedValue.append(c);
}
out.write(encodedValue.toString());
out.write("'");
}
out.write(">");
}
else if (name.equalsIgnoreCase("jdbcSourceParameter")) {
out.write("<" + name);
for (int i = 0; i < atts.getLength(); i++) {
out.write(" ");
out.write(atts.getName(i));
out.write("='");
String value = atts.getValue(i);
if (atts.getName(i).equalsIgnoreCase("value")) {
// checking, and if Variable exists changing
if (bJDBC) {
// going throw vector
for (int k = 0; k < this.vecReplaceInJDBC.size(); k++) {
// if this is to change
if (this.vecReplaceInJDBC.get(k).toString().equalsIgnoreCase("true")) {
String sPreNameSu = this.vecVariablePrefix.get(k).toString()
+ this.vecVariableName.get(k).toString()
+ this.vecVariableSufix.get(k).toString();
// if costant is variable
// if (value.equalsIgnoreCase(sPreNameSu)) {
// value = this.vecVariableValue.get(k).toString();
// }
value = Utils.replaceAll(value, sPreNameSu, this.vecVariableValue.get(k).toString());
}
}
}
}
// + 4 allows space for one entitiy reference.
// If there's more than that, then the StringBuffer
// will automatically expand
// Need to use character references if the encoding
// can't support the character
StringBuffer encodedValue = new StringBuffer(value.length()
+ 4);
for (int j = 0; j < value.length(); j++) {
char c = value.charAt(j);
if (c == '&')
encodedValue.append("&");
else if (c == '<')
encodedValue.append("<");
else if (c == '>')
encodedValue.append(">");
else if (c == '\'')
encodedValue.append("'");
else
encodedValue.append(c);
}
out.write(encodedValue.toString());
out.write("'");
}
out.write(">");
}
else if (name.equalsIgnoreCase("jdbcTargetParameter")) {
out.write("<" + name);
for (int i = 0; i < atts.getLength(); i++) {
out.write(" ");
out.write(atts.getName(i));
out.write("='");
String value = atts.getValue(i);
if (atts.getName(i).equalsIgnoreCase("value")) {
// checking, and if Variable exists changing
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -