KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > webdocwf > util > loader > OctopusXMLUtil


1 /**
2   OctopusXMLUtil - Utils for reading attributes form xml tags.
3
4     Copyright (C) 2002-2003 Together
5
6     This library is free software; you can redistribute it and/or
7     modify it under the terms of the GNU Lesser General Public
8     License as published by the Free Software Foundation; either
9     version 2.1 of the License, or (at your option) any later version.
10
11     This library is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14     Lesser General Public License for more details.
15
16     You should have received a copy of the GNU Lesser General Public
17     License along with this library; if not, write to the Free Software
18     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
20  OctopusXMLUtil.java
21  Date: 20.5.2003.
22  @version 1.0.0
23  @author: Zoran Milakovic zoran@prozone.co.yu
24  */

25
26 package org.webdocwf.util.loader;
27
28 import java.util.Vector JavaDoc;
29 import org.w3c.dom.*;
30
31 /**
32  * Class has utility methods.
33  *
34  * @author Zoran Milakovic
35  * @version 1.1
36  */

37 public class OctopusXMLUtil {
38
39
40   /**
41    * Gets attribute values from specified tags.
42    * @param tags NodeList of tags to check for attribute
43    * @param attrName name of attribute
44    * @param defaultValue defaultValue if attribute is not defined
45    * @return vector which contains values
46    */

47   public static Vector JavaDoc getAttributeValues (NodeList tags, String JavaDoc attrName, String JavaDoc defaultValue) {
48     Vector JavaDoc vecValues = new Vector JavaDoc();
49     String JavaDoc nodeValue = "";
50     if (attrName != null) {
51       for (int i = 0; i < tags.getLength(); i++) {
52         NamedNodeMap attrs = tags.item(i).getAttributes();
53         Node nodeResult = attrs.getNamedItem(attrName);
54         if (nodeResult != null)
55           nodeValue = nodeResult.getNodeValue();
56         else
57           nodeValue = defaultValue;
58         vecValues.addElement(nodeValue);
59       }
60     }
61     return vecValues;
62   }
63
64   /**
65    * Method importAttributeValue reads value for strAttrName attribute in strTagName tag.
66    * This method return this value.
67    * @param doc Parsed import XML file.
68    * @param strTagName The name of tag where attribute is situated.
69    * @param strAttrName The name of tag attribute which reads input value.
70    * @param iImportJobItem Number of ImportDefinition tag which is processed.
71    * @return String - importing value.
72    */

73   public static String JavaDoc importAttributeValue (Document doc, String JavaDoc strTagName, String JavaDoc strAttrName,
74       int iImportJobItem) {
75     String JavaDoc strValue = "";
76     NodeList tagBasic = doc.getElementsByTagName(strTagName);
77     Element docFragment = (Element)tagBasic.item(iImportJobItem);
78     if (docFragment != null)
79       strValue = docFragment.getAttribute(strAttrName);
80     return strValue;
81   }
82
83   /**
84    * Method importAttribute reads value for strAttrName attribute in strTagName tag.
85    * This method return this value.
86    * @param doc Parsed import XML file.
87    * @param strTagName The name of tag where attribute is situated.
88    * @param strAttrName The name of tag attribute which reads input value.
89    * @param iImportJobItem Number of ImportDefinition tag which is processed.
90    * @return String - importing value.
91    */

92   public static String JavaDoc importAttribute(Document doc, String JavaDoc strTagName, String JavaDoc strAttrName,
93                                        int iImportJobItem) {
94     String JavaDoc strValue = "";
95     NodeList tagBasic = doc.getElementsByTagName("importDefinition");
96     if (tagBasic.getLength() != 0) {
97       Element docFragment = (Element)tagBasic.item(iImportJobItem);
98 // NodeList tag = docFragment.getElementsByTagName(tagName);
99
// 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 JavaDoc importValue (Document doc, String JavaDoc tagName, String JavaDoc strAttrName,
120                                     int iImportJobItem) {
121     Vector JavaDoc strValue = new Vector JavaDoc();
122     NodeList tagBasic = doc.getElementsByTagName("importDefinition");
123     if (tagBasic.getLength() != 0) {
124       Element docFragment = (Element)tagBasic.item(iImportJobItem);
125       NodeList tag = docFragment.getElementsByTagName(tagName);
126       for (int i = 0; i < tag.getLength(); i++) {
127         String JavaDoc nodeValue = "";
128         if (strAttrName != null) {
129           NamedNodeMap attrs = tag.item(i).getAttributes();
130           Node nodeResult = attrs.getNamedItem(strAttrName);
131           if (nodeResult != null)
132             nodeValue = nodeResult.getNodeValue();
133           strValue.addElement(nodeValue);
134         }
135         else {
136           NodeList nodeText = tag.item(i).getChildNodes();
137           if (nodeText.item(0) != null) {
138             nodeValue = nodeText.item(0).getNodeValue();
139             strValue.addElement(nodeValue);
140           }
141         }
142       }
143     }
144     return strValue;
145   }
146   /**
147      * Method importValue reads values from desired XML tag and puts them into Vector.
148      * @param docFragment Parsed import XML file.
149      * @param tagName The name of XML tag.
150      * @param strAttrName The name of tag attribute which reads input strValue.
151      * @return Vector of importing values.
152      */

153     public static Vector JavaDoc importValueForTransform (Element docFragment, String JavaDoc tagName, String JavaDoc strAttrName) {
154         
155       Vector JavaDoc strValue = new Vector JavaDoc();
156       NodeList tag = docFragment.getElementsByTagName(tagName);
157         for (int i = 0; i < tag.getLength(); i++) {
158           String JavaDoc nodeValue = "";
159           if (strAttrName != null){
160             NamedNodeMap attrs = tag.item(i).getAttributes();
161             Node nodeResult = attrs.getNamedItem(strAttrName);
162             if (nodeResult != null)
163               nodeValue = nodeResult.getNodeValue();
164                   strValue.addElement(nodeValue);
165           }
166           else {
167             NodeList nodeText = tag.item(i).getChildNodes();
168             if (nodeText.item(0) != null) {
169               nodeValue = nodeText.item(0).getNodeValue();
170               strValue.addElement(nodeValue);
171             }
172           }
173       }
174       return strValue;
175     }
176   /**
177    * Method importValue reads values from desired XML tag and puts them into Vector.
178    * @param doc Parsed import XML file.
179    * @param tagName The name of XML tag.
180    * @param strAttrName The name of tag attribute which reads input strValue.
181    * @param iImportJobItem Number of ImportDefinition tag which is processed.
182    * @param defaultValue The default value of strattrname attribute.
183    * @return Vector of importing values.
184    */

185   public static Vector JavaDoc importValue (Document doc, String JavaDoc tagName, String JavaDoc strAttrName,
186                                     int iImportJobItem, String JavaDoc defaultValue) {
187     Vector JavaDoc strValue = new Vector JavaDoc();
188     NodeList tagBasic = doc.getElementsByTagName("importDefinition");
189     if (tagBasic.getLength() != 0) {
190       Element docFragment = (Element)tagBasic.item(iImportJobItem);
191       NodeList tag = docFragment.getElementsByTagName(tagName);
192       for (int i = 0; i < tag.getLength(); i++) {
193         String JavaDoc nodeValue = "";
194         if (strAttrName != null) {
195           NamedNodeMap attrs = tag.item(i).getAttributes();
196           Node nodeResult = attrs.getNamedItem(strAttrName);
197           if (nodeResult != null)
198             nodeValue = nodeResult.getNodeValue();
199           else
200             nodeValue = defaultValue;
201           strValue.addElement(nodeValue);
202         }
203         else {
204           NodeList nodeText = tag.item(i).getChildNodes();
205           if (nodeText.item(0) != null) {
206             nodeValue = nodeText.item(0).getNodeValue();
207             strValue.addElement(nodeValue);
208           }
209         }
210       }
211     }
212     return strValue;
213   }
214   
215
216   /**
217    * @return Vector of importing values.
218    *
219    */

220   //ZK change this 16.04.2004.Change Return type from Document to Element
221
public static Element getDocumentFragment (Document doc, String JavaDoc tagName, int iCurrent,
222                                     int iImportJobItem) {
223
224     NodeList tagBasic = doc.getElementsByTagName("importDefinition");
225     if (tagBasic.getLength() != 0) {
226       Element docFragment = (Element)tagBasic.item(iImportJobItem);
227       NodeList tag = docFragment.getElementsByTagName(tagName);
228       if(iCurrent < tag.getLength())
229          return (Element)tag.item(iCurrent);
230       else
231          return null;
232   }
233   else
234       return null;
235 }
236   
237 }
238
Popular Tags