001: /*
002: * This file is part of PFIXCORE.
003: *
004: * PFIXCORE is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU Lesser General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * PFIXCORE is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public License
015: * along with PFIXCORE; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: */
019:
020: package de.schlund.pfixcore.util.basicapp.helper;
021:
022: import org.w3c.dom.Document;
023: import org.w3c.dom.Element;
024: import org.w3c.dom.Node;
025: import org.w3c.dom.NodeList;
026:
027: /**
028: * Like @see de.schlund.pfixcore.util.basicapp.helper.StringUtils
029: * there also exists a XmlUtils Object with static methods
030: * (it's this one ;o) to convert the current dom
031: *
032: * @author <a href="mailto:rapude@schlund.de">Ralf Rapude</a>
033: * @version $Id: XmlUtils.java 1089 2004-06-07 14:04:39Z rrapude $
034: */
035: public class XmlUtils {
036:
037: /**
038: * Changing the attribute of a document in order to apply
039: * the changes made by the user
040: * @param domDoc The current document
041: * @param tagName The name of the tag changing the related attribute
042: * @param attribute The attribtute to change
043: * @param secNodeListItem TODO
044: * @param docName The name of the document in order to
045: * avoid problems if a node occurs in different files with the
046: * same name
047: */
048: public static Document changeAttributes(Document domDoc,
049: String tagName, String attribute, String newValue,
050: boolean secNodeListItem) {
051: System.out.println("Changing the attribute " + attribute);
052:
053: NodeList nodeList = domDoc.getElementsByTagName(tagName);
054: String myNodeName = null;
055: Element element = null;
056:
057: if (!secNodeListItem) {
058:
059: for (int i = 0; i < nodeList.getLength(); i++) {
060: element = (Element) nodeList.item(i);
061: myNodeName = element.getNodeName();
062:
063: if (myNodeName.equals(tagName)) {
064: element.setAttribute(attribute, newValue);
065: }
066: }
067: } else {
068: element = (Element) nodeList.item(1);
069: element.setAttribute(attribute, newValue);
070: }
071:
072: System.out.println("Changing attribute has been successfull");
073: return domDoc;
074: }
075:
076: /**
077: * Changing the text nodes of the config files
078: * @param domDoc a Document containing the dom
079: * @param tagName the tag to change
080: * @param value The nodes new content
081: */
082: public static Document changeTextValues(Document domDoc,
083: String tagName, String value, boolean firstChild) {
084: System.out.println("Changing the value of the tag " + tagName);
085:
086: NodeList nodeList = domDoc.getElementsByTagName(tagName);
087: String myNodeName = null;
088: Node oldValue = null;
089: Node newValue = domDoc.createTextNode(value);
090:
091: // running through the doms nodes
092: for (int i = 0; i < nodeList.getLength(); i++) {
093: Element element = (Element) nodeList.item(i);
094: myNodeName = element.getNodeName();
095:
096: // pick the right one out
097: if (myNodeName.equals(tagName)) {
098: // firstChild is a boolean given as an argument
099: if (firstChild) {
100: if (element.getFirstChild() != null) {
101: oldValue = element.getFirstChild();
102: } else {
103: oldValue = element.getNextSibling();
104: }
105: } else {
106: oldValue = element.getLastChild();
107: }
108: element.replaceChild(newValue, oldValue);
109: }
110:
111: }
112: System.out.println("Changing Textnode has been successfull\n");
113: return domDoc;
114: }
115: }
|