📄 readxml.java
字号:
package ding;
import org.w3c.dom.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import javax.xml.parsers.*;
import javax.swing.*;
/**
* <p>Title: 门电路画图器</p>
* <p>Description: www.1shu.net</p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: EyeSome</p>
* @author ES
* @version 1.0
*/
public class readXml
extends DefaultHandler {
/**
* 每个构件都初始40个,就是说每个元件的每种构件最大为40
*/
yjRec inRec[] = new yjRec[40];
yjLine inLine[] = new yjLine[40];
yjCircle inCircle[] = new yjCircle[40];
yjStr inStr[] = new yjStr[40];
yjArc inArc[] = new yjArc[40];
private int yuanjianNo = -1, lineNo = -1, recNo = -1, strNo = -1, arcNo = -1,
circleNo = -1;
private int yuanjianFlag = 0, lineFlag = 0, recFlag = 0, strFlag = 0,
arcFlag = 0, circleFlag = 0;
mainFrame mFrame;
private int index;
private Locator locator;
boolean yuanjianTemp = false;
String picpath, des;
int id;
//Constructor
public readXml(mainFrame frame) {
super(); //it must be done !
mFrame = frame;
yuanjianNo = -1;
}
public readXml(mainFrame frame, int tyuanjianNo) {
super(); //it must be done !
mFrame = frame;
this.yuanjianNo = tyuanjianNo;
}
public readXml(mainFrame frame, boolean yuanjianTemp) {
super(); //it must be done !
mFrame = frame;
this.yuanjianTemp = yuanjianTemp;
}
//nain method
/**public void readXmlMain(mainFrame frame) {
mFrame = frame;
try {
SAXParserFactory sf = SAXParserFactory.newInstance();
SAXParser sp = sf.newSAXParser();
readXml reader = new readXml();
sp.parse(new InputSource("src\\ding\\file.xml"), reader);
}
catch (Exception e) {
e.printStackTrace();
}
}*/
//Response the startDocument event
public void startDocument() {
}
//Response the startElement event
public void startElement(String uri, String localName, String qName,
Attributes attrs) {
int attrCount, i;
//int x, y, r, width, height, x1, y1, x2, y2, startDu, Du, typeDu;
//String str;
if (yuanjianFlag == 1) {
if (lineFlag == 1) {
if (qName.equalsIgnoreCase("line")) {
lineNo++; //计数加一
inLine[lineNo] = new yjLine();
inLine[lineNo].setValue(Integer.parseInt(attrs.getValue(0)),
Integer.parseInt(attrs.getValue(1)),
Integer.parseInt(attrs.getValue(2)),
Integer.parseInt(attrs.getValue(3)));
}
}
if (recFlag == 1) {
if (qName.equalsIgnoreCase("rec")) {
recNo++;
inRec[recNo] = new yjRec();
inRec[recNo].setValue(Integer.parseInt(attrs.getValue(0)),
Integer.parseInt(attrs.getValue(1)),
Integer.parseInt(attrs.getValue(2)),
Integer.parseInt(attrs.getValue(3)));
}
}
if (strFlag == 1) {
if (qName.equalsIgnoreCase("str")) {
strNo++;
inStr[strNo] = new yjStr();
inStr[strNo].setValue(Integer.parseInt(attrs.getValue(0)),
Integer.parseInt(attrs.getValue(1)),
attrs.getValue(2));
}
}
if (arcFlag == 1) {
if (qName.equalsIgnoreCase("arc")) {
arcNo++;
inArc[arcNo] = new yjArc();
inArc[arcNo].setValue(Integer.parseInt(attrs.getValue(0)),
Integer.parseInt(attrs.getValue(1)),
Integer.parseInt(attrs.getValue(2)),
Integer.parseInt(attrs.getValue(3)),
Integer.parseInt(attrs.getValue(4)),
Integer.parseInt(attrs.getValue(5)),
Integer.parseInt(attrs.getValue(6)));
}
}
if (circleFlag == 1) {
if (qName.equalsIgnoreCase("circle")) {
circleNo++;
inCircle[circleNo] = new yjCircle();
inCircle[circleNo].setValue(Integer.parseInt(attrs.getValue(0)),
Integer.parseInt(attrs.getValue(1)),
Integer.parseInt(attrs.getValue(2)),
Integer.parseInt(attrs.getValue(3)));
}
}
}
if (qName.equalsIgnoreCase("yuanjian")) {
yuanjianFlag = 1;
if (yuanjianTemp) {
picpath = attrs.getValue(0);
des = attrs.getValue(1);
}else{
id = Integer.parseInt(attrs.getValue(0));
des = attrs.getValue(1);
}
}
if (qName.equalsIgnoreCase("linep")) {
lineFlag = 1;
}
if (qName.equalsIgnoreCase("recp")) {
recFlag = 1;
}
if (qName.equalsIgnoreCase("arcp")) {
arcFlag = 1;
}
if (qName.equalsIgnoreCase("circlep")) {
circleFlag = 1;
}
if (qName.equalsIgnoreCase("strp")) {
strFlag = 1;
}
if (qName.equalsIgnoreCase("yjt")) {
mFrame.yjt[Integer.parseInt(attrs.getValue(0))].number = Integer.parseInt(
attrs.getValue(1));
}
}
//Response the endDocument event
public void endDocument() {
}
//Response the endElement event
public void endElement(String uri, String localName, String qName) {
int i, j;
if (qName.equalsIgnoreCase("yuanjian")) {
//System.out.println("recNo:" + recNo + " lineNo:" + lineNo + " circleNo:" +
// circleNo + " strNo:" + strNo + " arcNo:" + arcNo);
yjRec tRec[] = new yjRec[recNo + 1];
yjLine tLine[] = new yjLine[lineNo + 1];
yjCircle tCircle[] = new yjCircle[circleNo + 1];
yjStr tStr[] = new yjStr[strNo + 1];
yjArc tArc[] = new yjArc[arcNo + 1];
if (lineNo >= 0) {
//yjLine tLine[] = new yjLine[lineNo + 1];
for (i = 0; i <= lineNo; i++) {
tLine[i] = new yjLine();
tLine[i] = inLine[i].getValue();
}
}
else {
tLine = null;
}
if (recNo >= 0) {
//yjRec tRec[] = new yjRec[recNo + 1];
for (i = 0; i <= recNo; i++) {
tRec[i] = inRec[i];
}
}
else {
tRec = null;
}
if (arcNo >= 0) {
//yjArc tArc[] = new yjArc[arcNo + 1];
for (i = 0; i <= arcNo; i++) {
tArc[i] = inArc[i];
}
}
else {
tArc = null;
}
if (circleNo >= 0) {
//yjCircle tCircle[] = new yjCircle[circleNo + 1];
for (i = 0; i <= circleNo; i++) {
tCircle[i] = inCircle[i];
}
}
else {
tCircle = null;
}
if (strNo >= 0) {
//yjStr tStr[] = new yjStr[strNo + 1];
for (i = 0; i <= strNo; i++) {
tStr[i] = inStr[i];
}
}
else {
tStr = null;
}
yuanjianNo++;
//System.out.println("| yuanjianNo:" + yuanjianNo);
//System.out.println("yuanjianNo:" + yuanjianNo);
if (!yuanjianTemp) {
mFrame.yjc[yuanjianNo] = new yuanjian(tRec, tLine, tCircle, tStr, tArc);
//System.out.println("yuanjianNo:" + yuanjianNo);
mFrame.yjc[yuanjianNo].des = des;
mFrame.yjc[yuanjianNo].id = id;
//System.out.println(id);
}
else {
mFrame.yjt[yuanjianNo] = new yuanjiantemp(tRec, tLine, tCircle, tStr,
tArc);
mFrame.yjt[yuanjianNo].picPath = this.picpath;
mFrame.yjt[yuanjianNo].des = des;
mFrame.yjt[yuanjianNo].id = yuanjianNo;
}
lineNo = -1;
recNo = -1;
strNo = -1;
arcNo = -1;
circleNo = -1;
yuanjianFlag = 0;
}
if (qName.equalsIgnoreCase("linep")) {
lineFlag = 0;
}
if (qName.equalsIgnoreCase("recp")) {
recFlag = 0;
}
if (qName.equalsIgnoreCase("arcp")) {
arcFlag = 0;
}
if (qName.equalsIgnoreCase("circlep")) {
circleFlag = 0;
}
if (qName.equalsIgnoreCase("strp")) {
strFlag = 0;
}
if (qName.equalsIgnoreCase("file")) {
if (!yuanjianTemp) {
mFrame.yjcNo = yuanjianNo + 1;
}
else {
mFrame.yjtNo = yuanjianNo + 1;
}
}
}
//Print the fata error information
public void fatalError(SAXParseException e) {
/*System.out.println("\nFatal error information -->");
System.out.println("\t" + e.getMessage());
System.out.println("\tAt line " + locator.getLineNumber() +
",column " + locator.getColumnNumber());*/
JOptionPane.showMessageDialog(null, "严重的错误:" + e.getMessage());
}
//Print the usual error information
public void error(SAXParseException e) {
/*System.out.println("\nUsual error information -->");
System.out.println("\t" + e.getMessage());
System.out.println("\tAt line " + locator.getLineNumber() +
",column " + locator.getColumnNumber());*/
JOptionPane.showMessageDialog(null, "错误:" + e.getMessage());
}
//Print the warning information
public void warning(SAXParseException e) {
System.out.println("\nWarning information -->");
System.out.println("\t" + e.getMessage());
System.out.println("\tAt line " + locator.getLineNumber() + ",column " +
locator.getColumnNumber());
}
//Store the error locator object
public void setDocumentLocator(Locator lct) {
locator = lct;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -