001: package org.enhydra.kelp.ant;
002:
003: import java.io.FileOutputStream;
004: import org.w3c.dom.Document;
005: import org.w3c.dom.Node;
006: import org.w3c.dom.NodeList;
007:
008: import org.apache.xml.serialize.OutputFormat;
009: import org.apache.xml.serialize.XMLSerializer;
010: import org.apache.xerces.parsers.DOMParser;
011: import org.apache.xml.serialize.Method;
012:
013: /**
014: * @author Tweety
015: *
016: * To change this generated comment edit the template variable "typecomment":
017: * Window>Preferences>Java>Templates.
018: * To enable and disable the creation of type comments go to
019: * Window>Preferences>Java>Code Generation.
020: */
021: public class AntXMLUtil {
022:
023: //xml document
024: public Document document;
025:
026: //xml document name
027: private String xmlFileName;
028:
029: AntXMLUtil() throws Exception {
030: this ("");
031: }
032:
033: AntXMLUtil(String fileName) throws Exception {
034:
035: this .xmlFileName = fileName;
036: try {
037: DOMParser parser = new DOMParser();
038: parser.parse(xmlFileName);
039: document = parser.getDocument();
040:
041: } catch (Exception e) {
042: throw e;
043: }
044:
045: }
046:
047: /*
048: * @return Node that represents project tag
049: */
050: public Node getProjectNode() {
051: NodeList list = document.getElementsByTagName("project");
052: if (list.getLength() > 0)
053: return list.item(0);
054: else
055: return null;
056: }
057:
058: /*
059: * @param propertyName name attribute of the property tag
060: * @return Node that represents property tag
061: */
062: public Node getProjectProperty(String propertyName) {
063: Node project = this .getProjectNode();
064: NodeList childs = project.getChildNodes();
065: for (int i = 0; i < childs.getLength(); i++) {
066: if (childs.item(i).getNodeName().equals("property")) {
067: Node nameAttribute = childs.item(i).getAttributes()
068: .getNamedItem("name");
069: if (nameAttribute != null
070: && nameAttribute.getNodeValue().equals(
071: propertyName))
072: return childs.item(i);
073: }
074: }
075: return null;
076: }
077:
078: /*
079: * @param targetName: name attribute of the searched target
080: * @return Node that represents target with given name
081: */
082: public Node getTargetByName(String targetName) {
083: NodeList allTargets = document.getElementsByTagName("target");
084: for (int i = 0; i < allTargets.getLength(); i++) {
085: String nameValue = allTargets.item(i).getAttributes()
086: .getNamedItem("name").getNodeValue();
087:
088: if (nameValue.equals(targetName))
089: return allTargets.item(i);
090: }
091: return null;
092: }
093:
094: /*
095: * This method finds child Node in given subtree, with given name
096: * @param subRoot: root node
097: * @param childName: child tag name
098: */
099: public Node getChildByTagName(Node subRoot, String tagName) {
100: NodeList childs = subRoot.getChildNodes();
101: for (int i = 0; i < childs.getLength(); i++) {
102: if (childs.item(i).getNodeName().equals(tagName)) {
103: return childs.item(i);
104: }
105: }
106: return null;
107: }
108:
109: /*
110: * This method finds child attribute Node in given subtree, with given name
111: * @param subRoot: root node
112: * @param attributeName: name of attribute
113: */
114: public Node getAttributeByName(Node subRoot, String attributeName) {
115: return subRoot.getAttributes().getNamedItem(attributeName);
116: }
117:
118: /*
119: * This method finds patternset Node with given id
120: * @param id: id of searched patternset
121: */
122: public Node getPatternsetById(String id) {
123: Node projectNode = this .getProjectNode();
124: NodeList childs = projectNode.getChildNodes();
125: for (int i = 0; i < childs.getLength(); i++) {
126: if (childs.item(i).getNodeName().equals("patternset")) {
127: if (childs.item(i).getAttributes().getNamedItem("id")
128: .getNodeValue().equals(id))
129: return childs.item(i);
130: }
131: }
132:
133: return null;
134: }
135:
136: /*
137: * This method finds filterset Node with given id
138: * @param id: id of searched filterset
139: */
140: public Node getFiltersetById(String id) {
141: Node projectNode = this .getProjectNode();
142: NodeList childs = projectNode.getChildNodes();
143: for (int i = 0; i < childs.getLength(); i++) {
144: if (childs.item(i).getNodeName().equals("filterset")) {
145: if (childs.item(i).getAttributes().getNamedItem("id")
146: .getNodeValue().equalsIgnoreCase(id))
147: return childs.item(i);
148: }
149: }
150:
151: return null;
152: }
153:
154: public boolean saveDocument() {
155:
156: try {
157: FileOutputStream os = new FileOutputStream(xmlFileName);
158: OutputFormat of = new OutputFormat(); //xerces apache.xml.serializer
159: of.setIndenting(true);
160: of.setIndent(4);
161: of.setMethod(Method.XML);
162: of.setPreserveSpace(true);
163: XMLSerializer out = new XMLSerializer(os, of);
164: //IOException
165: out.serialize(document);
166: } catch (Exception e) {
167: e.printStackTrace();
168: return false;
169: }
170: return true;
171: }
172:
173: }
|