001: package de.webman.template.jsp;
002:
003: import org.w3c.dom.*;
004: import org.w3c.dom.traversal.*;
005:
006: /**
007: * Diese Klasse stellt Methoden zum Umgang mit Java Server Pages
008: * in Webman bereit.
009: * Ein Objekt WSP representiert einen dom.w3c.node
010: * @author $Author: alex $
011: * @version $Revision: 1.3 $
012: */
013: public class WSP {
014:
015: protected Node root;
016:
017: /** Erstellt ein WSP-Objekt aus einem gegebenen NODE */
018: public WSP(Node root) {
019: this .root = root;
020: }
021:
022: /** Erstellt ein WSP-Objekt aus obj */
023: public WSP(WSP obj) {
024: this .root = obj.root;
025: }
026:
027: /** gibt den representierten Node zurück
028: */
029: public Node getNode() {
030: return (root);
031: }
032:
033: /** erwartet Attributnamen und gibt dessen Value oder null zurück
034:
035: * Attribute sind zum Bsp. NAME, REF (bei einer Webman-REF) oder
036: * die bekannten Content-Attribute
037: @param Name des Attributes
038: @return Wert des Attributes
039: */
040: public String getAttribute(String attrname) {
041: try {
042: return (root.getAttributes().getNamedItem(attrname)
043: .getNodeValue());
044: } catch (NullPointerException e) {
045: return (null);
046: }
047: }
048:
049: /** erwartet Attributnamen und gibt dessen Value oder value zurück
050:
051: * Attribute sind zum Bsp. NAME, REF (bei einer Webman-REF) oder
052: * die bekannten Content-Attribute
053: @param Name des Attributes
054: @param value was er zurueckgeben soll, wenn es das Attribut nicht gibt
055: @return Wert des Attributes
056: */
057: public String getAttribute(String attrname, String value) {
058: try {
059: String v = root.getAttributes().getNamedItem(attrname)
060: .getNodeValue();
061: if (v == null)
062: return value;
063: return v;
064: } catch (NullPointerException e) {
065: return (value);
066: }
067: }
068:
069: /** gibt den Pfad zum Upload aus
070:
071: * (das aktuelle WSP-Objekt muss somit ein Webman-Upload sein)
072: @return Pfad zum Upload
073: */
074: public String getUploadPath() {
075: if (root.hasChildNodes())
076: return (root.getChildNodes().item(0).getNodeValue());
077: return (null);
078: }
079:
080: /** gibt den Namen des Unterknotens Item oder null aus
081:
082: sinnvoll für die meisten Formularelemente
083: der Wert eines Webman-textfeldes liegt z.b. im Unterknoten ITEM
084:
085: @return Wert von ITEM
086: */
087: public String getItem() {
088: if (root.hasChildNodes()) {
089: if (root.getChildNodes().item(0).hasChildNodes())
090: return (root.getChildNodes().item(0).getChildNodes()
091: .item(0).getNodeValue());
092: }
093: return (null);
094: }
095:
096: /** gibt false def. den Wert des x-ten Unterknotens Item aus (x = number) sonst null
097:
098: * sinnvoll für einfache Listen z.b. Auswahlliste
099: @param Position von ITEM
100: @return Wert von ITEM
101: */
102: public String getItem(int number) {
103: if (root.hasChildNodes()) {
104: if (root.getChildNodes().getLength() > number) {
105: if (root.getChildNodes().item(number).hasChildNodes())
106: return (root.getChildNodes().item(number)
107: .getChildNodes().item(0).getNodeValue());
108: }
109: }
110: return (null);
111: }
112:
113: /** gibt den Kinderknoten number oder null zurück
114: @param Position des Kindes
115: @return das Kind
116: */
117: public WSP getChild(int number) {
118: if (root.getChildNodes().getLength() > number) {
119: WSP help = new WSP(root.getChildNodes().item(number));
120: return (help);
121: }
122: return (null);
123:
124: }
125:
126: /** gibt den ersten Knoten des aktuellen Objektes zurück dessen
127: * Attribut 'attrname' den Wert 'attrvalue' hat oder null zurück
128: @param Attributname und Value
129: @return der erste Knoten mit Attributname=Attributvalue
130: */
131: public WSP getNodeByAttr(String attrname, String attrvalue) {
132:
133: // aktueller Knoten ,
134: if (this == null)
135: return (this );
136:
137: if (this .getAttribute(attrname) != null) {
138: if (this .getAttribute(attrname).equals(attrvalue))
139: return (this );
140: }
141:
142: // oder seine Kinder ?
143: for (int i = 0; this .getChild(i) != null; i++) {
144: WSP help = this .getChild(i).getNodeByAttr(attrname,
145: attrvalue);
146: if (help != null)
147: return (help);
148: }
149:
150: return (null);
151: }
152:
153: /** gibt den ersten Knoten mit dem Namen name oder null zurück
154: @param Name des Knotens
155: @return den ersten Knoten mit Name=name
156: */
157: public WSP getNodeByName(String name) {
158: return (this .getNodeByAttr("NAME", name));
159: }
160:
161: // zum auslesen eines Pfades
162: private static String getNextNode(String path) {
163: if (path == null)
164: return (null);
165:
166: int index = path.indexOf('.');
167:
168: if (index == -1)
169: return (path);
170: return (path.substring(0, index));
171: }
172:
173: // zur Bestimmung des verbleibenden Pfades
174: private static String getNextPath(String path) {
175: String Node = getNextNode(path);
176:
177: if (Node == null)
178: return (null);
179:
180: if (Node.length() + 1 <= path.length())
181: return (path.substring(Node.length() + 1, path.length()));
182:
183: return (null);
184: }
185:
186: /** gibt den Knoten zurück der im Pfad path liegt
187: der Pfad besteht aus den werten der Name-Attribute getrennt durch einen Punkt
188: z.b. : NAME1.NAME2
189: @param genauer Pfad der Name-Attribute
190: @return den Knoten
191: */
192: public WSP getNodeByPath(String path) {
193: String node = null;
194: WSP back = new WSP(this );
195: int i;
196:
197: while ((back != null) && (path != null)) {
198: node = getNextNode(path);
199: path = getNextPath(path);
200:
201: if (node == null)
202: break;
203:
204: for (i = 0; (back.getChild(i) != null); i++) {
205: if (back.getChild(i).getAttribute("NAME") != null) {
206: if (back.getChild(i).getAttribute("NAME").equals(
207: node))
208: break;
209: }
210: }
211: back = back.getChild(i);
212: }
213: return (back);
214: }
215:
216: /** wie getNodeByPath, nur das hier nach den Namen der representierten
217: Nodes durchgegangen wird
218:
219: @param genauer Pfad der Nodenamen
220: @return erster Knoten mit diesem Pfad
221: */
222: public WSP getNodeByNodePath(String path) {
223: return (getNodeByNodePath(path, 0));
224: }
225:
226: /** wie getNodeByNodePath,gibt allerdings x-ten Knoten mit diesem Pfad aus
227:
228: @param genauer Pfad des Nodenamen und die Position
229: @return den x-ten Knoten des Pfades
230: */
231: public WSP getNodeByNodePath(String path, int index) {
232: String node = null;
233: WSP back = new WSP(this );
234: int i, treffer;
235: treffer = 0;
236: while ((back != null) && (path != null)) {
237: node = getNextNode(path);
238: path = getNextPath(path);
239:
240: if (node == null)
241: break;
242:
243: for (i = 0; (back.getChild(i) != null); i++) {
244:
245: if (back.getChild(i).getNode().getNodeName() != null) {
246: if (back.getChild(i).getNode().getNodeName()
247: .equals(node)) {
248:
249: if (path == null) {
250: if (treffer == index)
251: break;
252: treffer++;
253: } else
254: break;
255: }
256: }
257: }
258: back = back.getChild(i);
259: }
260: return (back);
261: }
262:
263: /** gibt das x-te Element einer FEW bel. Elemente in folge mit dem
264: * Namen name aus
265: @param Name der FEW und die Stelle an der sie zu finden ist
266: @return die x-te FEW mit Namen Name (x=Element) */
267: public WSP getElement_FEW_BEL_EL_IN_F(String name, int Element) {
268:
269: if (root != null) {
270: int z = 0;
271:
272: for (int i = 0; this .getNodeByNodePath("FEW-BEL_ELE_IN_F",
273: i) != null; i++) {
274:
275: WSP help = this
276: .getNodeByNodePath("FEW-BEL_ELE_IN_F", i);
277:
278: if (help.getAttribute("NAME") != null) {
279: if (help.getAttribute("NAME").equals(name)) {
280: if (z == Element)
281: return (help);
282: z++;
283: }
284: }
285: }
286: }
287:
288: return (null);
289: }
290:
291: /** gibt die Länge des FEW bel. Elemente in Folge mit dem namen name aus
292: @param Name der FEW
293: @return Länge der FEW
294: */
295: public String getLength_FEW_BEL_EL_IN_F(String name) {
296:
297: WSP help = this .getElement_FEW_BEL_EL_IN_F(name, 0);
298:
299: if (help != null)
300: return (help.getAttribute("LENGTH"));
301:
302: return (null);
303: }
304:
305: /** gibt die Zelle (row,col) der Tabelle des pfades zurück
306: @param Pfad der Tabelle; Zeile,Spalte der Zelle
307: @return Zelle
308: */
309: public WSP getTableCell(String pfad, int row, int col) {
310: WSP table = this .getNodeByPath(pfad);
311: if (table == null)
312: return (null);
313: WSP row1 = table.getChild(row);
314: if (row1 == null)
315: return (null);
316: return (row1.getChild(col));
317: }
318:
319: }
|