import java.io.*;
import java.util.*;
import java.text.DecimalFormat;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.OutputStream;
import java.io.Writer;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Templates;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.CDATASection;
import org.w3c.dom.CharacterData;
import org.w3c.dom.Comment;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.EntityReference;
import org.w3c.dom.Node;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.NodeList;
import org.w3c.dom.ProcessingInstruction;
import org.w3c.dom.Text;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
class DOMPrinter
{
private static String NODE_TYPES[] = new String[]
{
" ",
"ELEMENT",
"ATTRIBUTE",
"TEXT",
"CDATA_SECTION",
"ENTITY_REFERENCE",
"ENTITY",
"PROCESSING_INSTRUCTION",
"COMMENT",
"DOCUMENT",
"DOCUMENT_TYPE",
"DOCUMENT_FRAGMENT",
"NOTATION"
};
public static void println(String s,int indent)
{
for(int i = 0;i< indent;i++)
{
System.out.print(" ");
}
System.out.println(s);
}
public static void println(String s)
{
println(s,0);
}
public static void print(Node node)
{
printImpl(node,0);
}
private static void printImpl(Node node, int indent)
{
if(node == null)
{
return;
}
String nodeType = NODE_TYPES[node.getNodeType()];
String nodeName = node.getNodeName();
String nodeValue = node.getNodeValue();
if(nodeValue != null)
{
nodeValue = nodeValue.trim();
}
if(nodeType.equals("TEXT")&&nodeValue.equals(" "))
{
;
}
else
{
println(nodeType+"-"+nodeName+"-"+nodeValue,indent);
}
NamedNodeMap attributes = node.getAttributes();
System.out.println("tag 1");
if(attributes != null)
{
for(int i =0;i<attributes.getLength();i++)
{
printImpl(attributes.item(i),indent+1);
}
}
NodeList children = node.getChildNodes();
System.out.println("tag 2");
if(children != null)
{
for(int i =0;i<children.getLength();i++)
{
printImpl(children.item(i),indent+1);
}
}
}
public static DocumentBuilder newBuilder(boolean validation)throws ParserConfigurationException
{
DocumentBuilderFactory domFactory =DocumentBuilderFactory.newInstance();
domFactory.setValidating(validation);
domFactory.setNamespaceAware(true);
DocumentBuilder domBuilder=domFactory.newDocumentBuilder();
//domBuilder.setErrorHandler(new PrintErrorHandler());
return domBuilder;
}
public static Document newDocument()throws ParserConfigurationException
{
DocumentBuilder domBuilder = newBuilder(false);
Document document = domBuilder.newDocument();
return document;
}
public static Document parse(String path,boolean validation)throws ParserConfigurationException,IOException,SAXException
{
DocumentBuilder domBuilder = newBuilder(validation);
Document document = domBuilder.parse(new File(path));
return document;
}
}
class DataUtils //收集字符数据
{
public static String[] getData(Node node)
{
Vector vector = new Vector();
StringBuffer buf = new StringBuffer();
getDataImpl(node,buf,vector);
if(buf.length()>0)
{
vector.add(buf.toString());
}
String array[] = new String[vector.size()];
for(int i = 0;i < array.length;i++)
{
array[i] = (String) vector.get(i);
}
if(array.length == 0)
{
array = new String[]
{
" "
};
}
return array;
}
public static void getDataImpl(Node node,StringBuffer buf,Vector vector)
{
if(node instanceof Text||node instanceof CDATASection)
{
buf.append(((CharacterData)node).getData());
return;
}
Node child = node.getFirstChild();
while(child!=null)
{
if(child instanceof Text||child instanceof CDATASection)
{
buf.append(((CharacterData)node).getData());
}
else if(child instanceof EntityReference)
{
getDataImpl(child,buf,vector);
}
else if(!(child instanceof Comment))
{
if(buf.length() > 0)
{
vector.add(buf.toString());
buf.setLength(0);
}
}
child = child.getNextSibling();
}
}
}
class ElementUtils
{
public static Element[] findElements(Element root,String tagName,String data[],String attrNames[],String attrValues[])
{
return findElementsImpl(root,null,tagName,data,attrNames,attrValues);
}
public static Element[] findElements(Element root,String namespaceURI,String localName,String data[],String attrNames[],String attrValues[])
{
return findElementsImpl(root,namespaceURI,localName,data,attrNames,attrValues);
}
public static Element[] findElementsImpl(Element root,String namespaceURI,String name,String data[],String attrNames[],String attrValues[])
{
Vector vector = new Vector();
findElementsImpl(root,namespaceURI,name,data,attrNames,attrValues,vector);
Element array[] = new Element[vector.size()];
for(int i = 0;i < array.length;i++)
{
array[i] = (Element) vector.get(i);
}
return array;
}
private static void findElementsImpl(Element root,String namespaceURI,String name,String data[],String attrNames[],String attrValues[],Vector vector)
{
boolean flag = false;
if(name == null||(namespaceURI == null&&name.equals(root.getTagName()))||(namespaceURI!=null&&namespaceURI.equals(root.getNamespaceURI())&&name.equals(root.getLocalName())))
{
flag = true;
if(data!=null)
{
String rootdata[] = DataUtils.getData(root);
for(int i = 0;i < data.length;i++)
{
flag = false;
for(int j = 0;j < rootdata.length;j++)
{
if(rootdata[j].indexOf(data[i])!=-1)
{
flag = true;
break;
}
}
if(flag == false){
break;
}
}
}
if(flag&&attrNames!=null&&attrValues!=null)
{
for(int i = 0;i < attrNames.length;i++)
{
String value = root.getAttribute(attrNames[i]);
if(value == null||!value.equals(attrValues[i]))
{
flag = false;
break;
}
}
}
}
if(flag)
{
vector.add(root);
}
Node child = root.getFirstChild();
while(child!=null)
{
if(child instanceof Element)
{
findElementsImpl((Element)child,namespaceURI,name,data,attrNames,attrValues,vector);
}
child = child.getNextSibling();
}
}
}
public class Begin extends DOMPrinter
{
public static void myBegin()//
{
int a;
System.out.println();
System.out.println("请选择:2-查看数据;3-运行计算;4-查看结果;5-退出程序;");
a=G.scanner.nextInt();
if (a==2)
{
View.myView();
}
else if (a==3)
{
Run.myRun();
}
else if(a==4)
{
A4.myA4();
}
else
{
System.out.println();
System.out.println("程序已退出,如要继续计算潮流,请重新运行程序");
System.out.println();
System.exit(1);
}
}
public static void main(String[] args)
{
System.out.println();
System.out.println();
System.out.println();
System.out.println(" 基于CIM的PQ分解法电力系统潮流计算");
System.out.println();
System.out.println(" 指导老师: 设计: 2009年6月");
System.out.print