KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdom > contrib > ids > IdElement


1 /*--
2
3  $Id: IdElement.java,v 1.6 2004/12/11 00:01:54 jhunter Exp $
4
5  Copyright (C) 2001-2004 Jason Hunter & Brett McLaughlin.
6  All rights reserved.
7
8  Redistribution and use in source and binary forms, with or without
9  modification, are permitted provided that the following conditions
10  are met:
11
12  1. Redistributions of source code must retain the above copyright
13     notice, this list of conditions, and the following disclaimer.
14
15  2. Redistributions in binary form must reproduce the above copyright
16     notice, this list of conditions, and the disclaimer that follows
17     these conditions in the documentation and/or other materials
18     provided with the distribution.
19
20  3. The name "JDOM" must not be used to endorse or promote products
21     derived from this software without prior written permission. For
22     written permission, please contact <request_AT_jdom_DOT_org>.
23
24  4. Products derived from this software may not be called "JDOM", nor
25     may "JDOM" appear in their name, without prior written permission
26     from the JDOM Project Management <request_AT_jdom_DOT_org>.
27
28  In addition, we request (but do not require) that you include in the
29  end-user documentation provided with the redistribution and/or in the
30  software itself an acknowledgement equivalent to the following:
31      "This product includes software developed by the
32       JDOM Project (http://www.jdom.org/)."
33  Alternatively, the acknowledgment may be graphical using the logos
34  available at http://www.jdom.org/images/logos.
35
36  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  DISCLAIMED. IN NO EVENT SHALL THE JDOM AUTHORS OR THE PROJECT
40  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  SUCH DAMAGE.
48
49  This software consists of voluntary contributions made by many
50  individuals on behalf of the JDOM Project and was originally
51  created by Jason Hunter <jhunter_AT_jdom_DOT_org> and
52  Brett McLaughlin <brett_AT_jdom_DOT_org>. For more information
53  on the JDOM Project, please see <http://www.jdom.org/>.
54
55  */

56
57 package org.jdom.contrib.ids;
58
59 import java.util.*;
60
61 import org.jdom.Attribute;
62 import org.jdom.Content;
63 import org.jdom.Document;
64 import org.jdom.Element;
65 import org.jdom.Parent;
66 import org.jdom.Namespace;
67
68 /**
69  * A sub-class of the default JDOM <code>Element</code> to help
70  * keeping up-to-date the element lookup table maintained by
71  * <code>IdDocument</code>.
72  *
73  * @author Laurent Bihanic
74  */

75 public class IdElement extends Element {
76
77    // Allow Javadocs to inherit from superclass
78

79    public IdElement(String JavaDoc name, Namespace namespace) {
80       super(name, namespace);
81    }
82
83    public IdElement(String JavaDoc name) {
84       this(name, (Namespace)null);
85    }
86
87    public IdElement(String JavaDoc name, String JavaDoc uri) {
88       this(name, Namespace.getNamespace("", uri));
89    }
90
91    public IdElement(String JavaDoc name, String JavaDoc prefix, String JavaDoc uri) {
92       this(name, Namespace.getNamespace(prefix, uri));
93    }
94
95    protected Content setParent(Parent parent) {
96       // Save previous owning document (if any).
97
Document prevDoc = this.getDocument();
98       Document newDoc = (parent != null)? parent.getDocument(): null;
99
100       // Attach to new parent element.
101
super.setParent(parent);
102
103       if (newDoc != prevDoc) {
104          // New and previous owning documents are different.
105
// => Remove all the IDs for the subtree this element is the root
106
// of from the previous owning document's lookup table and
107
// insert them into the new owning document's lookup table.
108
transferIds(prevDoc, newDoc);
109       }
110       return this;
111    }
112
113
114    /**
115     * <p>
116     * Transfers all the IDs for the subtree this element is the root
117     * of from the lookup table of the previous owning document to
118     * the lookup table of the new owning document.</p>
119     * <p>
120     * If either of the documents is <code>null</code> or is not an
121     * {@link IdDocument}, this method performs not action on the
122     * document. Hence, this method supports being called with both
123     * documents equal to <code>null</code>.</p>
124     *
125     * @param prevDoc the previous owning document.
126     * @param newDoc the new owning document.
127     */

128    private void transferIds(Document prevDoc, Document newDoc)
129    {
130       if ((prevDoc instanceof IdDocument) || (newDoc instanceof IdDocument)) {
131          // At least one of the documents supports lookup by ID.
132
// => Walk subtree to collect ID mappings.
133
Map ids = getIds(this, new HashMap());
134
135          // Update previous owning document.
136
if (prevDoc instanceof IdDocument) {
137             // Document supports lookup by ID.
138
// => Remove IDs from document's lookup table.
139
IdDocument idDoc = (IdDocument)prevDoc;
140
141             for (Iterator i=ids.keySet().iterator(); i.hasNext(); ) {
142                idDoc.removeId(i.next().toString());
143             }
144          }
145          // Else: Lookup by ID not supported. => Nothing to update!
146

147          // Update new owning document.
148
if (newDoc instanceof IdDocument) {
149             // Document supports lookup by ID.
150
// => Add IDs from document's lookup table.
151
IdDocument idDoc = (IdDocument)newDoc;
152
153             for (Iterator i=ids.keySet().iterator(); i.hasNext(); ) {
154                String JavaDoc key = i.next().toString();
155                idDoc.addId(key, (Element)(ids.get(key)));
156             }
157          }
158          // Else: Lookup by ID not supported. => Nothing to update!
159
}
160       // Else: None of the documents supports lookup by ID not supported.
161
// => Ignore call.
162
return;
163    }
164
165    /**
166     * <p>
167     * Retrieves the ID attributes for the subtree rooted at
168     * <code>root</code> to populate the ID mapping table
169     * <code>ids</code>. In the mapping table, ID attribute values are
170     * the keys to access the elements.</p>
171     *
172     * @param root the root element of the subtree to walk.
173     * @param ids the ID lookup table to populate.
174     *
175     * @return the updated ID lookup table.
176     */

177    private static Map getIds(Element root, Map ids) {
178       addIdAttributes(root, ids);
179
180       List children = root.getContent();
181       for (Iterator i=children.iterator(); i.hasNext(); ) {
182          Object JavaDoc o = i.next();
183          if (o instanceof Element) {
184             getIds((Element)o, ids);
185          }
186       }
187       return ids;
188    }
189
190    /**
191     * <p>
192     * Gets the ID attribute for <code>elt</code> and adds the
193     * correpsonding entries in the ID mapping table
194     * <code>ids</code>.</p>
195     *
196     * @param elt the element to the ID attributes from.
197     * @param ids the ID lookup table to populate.
198     */

199    private static void addIdAttributes(Element elt, Map ids) {
200       List attrs = elt.getAttributes();
201       for (Iterator i=attrs.iterator(); i.hasNext(); ) {
202          Attribute attr = (Attribute)(i.next());
203
204          if (attr.getAttributeType() == Attribute.ID_TYPE) {
205             ids.put(attr.getValue(), elt);
206             break;
207          }
208       }
209       return;
210    }
211 }
212
213
Popular Tags