001: /**
002: OctopusXMLUtil - Utils for reading attributes form xml tags.
003:
004: Copyright (C) 2002-2003 Together
005:
006: This library is free software; you can redistribute it and/or
007: modify it under the terms of the GNU Lesser General Public
008: License as published by the Free Software Foundation; either
009: version 2.1 of the License, or (at your option) any later version.
010:
011: This library is distributed in the hope that it will be useful,
012: but WITHOUT ANY WARRANTY; without even the implied warranty of
013: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: Lesser General Public License for more details.
015:
016: You should have received a copy of the GNU Lesser General Public
017: License along with this library; if not, write to the Free Software
018: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019:
020: OctopusXMLUtil.java
021: Date: 20.5.2003.
022: @version 1.0.0
023: @author: Zoran Milakovic zoran@prozone.co.yu
024: */package org.webdocwf.util.loader;
025:
026: import java.util.Vector;
027: import org.w3c.dom.*;
028:
029: /**
030: * Class has utility methods.
031: *
032: * @author Zoran Milakovic
033: * @version 1.1
034: */
035: public class OctopusXMLUtil {
036:
037: /**
038: * Gets attribute values from specified tags.
039: * @param tags NodeList of tags to check for attribute
040: * @param attrName name of attribute
041: * @param defaultValue defaultValue if attribute is not defined
042: * @return vector which contains values
043: */
044: public static Vector getAttributeValues(NodeList tags,
045: String attrName, String defaultValue) {
046: Vector vecValues = new Vector();
047: String nodeValue = "";
048: if (attrName != null) {
049: for (int i = 0; i < tags.getLength(); i++) {
050: NamedNodeMap attrs = tags.item(i).getAttributes();
051: Node nodeResult = attrs.getNamedItem(attrName);
052: if (nodeResult != null)
053: nodeValue = nodeResult.getNodeValue();
054: else
055: nodeValue = defaultValue;
056: vecValues.addElement(nodeValue);
057: }
058: }
059: return vecValues;
060: }
061:
062: /**
063: * Method importAttributeValue reads value for strAttrName attribute in strTagName tag.
064: * This method return this value.
065: * @param doc Parsed import XML file.
066: * @param strTagName The name of tag where attribute is situated.
067: * @param strAttrName The name of tag attribute which reads input value.
068: * @param iImportJobItem Number of ImportDefinition tag which is processed.
069: * @return String - importing value.
070: */
071: public static String importAttributeValue(Document doc,
072: String strTagName, String strAttrName, int iImportJobItem) {
073: String strValue = "";
074: NodeList tagBasic = doc.getElementsByTagName(strTagName);
075: Element docFragment = (Element) tagBasic.item(iImportJobItem);
076: if (docFragment != null)
077: strValue = docFragment.getAttribute(strAttrName);
078: return strValue;
079: }
080:
081: /**
082: * Method importAttribute reads value for strAttrName attribute in strTagName tag.
083: * This method return this value.
084: * @param doc Parsed import XML file.
085: * @param strTagName The name of tag where attribute is situated.
086: * @param strAttrName The name of tag attribute which reads input value.
087: * @param iImportJobItem Number of ImportDefinition tag which is processed.
088: * @return String - importing value.
089: */
090: public static String importAttribute(Document doc,
091: String strTagName, String strAttrName, int iImportJobItem) {
092: String strValue = "";
093: NodeList tagBasic = doc
094: .getElementsByTagName("importDefinition");
095: if (tagBasic.getLength() != 0) {
096: Element docFragment = (Element) tagBasic
097: .item(iImportJobItem);
098: // NodeList tag = docFragment.getElementsByTagName(tagName);
099: // for (int i = 0; i < tag.getLength(); i++) {
100:
101: tagBasic = docFragment.getElementsByTagName(strTagName);
102: if (tagBasic.getLength() != 0) {
103: docFragment = (Element) tagBasic.item(0);
104: if (docFragment != null)
105: strValue = docFragment.getAttribute(strAttrName);
106: }
107: }
108: return strValue;
109: }
110:
111: /**
112: * Method importValue reads values from desired XML tag and puts them into Vector.
113: * @param doc Parsed import XML file.
114: * @param tagName The name of XML tag.
115: * @param strAttrName The name of tag attribute which reads input strValue.
116: * @param iImportJobItem Number of ImportDefinition tag which is processed.
117: * @return Vector of importing values.
118: */
119: public static Vector importValue(Document doc, String tagName,
120: String strAttrName, int iImportJobItem) {
121: Vector strValue = new Vector();
122: NodeList tagBasic = doc
123: .getElementsByTagName("importDefinition");
124: if (tagBasic.getLength() != 0) {
125: Element docFragment = (Element) tagBasic
126: .item(iImportJobItem);
127: NodeList tag = docFragment.getElementsByTagName(tagName);
128: for (int i = 0; i < tag.getLength(); i++) {
129: String nodeValue = "";
130: if (strAttrName != null) {
131: NamedNodeMap attrs = tag.item(i).getAttributes();
132: Node nodeResult = attrs.getNamedItem(strAttrName);
133: if (nodeResult != null)
134: nodeValue = nodeResult.getNodeValue();
135: strValue.addElement(nodeValue);
136: } else {
137: NodeList nodeText = tag.item(i).getChildNodes();
138: if (nodeText.item(0) != null) {
139: nodeValue = nodeText.item(0).getNodeValue();
140: strValue.addElement(nodeValue);
141: }
142: }
143: }
144: }
145: return strValue;
146: }
147:
148: /**
149: * Method importValue reads values from desired XML tag and puts them into Vector.
150: * @param docFragment Parsed import XML file.
151: * @param tagName The name of XML tag.
152: * @param strAttrName The name of tag attribute which reads input strValue.
153: * @return Vector of importing values.
154: */
155: public static Vector importValueForTransform(Element docFragment,
156: String tagName, String strAttrName) {
157:
158: Vector strValue = new Vector();
159: NodeList tag = docFragment.getElementsByTagName(tagName);
160: for (int i = 0; i < tag.getLength(); i++) {
161: String nodeValue = "";
162: if (strAttrName != null) {
163: NamedNodeMap attrs = tag.item(i).getAttributes();
164: Node nodeResult = attrs.getNamedItem(strAttrName);
165: if (nodeResult != null)
166: nodeValue = nodeResult.getNodeValue();
167: strValue.addElement(nodeValue);
168: } else {
169: NodeList nodeText = tag.item(i).getChildNodes();
170: if (nodeText.item(0) != null) {
171: nodeValue = nodeText.item(0).getNodeValue();
172: strValue.addElement(nodeValue);
173: }
174: }
175: }
176: return strValue;
177: }
178:
179: /**
180: * Method importValue reads values from desired XML tag and puts them into Vector.
181: * @param doc Parsed import XML file.
182: * @param tagName The name of XML tag.
183: * @param strAttrName The name of tag attribute which reads input strValue.
184: * @param iImportJobItem Number of ImportDefinition tag which is processed.
185: * @param defaultValue The default value of strattrname attribute.
186: * @return Vector of importing values.
187: */
188: public static Vector importValue(Document doc, String tagName,
189: String strAttrName, int iImportJobItem, String defaultValue) {
190: Vector strValue = new Vector();
191: NodeList tagBasic = doc
192: .getElementsByTagName("importDefinition");
193: if (tagBasic.getLength() != 0) {
194: Element docFragment = (Element) tagBasic
195: .item(iImportJobItem);
196: NodeList tag = docFragment.getElementsByTagName(tagName);
197: for (int i = 0; i < tag.getLength(); i++) {
198: String nodeValue = "";
199: if (strAttrName != null) {
200: NamedNodeMap attrs = tag.item(i).getAttributes();
201: Node nodeResult = attrs.getNamedItem(strAttrName);
202: if (nodeResult != null)
203: nodeValue = nodeResult.getNodeValue();
204: else
205: nodeValue = defaultValue;
206: strValue.addElement(nodeValue);
207: } else {
208: NodeList nodeText = tag.item(i).getChildNodes();
209: if (nodeText.item(0) != null) {
210: nodeValue = nodeText.item(0).getNodeValue();
211: strValue.addElement(nodeValue);
212: }
213: }
214: }
215: }
216: return strValue;
217: }
218:
219: /**
220: * @return Vector of importing values.
221: *
222: */
223: //ZK change this 16.04.2004.Change Return type from Document to Element
224: public static Element getDocumentFragment(Document doc,
225: String tagName, int iCurrent, int iImportJobItem) {
226:
227: NodeList tagBasic = doc
228: .getElementsByTagName("importDefinition");
229: if (tagBasic.getLength() != 0) {
230: Element docFragment = (Element) tagBasic
231: .item(iImportJobItem);
232: NodeList tag = docFragment.getElementsByTagName(tagName);
233: if (iCurrent < tag.getLength())
234: return (Element) tag.item(iCurrent);
235: else
236: return null;
237: } else
238: return null;
239: }
240:
241: }
|