KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdom > UncheckedJDOMFactory


1 package org.jdom;
2
3 import java.util.*;
4
5 /**
6  * Special factory for building documents without any content or structure
7  * checking. This should only be used when you are 100% positive that the
8  * input is absolutely correct. This factory can speed builds, but any
9  * problems in the input will be uncaught until later when they could cause
10  * infinite loops, malformed XML, or worse. Use with extreme caution.
11  */

12 public class UncheckedJDOMFactory implements JDOMFactory {
13
14     // =====================================================================
15
// Element Factory
16
// =====================================================================
17

18     public Element element(String JavaDoc name, Namespace namespace) {
19         Element e = new Element();
20         e.name = name;
21         if (namespace == null) {
22             namespace = Namespace.NO_NAMESPACE;
23         }
24         e.namespace = namespace;
25         return e;
26     }
27
28     public Element element(String JavaDoc name) {
29         Element e = new Element();
30         e.name = name;
31         e.namespace = Namespace.NO_NAMESPACE;
32         return e;
33     }
34
35     public Element element(String JavaDoc name, String JavaDoc uri) {
36         return element(name, Namespace.getNamespace("", uri));
37     }
38
39     public Element element(String JavaDoc name, String JavaDoc prefix, String JavaDoc uri) {
40         return element(name, Namespace.getNamespace(prefix, uri));
41     }
42
43     // =====================================================================
44
// Attribute Factory
45
// =====================================================================
46

47     public Attribute attribute(String JavaDoc name, String JavaDoc value, Namespace namespace) {
48         Attribute a = new Attribute();
49         a.name = name;
50         a.value = value;
51         if (namespace == null) {
52             namespace = Namespace.NO_NAMESPACE;
53         }
54         a.namespace = namespace;
55         return a;
56     }
57
58     public Attribute attribute(String JavaDoc name, String JavaDoc value, int type, Namespace namespace) {
59         Attribute a = new Attribute();
60         a.name = name;
61         a.type = type;
62         a.value = value;
63         if (namespace == null) {
64             namespace = Namespace.NO_NAMESPACE;
65         }
66         a.namespace = namespace;
67         return a;
68     }
69
70     public Attribute attribute(String JavaDoc name, String JavaDoc value) {
71         Attribute a = new Attribute();
72         a.name = name;
73         a.value = value;
74         a.namespace = Namespace.NO_NAMESPACE;
75         return a;
76     }
77
78     public Attribute attribute(String JavaDoc name, String JavaDoc value, int type) {
79         Attribute a = new Attribute();
80         a.name = name;
81         a.type = type;
82         a.value = value;
83         a.namespace = Namespace.NO_NAMESPACE;
84         return a;
85     }
86
87     // =====================================================================
88
// Text Factory
89
// =====================================================================
90

91     public Text text(String JavaDoc str) {
92         Text t = new Text();
93         t.value = str;
94         return t;
95     }
96
97     // =====================================================================
98
// CDATA Factory
99
// =====================================================================
100

101     public CDATA cdata(String JavaDoc str) {
102         CDATA c = new CDATA();
103         c.value = str;
104         return c;
105     }
106
107     // =====================================================================
108
// Comment Factory
109
// =====================================================================
110

111     public Comment comment(String JavaDoc str) {
112         Comment c = new Comment();
113         c.text = str;
114         return c;
115     }
116
117     // =====================================================================
118
// Processing Instruction Factory
119
// =====================================================================
120

121     public ProcessingInstruction processingInstruction(String JavaDoc target, Map data) {
122         ProcessingInstruction p = new ProcessingInstruction();
123         p.target = target;
124         p.setData(data);
125         return p;
126     }
127
128     public ProcessingInstruction processingInstruction(String JavaDoc target, String JavaDoc data) {
129         ProcessingInstruction p = new ProcessingInstruction();
130         p.target = target;
131         p.setData(data);
132         return p;
133     }
134
135     // =====================================================================
136
// Entity Ref Factory
137
// =====================================================================
138

139     public EntityRef entityRef(String JavaDoc name) {
140         EntityRef e = new org.jdom.EntityRef();
141         e.name = name;
142         return e;
143     }
144
145     public EntityRef entityRef(String JavaDoc name, String JavaDoc systemID) {
146         EntityRef e = new EntityRef();
147         e.name = name;
148         e.systemID = systemID;
149         return e;
150     }
151
152     public EntityRef entityRef(String JavaDoc name, String JavaDoc publicID, String JavaDoc systemID) {
153         EntityRef e = new EntityRef();
154         e.name = name;
155         e.publicID = publicID;
156         e.systemID = systemID;
157         return e;
158     }
159
160     // =====================================================================
161
// DocType Factory
162
// =====================================================================
163

164     public DocType docType(String JavaDoc elementName, String JavaDoc publicID, String JavaDoc systemID) {
165         DocType d = new DocType();
166         d.elementName = elementName;
167         d.publicID = publicID;
168         d.systemID = systemID;
169         return d;
170     }
171
172     public DocType docType(String JavaDoc elementName, String JavaDoc systemID) {
173         return docType(elementName, null, systemID);
174     }
175
176     public DocType docType(String JavaDoc elementName) {
177         return docType(elementName, null, null);
178     }
179
180     // =====================================================================
181
// Document Factory
182
// =====================================================================
183

184     public Document document(Element rootElement, DocType docType, String JavaDoc baseURI) {
185         Document d = new Document();
186         if (docType != null) {
187             addContent(d, docType);
188         }
189         if (rootElement != null) {
190             addContent(d, rootElement);
191         }
192         if (baseURI != null) {
193             d.baseURI = baseURI;
194         }
195         return d;
196     }
197
198     public Document document(Element rootElement, DocType docType) {
199         return document(rootElement, docType, null);
200     }
201
202     public Document document(Element rootElement) {
203         return document(rootElement, null, null);
204     }
205
206     // =====================================================================
207
// List manipulation
208
// =====================================================================
209

210     public void addContent(Parent parent, Content child) {
211         if (parent instanceof Element) {
212             Element elt = (Element) parent;
213             elt.content.uncheckedAddContent(child);
214         }
215         else {
216             Document doc = (Document) parent;
217             doc.content.uncheckedAddContent(child);
218         }
219     }
220
221     public void setAttribute(Element parent, Attribute a) {
222         parent.attributes.uncheckedAddAttribute(a);
223     }
224
225     public void addNamespaceDeclaration(Element parent, Namespace additional) {
226         if (parent.additionalNamespaces == null) {
227             parent.additionalNamespaces = new ArrayList(5); //Element.INITIAL_ARRAY_SIZE
228
}
229         parent.additionalNamespaces.add(additional);
230     }
231 }
232
Popular Tags