📄 writexml.java
字号:
package org.osu.ogsa.stream.util;import org.osu.ogsa.stream.util.xmlconfig.*;import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.FactoryConfigurationError; import javax.xml.parsers.ParserConfigurationException; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; import java.io.File;import java.io.IOException;import java.io.*;import java.net.URL;import org.w3c.dom.Document;import org.w3c.dom.DOMException;import org.w3c.dom.Element;import org.w3c.dom.Node;import org.w3c.dom.NodeList;import org.apache.axis.utils.XMLUtils;import org.apache.log4j.*;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;public class WriteXml{ Document doc; Element app;// private static Category log = Category.getInstance(WriteXml.class.getName()); static Log log = LogFactory.getLog(WriteXml.class.getName()); public WriteXml() throws javax.xml.parsers.ParserConfigurationException { doc = XMLUtils.newDocument(); app = doc.createElement("Application"); app.setAttribute("name", "client"); app.setAttributeNS("http://www.w3.org/2001/XMLSchema-instance", "xsi:noNamespaceSchemaLocation", "schema/stream/Stream/XMLConfigurator.xsd"); } public WriteXml(String schema) throws javax.xml.parsers.ParserConfigurationException { doc = XMLUtils.newDocument(); app = doc.createElement("Application"); app.setAttribute("name", "client"); app.setAttributeNS("http://www.w3.org/2001/XMLSchema-instance", "xsi:noNamespaceSchemaLocation", schema); } public void initXMLConfig_for_dynamic() { Integer tempInt; int numSpots, numStages; String strHandle; if(((Boolean)XMLConfigurator.getParameter("filelogging")).booleanValue()) addPara("filelogging", "Boolean", "true"); else addPara("filelogging", "Boolean", "false"); numStages = ((Integer)XMLConfigurator.getParameter("numStages")).intValue(); addPara("numStages", "Integer", Integer.toString(numStages)); Node topStages = addTopic("stages"); for(int i = 1; i <= numStages; i ++) { log.debug(topStages); String strStage = "stage" + i; Node topStageI = addTopic(topStages, strStage); if(i == 1) { //the number of placements (sources) tempInt = (Integer)XMLConfigurator.getParameter("stages|stage1|numPlacements"); numSpots= tempInt.intValue(); addPara(topStageI, "numPlacements", "Integer", Integer.toString(numSpots)); for(int j = 1; j <= numSpots; j++) { //The placements' information strHandle = ((URL)(XMLConfigurator.getParameter("stages|stage1|placement" + j))).toString(); addObject(topStageI, "placement" + j, "java.net.URL", "String", strHandle); } } //The class's information String strClassName = "stages|" + "stage"+ i+ "|class|class_name"; String strClass = (String)XMLConfigurator.getParameter(strClassName); String strClassLocation = "stages|" + "stage" + (i) + "|class|location"; String strLocation = ((URL)XMLConfigurator.getParameter(strClassLocation)).toString(); addClass(topStageI, strClass, strLocation); //The destination if(i == numStages) { //the number of placements (sources) addPara(topStageI, "numPlacements", "Integer", "1"); //The placements' information strHandle = ((URL)(XMLConfigurator.getParameter("stages|stage"+ numStages + "|placement1"))).toString(); addObject(topStageI, "placement1", "java.net.URL", "String", strHandle); } } XMLUtils.PrettyElementToStream((Element)app, System.out); }// public void initXMLConfig(XMLConfigurator xmlConfig) public void initXMLConfig_for_static() { Integer tempInt; int numSpots, numStages; if(((Boolean)XMLConfigurator.getParameter("filelogging")).booleanValue()) addPara("filelogging", "Boolean", "true"); else addPara("filelogging", "Boolean", "false"); numStages = ((Integer)XMLConfigurator.getParameter("numStages")).intValue(); addPara("numStages", "Integer", Integer.toString(numStages)); Node topStages = addTopic("stages"); for(int i = 1; i <= numStages; i ++) { log.debug(topStages); String strStage = "stage" + i; Node topStageI = addTopic(topStages, strStage); if(i == 1) { //the number of placements (sources) tempInt = (Integer)XMLConfigurator.getParameter("sources|numSpots"); numSpots= tempInt.intValue(); addPara(topStageI, "numPlacements", "Integer", Integer.toString(numSpots)); for(int j = 1; j <= numSpots; j++) { //The placements' information String strIp = (String)(XMLConfigurator.getParameter("sources|spot" + j)); addObject(topStageI, "placement" + j, "java.net.URL", "String", "http://"+strIp+":8080/ogsa/services/Stream/StreamService"); } } //The class's information String strClassName = "stages|" + "stage"+ i+ "|class|class_name"; String strClass = (String)XMLConfigurator.getParameter(strClassName); String strClassLocation = "stages|" + "stage" + (i) + "|class|location"; String strLocation = (String)XMLConfigurator.getParameter(strClassLocation); addClass(topStageI, strClass, strLocation); //The destination if(i == numStages) { //the number of placements (sources) addPara(topStageI, "numPlacements", "Integer", "1"); //The placements' information String strIp = (String)(XMLConfigurator.getParameter("destination")); addObject(topStageI, "placement1", "java.net.URL", "String", "http://"+strIp+":8080/ogsa/services/Stream/StreamService"); } } XMLUtils.PrettyElementToStream((Element)app, System.out); } public Node addTopic(String name) { return addTopic(app, name); } public Node addTopic(Node father, String name) { log.debug(name); Node en = __createTopic(name); log.debug(en); father.appendChild(en); return en; } private Node __createTopic(String name) { Element en = doc.createElement("Topic"); if(en == null) System.out.println("aksdjfaskdjf"); log.debug(en); en.setAttribute("name", name); return en; } public Node addPara(Node father, String name, String type, String value) { Node en = __createParameter(name, type, value); father.appendChild(en); return en; } public Node addPara(String name, String type, String value) { return addPara(app, name, type, value); } private Node __createParameter(String name, String type, String value) { Element en = doc.createElement("Parameter"); Element child_en = doc.createElement(type); Node nodeValue = doc.createTextNode(value); en.appendChild(child_en); child_en.appendChild(nodeValue); en.setAttribute("name", name); return en; } public Node addObject(String name, String class_name, String type, String type_value) { return addObject(app, name, class_name, type, type_value); } public Node addObject(Node father, String name, String class_name, String type, String type_value) { Node en = __createObject(name, class_name, type, type_value); father.appendChild(en); return en; } public Node __createObject(String name, String class_name, String type, String type_value) { Element en = doc.createElement("Object"); Element child_en = doc.createElement(type); Node nodeValue = doc.createTextNode(type_value); en.appendChild(child_en); child_en.appendChild(nodeValue); en.setAttribute("name", name); en.setAttribute("class", class_name); return en; } public Node addObject(int nstage, String name, String class_name, String type, String type_value) { String strStage = "stage" + nstage; log.debug(strStage); Element stage = getElementByAttr("Topic", "name", strStage); if(stage != null) { log.debug("there is a node named "+ strStage); //Node stage = ndlist.item(0); return addObject(stage, name, class_name, type, type_value); } else //Create a stage node { log.debug("there is not a node named "+ strStage); Element stages = getElementByAttr("Topic", "name", "stages"); //Node stages = stageslist.item(0); Node n1 = addTopic(stages, strStage); return addObject(n1, name, class_name, type, type_value); } } public Node addPara(int nstage, String name, String type, String value) { String strStage = "stage" + nstage; Element stage = getElementByAttr("Topic", "name", strStage); if(stage != null) {// Node stage = ndlist.item(0); return addPara(stage, name, type, value); } else //Create a stage node { log.debug("there is not a node named "+ strStage); Element stages = getElementByAttr("Topic", "name", "stages"); //Node stages = stageslist.item(0); Node n1 = addTopic(stages, strStage); return addPara(n1, name, type, value); } } public void addClass(int nstage, String class_name, String location) { String strStage = "stage" + nstage; Element stage = getElementByAttr("Topic", "name", strStage); if(stage != null) {// Node stage = ndlist.item(0); addClass(stage, class_name, location); } else //Create a stage node { Element stages = getElementByAttr("Topic", "name", "stages");// Node stages = stageslist.item(0); Node n1 = addTopic(stages, strStage); addClass(n1, class_name, location); } } private void addClass(Node stage, String class_name, String location) { Node topClass = addTopic(stage, "class"); addPara(topClass, "class_name", "String", class_name); addObject(topClass, "location", "java.net.URL", "String", location); } public void addSpecialClass(String class_name, String location) { Node top = addTopic("special_class"); addPara(top, "class_name", "String", class_name); addObject(top, "location", "java.net.URL", "String", location); } public Element getElementByAttr(String tagName, String attrName, String attrValue) { NodeList elementList = app.getElementsByTagName(tagName); int len = elementList.getLength(); log.debug("length:"+len); while(len > 0) { Element foundNode = (Element)elementList.item(len-1); if(attrValue.equals(foundNode.getAttribute(attrName))) return foundNode; len --; } return null; } public Element getApp() { return app; } public static void main(String args[] ) { try{ WriteXml xml = new WriteXml(); xml.addPara("filelogging", "Boolean", "false"); xml.addPara("numStages", "Integer", "3"); Node topStages = xml.addTopic("stages"); Node topStage1 = xml.addTopic(topStages, "stage1"); xml.addPara(topStage1, "numPlacements", "Integer", "4"); Node topClass = xml.addTopic(topStage1, "class"); xml.addPara(topClass, "class_name", "String", "counting.Producer"); xml.addObject(topClass, "location", "java.net.URL", "String", "http://localhost/"); xml.addObject(topStage1, "placement1", "java.net.URL", "String", "http://localhost:8080/ogsa/services/Stream/StreamService"); xml.addPara(topStage1, "connection1", "String", "placement1"); XMLUtils.PrettyElementToStream(xml.getApp(), System.out); } catch(Exception e) { System.out.println(e); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -