📄 xmlreader.java
字号:
/*
* 创建日期 2005-4-27
* 作 者 liug
*/
package com.icbc.util;
import java.sql.SQLException;
import java.util.Vector;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import com.ibm.xml.parsers.DOMParser;
import com.icbc.core.Externalizable;
import com.icbc.core.TLException;
/**
* @author liug
* @time 15:20:28
*/
public class XMLReader {
private String xmlFileName;
private Vector hDocument = new Vector(1);
public XMLReader() {
super();
}
public XMLReader(String fileName) throws Exception {
super();
setDocument(loadXMLFile(fileName));
}
public void setDocument(Document doc) {
if (doc != null) {
hDocument.add(doc);
}
}
public Vector getDocument() {
return hDocument;
}
/**
* @This method was created by ZhongMingChang.
* @05/04/2000
* @return java.lang.Object
* @param node
* org.w3c.dom.Node
*/
protected Object createObject(Node node) throws Exception {
try {
Object o = null;
String elementName = node.getNodeName();
//Node aNode = profile.getElement(elementName);
String classType = null;
try {
classType = node.getAttributes().getNamedItem("impclass")
.getNodeValue();
} catch (Exception e) {
}
if (classType == null) {
if (elementName.equalsIgnoreCase("field")) {
classType = "com.icbc.util.TagField";
} else if (elementName.equalsIgnoreCase("tag")) {
classType = "com.icbc.util.TagElement";
} else if (elementName.equalsIgnoreCase("input")
|| elementName.equalsIgnoreCase("output")) {
classType = "com.icbc.util.DataCollection";
}
}
Class cl = Class.forName(classType);
if (cl == null) {
System.out.println("Exception from XMLReader: class +["
+ classType + "] not found!");
return null;
}
try {
o = cl.newInstance();
} catch (Throwable te) {
throw new TLException("Failed to instance class "
+ cl.toString() + "!");
}
Object o1 = ((Externalizable) o).initializeFrom(node);
if (o1 != null && node.getChildNodes() != null) {
NodeList children = node.getChildNodes();
int childrenSize = children.getLength();
for (int i = 0; i < childrenSize; i++) {
Node child = children.item(i);
if (child == null) {
continue;
}
switch (child.getNodeType()) {
case (Node.TEXT_NODE): {
break;
}
case (Node.ELEMENT_NODE):
{
Object co = createObject(child);
if (co != null) {
((Externalizable) o)
.addElement((Externalizable) co);
} else {
}
break;
}
default: {
break;
}
}
}
}
return o;
} catch (TLException e) {
throw e;
} catch (Exception e) {
throw new TLException("Failed to create the object "
+ node.getNodeName() + " exception: " + e);
}
}
/**
* @This method was created by ZhongMingChang.
* @05/13/2000
*
* @return org.w3c.dom.Node
* @param id
* java.lang.String
*/
public Node getNodeByID(Node parent) {
NodeList children = parent.getChildNodes();
int childrenSize = children.getLength();
for (int i = 0; i < childrenSize; i++) {
Node child = children.item(i);
if (child == null)
continue;
switch (child.getNodeType()) {
case (Node.TEXT_NODE):
{
break;
}
case (Node.ELEMENT_NODE):
{
return child;
// break;
}
default:
{
break;
}
}
}
return null;
}
/**
* @param fileName
* java.lang.String
*/
public Document loadXMLFile(String fileName) throws Exception {
String xml_source;
this.xmlFileName = fileName;
if (fileName.indexOf(":") != -1) {
if (fileName.indexOf(":") <= 2) {
xml_source = "file:///" + fileName;
java.net.URL aURL = new java.net.URL(xml_source);
//getClass().getResource( xml_source );
xml_source = aURL.toString();
} else {
xml_source = fileName;
}
} else {
xml_source = fileName;
}
DOMParser parser = new DOMParser();
try {
parser.parse(xml_source);
} catch (org.xml.sax.SAXParseException se) {
String msg = "file[" + fileName + "] have error: at line["
+ se.getLineNumber() + "] column[" + se.getColumnNumber()
+ "]:" + se.getLocalizedMessage();
System.out.println(msg);
throw new Exception(msg);
} catch (java.io.IOException ioe) {
throw ioe;
} catch (Exception e) {
System.out
.println("Warning - XMLLoader.loadXMLFile(String fileName): The XML parser has not read correctly the XML file , check file "
+ fileName + ": " + e.toString());
//try to read as a stream from a JAR, if XML file not found
java.net.URL aURL = getClass().getResource(xml_source);
System.out
.println("XMLLoader.loadXMLFile(): Now Load the XML file As: "
+ aURL.toString());
parser.parse(aURL.toString());
// doc = documentbuilder.parse(new InputSource(aURL.toString()));
}
Document doc = parser.getDocument();
if (doc == null) {
System.out.println("Error - check file: " + fileName);
return null;
}
return doc;
}
public Node getRootNode(Document document) {
NodeList children = document.getChildNodes();
int childrenSize = children.getLength();
for (int i = 0; i < childrenSize; i++) {
Node child = children.item(i);
switch (child.getNodeType()) {
case (Node.TEXT_NODE):
{
break;
}
case (Node.ELEMENT_NODE):
{
return child;
}
default:
{
break;
}
}
}
return null;
}
/**
* @Creation date: (2002-1-10 16:31:45)
* @return java.lang.Object
* @param name
* java.lang.String
*/
public void readObject() throws java.io.IOException,
TLException, Exception {
for (int idoc = 0; idoc < getDocument().size(); idoc++) {
Document document = (Document) getDocument().get(idoc);
Node aNode = this.getRootNode(document);
if (aNode != null) {
NodeList children = aNode.getChildNodes();
int childrenSize = children.getLength();
for (int i = 0; i < childrenSize; i++) {
Node child = children.item(i);
if (child == null)
continue;
switch (child.getNodeType()) {
case (Node.TEXT_NODE):
{
break;
}
case (Node.ELEMENT_NODE):
{
Object obj = createObject(child);
if(obj instanceof TagElement){
Context ctx = ((TagElement) obj).getContext();
ctx.setParent(Context.getRoot());
Context.getRoot().addChild(ctx);
}
}
default:
{
break;
}
}
}
}
}
}
public static void start() {
try {
Context context = new Context("root");
context.setRoot(context);
XMLReader reader = new XMLReader("cfg\\dbconfig.xml");
reader.readObject();
} catch (SQLException e) {
e.printStackTrace();
} catch (TLException e) {
System.out.println(e.getMessage());
e.printStackTrace();
System.out.println(e.getErrormsg());
} catch (Exception e) {
e.printStackTrace();
}
}
/*
public static void main(String[] args) {
try {
Context context = new Context("root");
context.setRoot(context);
XMLReader reader = new XMLReader("c:/dbconfig.xml");
reader.readObject();
DAO srcDao = new DAO("jdbc:oracle:thin:@122.136.15.13:1521:liaomy",
"DDL",
"DDL",
"oracle.jdbc.driver.OracleDriver");
DAO desDao = new DAO("jdbc:oracle:thin:@122.136.14.166:1521:ora92",
"DDL",
"DDL",
"oracle.jdbc.driver.OracleDriver");
DbCompare dbComp = new DbCompare(srcDao,desDao,srcDao);
String scope = "TABLE(*)";
dbComp.prepareComp(scope,0,Context.getRoot());
String owner = "DDL,DDL";
dbComp.doCompare(owner);
System.out.println(DbCompare.scanResult);
Vector v = new Vector();
} catch (SQLException e) {
e.printStackTrace();
} catch (TLException e) {
// TODO 自动生成 catch 块
System.out.println(e.getMessage());
e.printStackTrace();
System.out.println(e.getErrormsg());
} catch (Exception e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -