KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > tests > org > enhydra > xml > xhtml > dominfo > Relations


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  *
21  * $Id: Relations.java,v 1.2 2005/01/26 08:29:24 jkjome Exp $
22  */

23
24 package tests.org.enhydra.xml.xhtml.dominfo;
25
26 import java.util.HashMap JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.Set JavaDoc;
30 import java.util.TreeMap JavaDoc;
31
32 /**
33  * Build a table mapping the relationship between the XHTML DOM interfaces,
34  * implementations and the W3C HTML interfaces.
35  */

36 class Relations {
37     /* Indices used hwne addressing the set of DOM classes */
38     public static final int HTML_INTERFACE_IDX = 0;
39     public static final int XHTML_INTERFACE_IDX = 1;
40     public static final int XHTML_IMPLEMENTATION_IDX = 2;
41     public static final int NUM_DOM_CLASS_TYPES = 3;
42
43     /**
44      * A tag relation to DOM classes.
45      */

46     public class TagRelation {
47         /* Tag name */
48         private final String JavaDoc fTagName;
49         
50         /* DTD information */
51         private final DTDInfo.ElementInfo fElementInfo;
52
53         /* Element class information for each DOM. */
54         private DOMInfo.ElementClass[] fElementClassInfo = new DOMInfo.ElementClass[NUM_DOM_CLASS_TYPES];
55
56         /*
57          * Attribute relations for the tag, keyed by lower-case attribute name.
58          * Each value is a linked-list of AttrRelations.
59          */

60         private Map JavaDoc fAttrRelations = new TreeMap JavaDoc();
61
62         /** Constructor */
63         public TagRelation(DTDInfo.ElementInfo elementInfo) {
64             fTagName = elementInfo.getRawName();
65             fElementInfo = elementInfo;
66
67             // Create AttrRelation objects.
68
Iterator JavaDoc attrNames = elementInfo.getAttrNames().iterator();
69             while (attrNames.hasNext()) {
70                 addDTDAttrInfo(elementInfo.getAttr((String JavaDoc)attrNames.next()));
71             }
72         }
73
74         /** Constructor without elementInfo (for special tag mappings) */
75         public TagRelation(String JavaDoc tagName) {
76             fTagName = tagName;
77             fElementInfo = null;
78         }
79         
80         /** Add a new DTDInfo.AttrInfo object. */
81         private void addDTDAttrInfo(DTDInfo.AttrInfo attrInfo) {
82             // NB name is lower-cased by AttrRelation
83
AttrRelation attrRelation = new AttrRelation(this, attrInfo);
84             if (fAttrRelations.containsKey(attrRelation.getAttrName())) {
85                 throw new Error JavaDoc("attribute \"" + attrRelation.getAttrName() + "\" already exists");
86             }
87             fAttrRelations.put(attrRelation.getAttrName(), attrRelation);
88         }
89
90         
91         /** Add the properties to the AttrRelation for the tag */
92         private void addElementProperties(int domIdx,
93                                           DOMInfo.ElementClass elementClassInfo) {
94             Iterator JavaDoc props = elementClassInfo.getPropertyNames().iterator();
95             while (props.hasNext()) {
96                 addElementProperty(domIdx, elementClassInfo.getProperty((String JavaDoc)props.next()));
97             }
98         }
99
100         /** Get the names for the AttributeRelations. */
101         public Set JavaDoc getAttrRelationNames() {
102             return fAttrRelations.keySet();
103         }
104
105         /** An attribute relation (which maybe chained) */
106         public AttrRelation getAttrRelation(String JavaDoc name) {
107             return (AttrRelation)fAttrRelations.get(name);
108         }
109
110         /** Get the tag name */
111         public String JavaDoc getTagName() {
112             return fTagName;
113         }
114
115         /** Get the DTDInfo.ElementInfo object. */
116         public DTDInfo.ElementInfo getElementInfo() {
117             return fElementInfo;
118         }
119
120         /** Get the element clas info for a DOM set */
121         public DOMInfo.ElementClass getElementClassInfo(int domIdx) {
122             return fElementClassInfo[domIdx];
123         }
124
125         /** Set the element class info for a DOM set */
126         public void setElementClassInfo(int domIdx,
127                                         DOMInfo.ElementClass elementClassInfo) {
128             fElementClassInfo[domIdx] = elementClassInfo;
129             //FIXME: System.err.println("setElementClassInfo" + domIdx);
130
addElementProperties(domIdx, elementClassInfo);
131         }
132
133         /** Add a property to the AttrRelation for the tag */
134         private void addElementProperty(int domIdx,
135                                         DOMInfo.ElementProperty prop) {
136             String JavaDoc attrName = prop.getAttrName();
137             if (fSpecialPropertyAttrMap.containsKey(attrName)) {
138                 attrName = (String JavaDoc)fSpecialPropertyAttrMap.get(attrName);
139             }
140
141             AttrRelation attrRelHead = (AttrRelation)fAttrRelations.get(attrName);
142             if (attrRelHead == null) {
143                 // Hmm, no entry for this in DTD!
144
attrRelHead = new AttrRelation(this, prop.getAttrName());
145                 fAttrRelations.put(attrRelHead.getAttrName(), attrRelHead);
146             }
147
148             // Find the first one with an empty entry for this DOM idx.
149
AttrRelation attrRel = attrRelHead;
150             while ((attrRel != null) && (attrRel.getElementPropertyInfo(domIdx) != null)) {
151                 attrRel = attrRel.getNextAttrRelation();
152             }
153             if (attrRel == null) {
154                 // If available, construct with attrInfo
155
DTDInfo.AttrInfo attrInfo = attrRelHead.getAttrInfo();
156                 if (attrInfo != null) {
157                     attrRel = new AttrRelation(this, attrInfo);
158                 } else {
159                     attrRel = new AttrRelation(this, prop.getAttrName());
160                 }
161                 attrRelHead.addAttrRelation(attrRel);
162             }
163             attrRel.setElementPropertyInfo(domIdx, prop);
164         }
165     }
166
167     /**
168      * A relationship between an attribute of a tag and the properties
169      * detected on the DOM classes.
170      */

171     public class AttrRelation {
172         /* Tag associated with */
173         private final TagRelation fTagRelation;
174
175         /* Attribute name (lower-cased) */
176         private final String JavaDoc fAttrName;
177         
178         /* DTD information */
179         private final DTDInfo.AttrInfo fAttrInfo;
180
181         /* Element class information for each DOM. */
182         private DOMInfo.ElementProperty[] fElementPropertyInfo = new DOMInfo.ElementProperty[NUM_DOM_CLASS_TYPES];
183
184         /**
185          * If multiple properties map to the same method, due to case
186          * insensitive compares, different access method mapping to the same
187          * attribute, etc.
188          */

189         private AttrRelation fNext;
190
191         /** Constructor */
192         public AttrRelation(TagRelation tagRelation,
193                             DTDInfo.AttrInfo attrInfo) {
194             fTagRelation = tagRelation;
195             fAttrName = attrInfo.getRawName().toLowerCase();
196             fAttrInfo = attrInfo;
197             //FIXME: System.err.println("AttrRelation: " + tagRelation.getTagName() + ": " + fAttrName);
198
}
199
200         /** Constructor with no DTDInfo.AttrInfo */
201         public AttrRelation(TagRelation tagRelation,
202                             String JavaDoc attrNameLower) {
203             fTagRelation = tagRelation;
204             fAttrName = attrNameLower;
205             fAttrInfo = null;
206             //FIXME: System.err.println("AttrRelation: " + tagRelation.getTagName() + ": " + fAttrName);
207
}
208
209         /* Get tag associated with */
210         public TagRelation getTagRelation() {
211             return fTagRelation;
212         }
213
214         /** Get the tag name */
215         public String JavaDoc getAttrName() {
216             return fAttrName;
217         }
218
219         /** Get the DTDInfo.AttrInfo object. */
220         public DTDInfo.AttrInfo getAttrInfo() {
221             return fAttrInfo;
222         }
223
224         /** Get the attr clas info for a DOM set */
225         public DOMInfo.ElementProperty getElementPropertyInfo(int domIdx) {
226             return fElementPropertyInfo[domIdx];
227         }
228
229         /** Set the element clas info for a DOM set */
230         public void setElementPropertyInfo(int domIdx,
231                                            DOMInfo.ElementProperty elementPropertyInfo) {
232             fElementPropertyInfo[domIdx] = elementPropertyInfo;
233         }
234
235         /**
236          * Add a next AttrRelation. This will traverse to the end of the chain and
237          * add at the tail.
238          */

239         public void addAttrRelation(AttrRelation next) {
240             if (fNext == null) {
241                 fNext = next;
242             } else {
243                 fNext.addAttrRelation(next);
244             }
245         }
246
247         /**
248          * Get next AttrRelation in the chain.
249          */

250         public AttrRelation getNextAttrRelation() {
251             return fNext;
252         }
253     }
254
255
256     /** Parsed DTD */
257     private DTDInfo fDTDInfo;
258
259     /** DOMInfo objects */
260     private DOMInfo[] fDOMInfos = new DOMInfo[NUM_DOM_CLASS_TYPES];
261
262     /**
263      * DOMInfo.ElementClass objects that have not been added to the relation
264      * table.
265      */

266     private Set JavaDoc[] fPendingElementClasses = new Set JavaDoc[NUM_DOM_CLASS_TYPES];
267
268     /** Tag relations tables, indexed by tag name. */
269     private Map JavaDoc fTags = new TreeMap JavaDoc();
270
271     /** Special mappings of property names to attribute names (global for now); mapped to lowercase */
272     private static final String JavaDoc[][] SPECIAL_PROPERTY_ATTR_MAP = {
273         {"ClassName", "class"},
274         {"ChOff", "charoff"},
275         {"Ch", "char"}
276     };
277     private static final HashMap JavaDoc fSpecialPropertyAttrMap = new HashMap JavaDoc();
278
279     static {
280         for (int idx =