001: package org.jdom;
002:
003: import java.util.*;
004:
005: /**
006: * Special factory for building documents without any content or structure
007: * checking. This should only be used when you are 100% positive that the
008: * input is absolutely correct. This factory can speed builds, but any
009: * problems in the input will be uncaught until later when they could cause
010: * infinite loops, malformed XML, or worse. Use with extreme caution.
011: */
012: public class UncheckedJDOMFactory implements JDOMFactory {
013:
014: // =====================================================================
015: // Element Factory
016: // =====================================================================
017:
018: public Element element(String name, Namespace namespace) {
019: Element e = new Element();
020: e.name = name;
021: if (namespace == null) {
022: namespace = Namespace.NO_NAMESPACE;
023: }
024: e.namespace = namespace;
025: return e;
026: }
027:
028: public Element element(String name) {
029: Element e = new Element();
030: e.name = name;
031: e.namespace = Namespace.NO_NAMESPACE;
032: return e;
033: }
034:
035: public Element element(String name, String uri) {
036: return element(name, Namespace.getNamespace("", uri));
037: }
038:
039: public Element element(String name, String prefix, String uri) {
040: return element(name, Namespace.getNamespace(prefix, uri));
041: }
042:
043: // =====================================================================
044: // Attribute Factory
045: // =====================================================================
046:
047: public Attribute attribute(String name, String value,
048: Namespace namespace) {
049: Attribute a = new Attribute();
050: a.name = name;
051: a.value = value;
052: if (namespace == null) {
053: namespace = Namespace.NO_NAMESPACE;
054: }
055: a.namespace = namespace;
056: return a;
057: }
058:
059: public Attribute attribute(String name, String value, int type,
060: Namespace namespace) {
061: Attribute a = new Attribute();
062: a.name = name;
063: a.type = type;
064: a.value = value;
065: if (namespace == null) {
066: namespace = Namespace.NO_NAMESPACE;
067: }
068: a.namespace = namespace;
069: return a;
070: }
071:
072: public Attribute attribute(String name, String value) {
073: Attribute a = new Attribute();
074: a.name = name;
075: a.value = value;
076: a.namespace = Namespace.NO_NAMESPACE;
077: return a;
078: }
079:
080: public Attribute attribute(String name, String value, int type) {
081: Attribute a = new Attribute();
082: a.name = name;
083: a.type = type;
084: a.value = value;
085: a.namespace = Namespace.NO_NAMESPACE;
086: return a;
087: }
088:
089: // =====================================================================
090: // Text Factory
091: // =====================================================================
092:
093: public Text text(String str) {
094: Text t = new Text();
095: t.value = str;
096: return t;
097: }
098:
099: // =====================================================================
100: // CDATA Factory
101: // =====================================================================
102:
103: public CDATA cdata(String str) {
104: CDATA c = new CDATA();
105: c.value = str;
106: return c;
107: }
108:
109: // =====================================================================
110: // Comment Factory
111: // =====================================================================
112:
113: public Comment comment(String str) {
114: Comment c = new Comment();
115: c.text = str;
116: return c;
117: }
118:
119: // =====================================================================
120: // Processing Instruction Factory
121: // =====================================================================
122:
123: public ProcessingInstruction processingInstruction(String target,
124: Map data) {
125: ProcessingInstruction p = new ProcessingInstruction();
126: p.target = target;
127: p.setData(data);
128: return p;
129: }
130:
131: public ProcessingInstruction processingInstruction(String target,
132: String data) {
133: ProcessingInstruction p = new ProcessingInstruction();
134: p.target = target;
135: p.setData(data);
136: return p;
137: }
138:
139: // =====================================================================
140: // Entity Ref Factory
141: // =====================================================================
142:
143: public EntityRef entityRef(String name) {
144: EntityRef e = new org.jdom.EntityRef();
145: e.name = name;
146: return e;
147: }
148:
149: public EntityRef entityRef(String name, String systemID) {
150: EntityRef e = new EntityRef();
151: e.name = name;
152: e.systemID = systemID;
153: return e;
154: }
155:
156: public EntityRef entityRef(String name, String publicID,
157: String systemID) {
158: EntityRef e = new EntityRef();
159: e.name = name;
160: e.publicID = publicID;
161: e.systemID = systemID;
162: return e;
163: }
164:
165: // =====================================================================
166: // DocType Factory
167: // =====================================================================
168:
169: public DocType docType(String elementName, String publicID,
170: String systemID) {
171: DocType d = new DocType();
172: d.elementName = elementName;
173: d.publicID = publicID;
174: d.systemID = systemID;
175: return d;
176: }
177:
178: public DocType docType(String elementName, String systemID) {
179: return docType(elementName, null, systemID);
180: }
181:
182: public DocType docType(String elementName) {
183: return docType(elementName, null, null);
184: }
185:
186: // =====================================================================
187: // Document Factory
188: // =====================================================================
189:
190: public Document document(Element rootElement, DocType docType,
191: String baseURI) {
192: Document d = new Document();
193: if (docType != null) {
194: addContent(d, docType);
195: }
196: if (rootElement != null) {
197: addContent(d, rootElement);
198: }
199: if (baseURI != null) {
200: d.baseURI = baseURI;
201: }
202: return d;
203: }
204:
205: public Document document(Element rootElement, DocType docType) {
206: return document(rootElement, docType, null);
207: }
208:
209: public Document document(Element rootElement) {
210: return document(rootElement, null, null);
211: }
212:
213: // =====================================================================
214: // List manipulation
215: // =====================================================================
216:
217: public void addContent(Parent parent, Content child) {
218: if (parent instanceof Element) {
219: Element elt = (Element) parent;
220: elt.content.uncheckedAddContent(child);
221: } else {
222: Document doc = (Document) parent;
223: doc.content.uncheckedAddContent(child);
224: }
225: }
226:
227: public void setAttribute(Element parent, Attribute a) {
228: parent.attributes.uncheckedAddAttribute(a);
229: }
230:
231: public void addNamespaceDeclaration(Element parent,
232: Namespace additional) {
233: if (parent.additionalNamespaces == null) {
234: parent.additionalNamespaces = new ArrayList(5); //Element.INITIAL_ARRAY_SIZE
235: }
236: parent.additionalNamespaces.add(additional);
237: }
238: }
|