Source code: iiuf/xmillum/ElementTagger.java
1 /* (C) 2001-2002, DIUF, http://www.unifr.ch/diuf
2 *
3 * This program is free software; you can redistribute it and/or modify it
4 * under the terms of the GNU General Public License as published by the
5 * Free Software Foundation; either version 2 of the License, or (at your
6 * option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
11 * Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 */
17
18 package iiuf.xmillum;
19
20 import iiuf.dom.DOMUtils;
21
22 import java.util.ArrayList;
23
24 import org.w3c.dom.Element;
25 import org.w3c.dom.NodeList;
26
27 /**
28 * ElementTagger
29 *
30 * This object associates a tag to every element of a given DOM structure
31 * and allows direct access to these elements by their tag. In addition, the
32 * tag is put as an attribute of the "tmp" namespace into the element in
33 * question.
34 *
35 * @author $Author: ohitz $
36 * @version $Revision: 1.1 $
37 */
38 public class ElementTagger {
39 protected String tagName;
40 protected int tagValue;
41 protected ArrayList tagged;
42
43 public ElementTagger(Element element, String tag) {
44 tagName = tag;
45 tagValue = 0;
46 tagged = new ArrayList(2048);
47 tagElements(element);
48 }
49
50 public Element getReferencedElement(Element e) {
51 String tag = e.getAttribute("ref");
52 if (tag != null) {
53 return getElementWithTag(tag);
54 }
55 return null;
56 }
57
58 public Element getElementWithTag(String tag) {
59 return getElementWithTag(Integer.parseInt(tag));
60 }
61
62 public Element getElementWithTag(int tag) {
63 return (Element) tagged.get(tag);
64 }
65
66 protected void tagElements(Element element) {
67 element.setAttributeNS("tmp", tagName, Integer.toString(tagged.size()));
68 tagged.add(element);
69
70 NodeList nl = DOMUtils.getChildElements(element);
71 for (int i = 0; i < nl.getLength(); i++) {
72 tagElements((Element) nl.item(i));
73 }
74 }
75 }