001: /*
002: * Copyright 2005-2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.xml.internal.stream.writers;
027:
028: import java.lang.reflect.InvocationTargetException;
029: import java.lang.reflect.Method;
030: import javax.xml.XMLConstants;
031: import javax.xml.namespace.NamespaceContext;
032: import javax.xml.stream.XMLStreamException;
033: import javax.xml.stream.XMLStreamWriter;
034: import javax.xml.transform.dom.DOMResult;
035: import org.w3c.dom.Attr;
036: import org.w3c.dom.CDATASection;
037: import org.w3c.dom.Comment;
038: import org.w3c.dom.Document;
039: import org.w3c.dom.Element;
040: import org.w3c.dom.EntityReference;
041: import org.w3c.dom.Node;
042: import org.w3c.dom.ProcessingInstruction;
043: import org.w3c.dom.Text;
044: import org.xml.sax.helpers.NamespaceSupport;
045:
046: /**
047: * This class provides support to build a DOM tree using XMLStreamWriter API's.
048: * @author K.Venugopal@sun.com
049: */
050:
051: /*
052: * TODO : -Venu
053: * Internal NamespaceManagement
054: * setPrefix
055: * support for isRepairNamespace property.
056: * Some Unsupported Methods.
057: * Change StringBuffer to StringBuilder, when JDK 1.5 will be minimum requirement for SJSXP.
058: */
059:
060: public class XMLDOMWriterImpl implements XMLStreamWriter {
061:
062: private Document ownerDoc = null;
063: private Node currentNode = null;
064: private Node node = null;
065: private NamespaceSupport namespaceContext = null;
066: private Method mXmlVersion = null;
067: private boolean[] needContextPop = null;
068: private StringBuffer stringBuffer = null;
069: private int resizeValue = 20;
070: private int depth = 0;
071:
072: /**
073: * Creates a new instance of XMLDOMwriterImpl
074: * @param result DOMResult object @javax.xml.transform.dom.DOMResult
075: */
076: public XMLDOMWriterImpl(DOMResult result) {
077:
078: node = result.getNode();
079: if (node.getNodeType() == Node.DOCUMENT_NODE) {
080: ownerDoc = (Document) node;
081: currentNode = ownerDoc;
082: } else {
083: ownerDoc = node.getOwnerDocument();
084: currentNode = node;
085: }
086: getDLThreeMethods();
087: stringBuffer = new StringBuffer();
088: needContextPop = new boolean[resizeValue];
089: namespaceContext = new NamespaceSupport();
090: }
091:
092: private void getDLThreeMethods() {
093: try {
094: mXmlVersion = ownerDoc.getClass().getMethod(
095: "setXmlVersion", new Class[] { String.class });
096: } catch (NoSuchMethodException mex) {
097: //log these errors at fine level.
098: mXmlVersion = null;
099: } catch (SecurityException se) {
100: //log these errors at fine level.
101: mXmlVersion = null;
102: }
103: }
104:
105: /**
106: * This method has no effect when called.
107: * @throws javax.xml.stream.XMLStreamException {@inheritDoc}
108: */
109: public void close() throws XMLStreamException {
110: //no-op
111: }
112:
113: /**
114: * This method has no effect when called.
115: * @throws javax.xml.stream.XMLStreamException {@inheritDoc}
116: */
117: public void flush() throws XMLStreamException {
118: //no-op
119: }
120:
121: /**
122: * {@inheritDoc}
123: * @return {@inheritDoc}
124: */
125: public javax.xml.namespace.NamespaceContext getNamespaceContext() {
126: return null;
127: }
128:
129: /**
130: * {@inheritDoc}
131: * @param namespaceURI {@inheritDoc}
132: * @throws javax.xml.stream.XMLStreamException {@inheritDoc}
133: * @return {@inheritDoc}
134: */
135: public String getPrefix(String namespaceURI)
136: throws XMLStreamException {
137: String prefix = null;
138: if (this .namespaceContext != null) {
139: prefix = namespaceContext.getPrefix(namespaceURI);
140: }
141: return prefix;
142: }
143:
144: /**
145: * Is not supported in this implementation.
146: * @param str {@inheritDoc}
147: * @throws java.lang.IllegalArgumentException {@inheritDoc}
148: * @return {@inheritDoc}
149: */
150: public Object getProperty(String str)
151: throws IllegalArgumentException {
152: throw new UnsupportedOperationException();
153: }
154:
155: /**
156: * Is not supported in this version of the implementation.
157: * @param uri {@inheritDoc}
158: * @throws javax.xml.stream.XMLStreamException {@inheritDoc}
159: */
160: public void setDefaultNamespace(String uri)
161: throws XMLStreamException {
162: namespaceContext.declarePrefix(XMLConstants.DEFAULT_NS_PREFIX,
163: uri);
164: if (!needContextPop[depth]) {
165: needContextPop[depth] = true;
166: }
167: }
168:
169: /**
170: * {@inheritDoc}
171: * @param namespaceContext {@inheritDoc}
172: * @throws javax.xml.stream.XMLStreamException {@inheritDoc}
173: */
174: public void setNamespaceContext(
175: javax.xml.namespace.NamespaceContext namespaceContext)
176: throws XMLStreamException {
177: throw new UnsupportedOperationException();
178: }
179:
180: /**
181: * Is not supported in this version of the implementation.
182: * @param prefix {@inheritDoc}
183: * @param uri {@inheritDoc}
184: * @throws javax.xml.stream.XMLStreamException {@inheritDoc}
185: */
186: public void setPrefix(String prefix, String uri)
187: throws XMLStreamException {
188: if (prefix == null) {
189: throw new XMLStreamException("Prefix cannot be null");
190: }
191: namespaceContext.declarePrefix(prefix, uri);
192: if (!needContextPop[depth]) {
193: needContextPop[depth] = true;
194: }
195: }
196:
197: /**
198: * Creates a DOM Atrribute @see org.w3c.dom.Node and associates it with the current DOM element @see org.w3c.dom.Node.
199: * @param localName {@inheritDoc}
200: * @param value {@inheritDoc}
201: * @throws javax.xml.stream.XMLStreamException {@inheritDoc}
202: */
203: public void writeAttribute(String localName, String value)
204: throws XMLStreamException {
205:
206: if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
207: Attr attr = ownerDoc.createAttribute(localName);
208: attr.setValue(value);
209: ((Element) currentNode).setAttributeNode(attr);
210: } else {
211: //Convert node type to String
212: throw new IllegalStateException(
213: "Current DOM Node type is "
214: + currentNode.getNodeType()
215: + "and does not allow attributes to be set ");
216: }
217: }
218:
219: /**
220: * Creates a DOM Atrribute @see org.w3c.dom.Node and associates it with the current DOM element @see org.w3c.dom.Node.
221: * @param namespaceURI {@inheritDoc}
222: * @param localName {@inheritDoc}
223: * @param value {@inheritDoc}
224: * @throws javax.xml.stream.XMLStreamException {@inheritDoc}
225: */
226: public void writeAttribute(String namespaceURI, String localName,
227: String value) throws XMLStreamException {
228: if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
229: String prefix = null;
230: if (namespaceURI == null) {
231: throw new XMLStreamException(
232: "NamespaceURI cannot be null");
233: }
234: if (localName == null) {
235: throw new XMLStreamException(
236: "Local name cannot be null");
237: }
238: if (namespaceContext != null) {
239: prefix = namespaceContext.getPrefix(namespaceURI);
240: }
241:
242: if (prefix == null) {
243: throw new XMLStreamException("Namespace URI "
244: + namespaceURI + "is not bound to any prefix");
245: }
246:
247: String qualifiedName = null;
248: if (prefix.equals("")) {
249: qualifiedName = localName;
250: } else {
251: qualifiedName = getQName(prefix, localName);
252: }
253: Attr attr = ownerDoc.createAttributeNS(namespaceURI,
254: qualifiedName);
255: attr.setValue(value);
256: ((Element) currentNode).setAttributeNode(attr);
257: } else {
258: //Convert node type to String
259: throw new IllegalStateException(
260: "Current DOM Node type is "
261: + currentNode.getNodeType()
262: + "and does not allow attributes to be set ");
263: }
264: }
265:
266: /**
267: * Creates a DOM Atrribute @see org.w3c.dom.Node and associates it with the current DOM element @see org.w3c.dom.Node.
268: * @param prefix {@inheritDoc}
269: * @param namespaceURI {@inheritDoc}
270: * @param localName {@inheritDoc}
271: * @param value {@inheritDoc}
272: * @throws javax.xml.stream.XMLStreamException {@inheritDoc}
273: */
274: public void writeAttribute(String prefix, String namespaceURI,
275: String localName, String value) throws XMLStreamException {
276: if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
277: if (namespaceURI == null) {
278: throw new XMLStreamException(
279: "NamespaceURI cannot be null");
280: }
281: if (localName == null) {
282: throw new XMLStreamException(
283: "Local name cannot be null");
284: }
285: if (prefix == null) {
286: throw new XMLStreamException("prefix cannot be null");
287: }
288: String qualifiedName = null;
289: if (prefix.equals("")) {
290: qualifiedName = localName;
291: } else {
292:
293: qualifiedName = getQName(prefix, localName);
294: }
295: Attr attr = ownerDoc.createAttributeNS(namespaceURI,
296: qualifiedName);
297: attr.setValue(value);
298: ((Element) currentNode).setAttributeNodeNS(attr);
299: } else {
300: //Convert node type to String
301: throw new IllegalStateException(
302: "Current DOM Node type is "
303: + currentNode.getNodeType()
304: + "and does not allow attributes to be set ");
305: }
306:
307: }
308:
309: /**
310: * Creates a CDATA object @see org.w3c.dom.CDATASection.
311: * @param data {@inheritDoc}
312: * @throws javax.xml.stream.XMLStreamException {@inheritDoc}
313: */
314: public void writeCData(String data) throws XMLStreamException {
315: if (data == null) {
316: throw new XMLStreamException("CDATA cannot be null");
317: }
318:
319: CDATASection cdata = ownerDoc.createCDATASection(data);
320: getNode().appendChild(cdata);
321: }
322:
323: /**
324: * Creates a character object @see org.w3c.dom.Text and appends it to the current
325: * element in the DOM tree.
326: * @param charData {@inheritDoc}
327: * @throws javax.xml.stream.XMLStreamException {@inheritDoc}
328: */
329: public void writeCharacters(String charData)
330: throws XMLStreamException {
331: Text text = ownerDoc.createTextNode(charData);
332: currentNode.appendChild(text);
333: }
334:
335: /**
336: * Creates a character object @see org.w3c.dom.Text and appends it to the current
337: * element in the DOM tree.
338: * @param values {@inheritDoc}
339: * @param param {@inheritDoc}
340: * @param param2 {@inheritDoc}
341: * @throws javax.xml.stream.XMLStreamException {@inheritDoc}
342: */
343: public void writeCharacters(char[] values, int param, int param2)
344: throws XMLStreamException {
345:
346: Text text = ownerDoc.createTextNode(new String(values, param,
347: param2));
348: currentNode.appendChild(text);
349: }
350:
351: /**
352: * Creates a Comment object @see org.w3c.dom.Comment and appends it to the current
353: * element in the DOM tree.
354: * @param str {@inheritDoc}
355: * @throws javax.xml.stream.XMLStreamException {@inheritDoc}
356: */
357: public void writeComment(String str) throws XMLStreamException {
358: Comment comment = ownerDoc.createComment(str);
359: getNode().appendChild(comment);
360: }
361:
362: /**
363: * This method is not supported in this implementation.
364: * @param str {@inheritDoc}
365: * @throws javax.xml.stream.XMLStreamException {@inheritDoc}
366: */
367: public void writeDTD(String str) throws XMLStreamException {
368: throw new UnsupportedOperationException();
369: }
370:
371: /**
372: * Creates a DOM attribute and adds it to the current element in the DOM tree.
373: * @param namespaceURI {@inheritDoc}
374: * @throws javax.xml.stream.XMLStreamException {@inheritDoc}
375: */
376: public void writeDefaultNamespace(String namespaceURI)
377: throws XMLStreamException {
378: if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
379: String qname = XMLConstants.XMLNS_ATTRIBUTE;
380: ((Element) currentNode).setAttributeNS(
381: XMLConstants.XMLNS_ATTRIBUTE_NS_URI, qname,
382: namespaceURI);
383: } else {
384: //Convert node type to String
385: throw new IllegalStateException(
386: "Current DOM Node type is "
387: + currentNode.getNodeType()
388: + "and does not allow attributes to be set ");
389: }
390: }
391:
392: /**
393: * creates a DOM Element and appends it to the current element in the tree.
394: * @param localName {@inheritDoc}
395: * @throws javax.xml.stream.XMLStreamException {@inheritDoc}
396: */
397: public void writeEmptyElement(String localName)
398: throws XMLStreamException {
399: if (ownerDoc != null) {
400: Element element = ownerDoc.createElement(localName);
401: if (currentNode != null) {
402: currentNode.appendChild(element);
403: } else {
404: ownerDoc.appendChild(element);
405: }
406: }
407:
408: }
409:
410: /**
411: * creates a DOM Element and appends it to the current element in the tree.
412: * @param namespaceURI {@inheritDoc}
413: * @param localName {@inheritDoc}
414: * @throws javax.xml.stream.XMLStreamException {@inheritDoc}
415: */
416: public void writeEmptyElement(String namespaceURI, String localName)
417: throws XMLStreamException {
418: if (ownerDoc != null) {
419: String qualifiedName = null;
420: String prefix = null;
421: if (namespaceURI == null) {
422: throw new XMLStreamException(
423: "NamespaceURI cannot be null");
424: }
425: if (localName == null) {
426: throw new XMLStreamException(
427: "Local name cannot be null");
428: }
429:
430: if (namespaceContext != null) {
431: prefix = namespaceContext.getPrefix(namespaceURI);
432: }
433: if (prefix == null) {
434: throw new XMLStreamException("Namespace URI "
435: + namespaceURI + "is not bound to any prefix");
436: }
437: if ("".equals(prefix)) {
438: qualifiedName = localName;
439: } else {
440:
441: qualifiedName = getQName(prefix, localName);
442:
443: }
444: Element element = ownerDoc.createElementNS(namespaceURI,
445: qualifiedName);
446: if (currentNode != null) {
447: currentNode.appendChild(element);
448: } else {
449: ownerDoc.appendChild(element);
450: }
451: //currentNode = element;
452: }
453: }
454:
455: /**
456: * creates a DOM Element and appends it to the current element in the tree.
457: * @param prefix {@inheritDoc}
458: * @param localName {@inheritDoc}
459: * @param namespaceURI {@inheritDoc}
460: * @throws javax.xml.stream.XMLStreamException {@inheritDoc}
461: */
462: public void writeEmptyElement(String prefix, String localName,
463: String namespaceURI) throws XMLStreamException {
464: if (ownerDoc != null) {
465: if (namespaceURI == null) {
466: throw new XMLStreamException(
467: "NamespaceURI cannot be null");
468: }
469: if (localName == null) {
470: throw new XMLStreamException(
471: "Local name cannot be null");
472: }
473: if (prefix == null) {
474: throw new XMLStreamException("Prefix cannot be null");
475: }
476: String qualifiedName = null;
477: if ("".equals(prefix)) {
478: qualifiedName = localName;
479: } else {
480: qualifiedName = getQName(prefix, localName);
481: }
482: Element el = ownerDoc.createElementNS(namespaceURI,
483: qualifiedName);
484: if (currentNode != null) {
485: currentNode.appendChild(el);
486: } else {
487: ownerDoc.appendChild(el);
488: }
489:
490: }
491: }
492:
493: /**
494: * Will reset current Node pointer maintained by the implementation.
495: * @throws javax.xml.stream.XMLStreamException {@inheritDoc}
496: */
497: public void writeEndDocument() throws XMLStreamException {
498: //What do you want me to do eh! :)
499: currentNode = null;
500: for (int i = 0; i < depth; i++) {
501: if (needContextPop[depth]) {
502: needContextPop[depth] = false;
503: namespaceContext.popContext();
504: }
505: depth--;
506: }
507: depth = 0;
508: }
509:
510: /**
511: * Internal current Node pointer will point to the parent of the current Node.
512: * @throws javax.xml.stream.XMLStreamException {@inheritDoc}
513: */
514: public void writeEndElement() throws XMLStreamException {
515: Node node = currentNode.getParentNode();
516: if (currentNode.getNodeType() == Node.DOCUMENT_NODE) {
517: currentNode = null;
518: } else {
519: currentNode = node;
520: }
521: if (needContextPop[depth]) {
522: needContextPop[depth] = false;
523: namespaceContext.popContext();
524: }
525: depth--;
526: }
527:
528: /**
529: * Is not supported in this implementation.
530: * @param name {@inheritDoc}
531: * @throws javax.xml.stream.XMLStreamException {@inheritDoc}
532: */
533: public void writeEntityRef(String name) throws XMLStreamException {
534: EntityReference er = ownerDoc.createEntityReference(name);
535: currentNode.appendChild(er);
536: }
537:
538: /**
539: * creates a namespace attribute and will associate it with the current element in
540: * the DOM tree.
541: * @param prefix {@inheritDoc}
542: * @param namespaceURI {@inheritDoc}
543: * @throws javax.xml.stream.XMLStreamException {@inheritDoc}
544: */
545: public void writeNamespace(String prefix, String namespaceURI)
546: throws XMLStreamException {
547:
548: if (prefix == null) {
549: throw new XMLStreamException("prefix cannot be null");
550: }
551:
552: if (namespaceURI == null) {
553: throw new XMLStreamException("NamespaceURI cannot be null");
554: }
555:
556: String qname = null;
557:
558: if (prefix.equals("")) {
559: qname = XMLConstants.XMLNS_ATTRIBUTE;
560: } else {
561: qname = getQName(XMLConstants.XMLNS_ATTRIBUTE, prefix);
562: }
563:
564: ((Element) currentNode).setAttributeNS(
565: XMLConstants.XMLNS_ATTRIBUTE_NS_URI, qname,
566: namespaceURI);
567: }
568:
569: /**
570: * is not supported in this release.
571: * @param target {@inheritDoc}
572: * @throws javax.xml.stream.XMLStreamException {@inheritDoc}
573: */
574: public void writeProcessingInstruction(String target)
575: throws XMLStreamException {
576: if (target == null) {
577: throw new XMLStreamException("Target cannot be null");
578: }
579: ProcessingInstruction pi = ownerDoc
580: .createProcessingInstruction(target, "");
581: currentNode.appendChild(pi);
582: }
583:
584: /**
585: * is not supported in this release.
586: * @param target {@inheritDoc}
587: * @param data {@inheritDoc}
588: * @throws javax.xml.stream.XMLStreamException {@inheritDoc}
589: */
590: public void writeProcessingInstruction(String target, String data)
591: throws XMLStreamException {
592: if (target == null) {
593: throw new XMLStreamException("Target cannot be null");
594: }
595: ProcessingInstruction pi = ownerDoc
596: .createProcessingInstruction(target, data);
597: currentNode.appendChild(pi);
598: }
599:
600: /**
601: * will set version on the Document object when the DOM Node passed to this implementation
602: * supports DOM Level3 API's.
603: * @throws javax.xml.stream.XMLStreamException {@inheritDoc}
604: */
605: public void writeStartDocument() throws XMLStreamException {
606: try {
607: if (mXmlVersion != null) {
608: mXmlVersion.invoke(ownerDoc, new Object[] { "1.0" });
609: }
610: } catch (IllegalAccessException iae) {
611: throw new XMLStreamException(iae);
612: } catch (InvocationTargetException ite) {
613: throw new XMLStreamException(ite);
614: }
615: }
616:
617: /**
618: * will set version on the Document object when the DOM Node passed to this implementation
619: * supports DOM Level3 API's.
620: * @param version {@inheritDoc}
621: * @throws javax.xml.stream.XMLStreamException {@inheritDoc}
622: */
623: public void writeStartDocument(String version)
624: throws XMLStreamException {
625: try {
626: if (mXmlVersion != null) {
627: mXmlVersion.invoke(ownerDoc, new Object[] { version });
628: }
629: } catch (IllegalAccessException iae) {
630: throw new XMLStreamException(iae);
631: } catch (InvocationTargetException ite) {
632: throw new XMLStreamException(ite);
633: }
634: }
635:
636: /**
637: * will set version on the Document object when the DOM Node passed to this implementation
638: * supports DOM Level3 API's.
639: * @param encoding {@inheritDoc}
640: * @param version {@inheritDoc}
641: * @throws javax.xml.stream.XMLStreamException {@inheritDoc}
642: */
643: public void writeStartDocument(String encoding, String version)
644: throws XMLStreamException {
645: try {
646: if (mXmlVersion != null) {
647: mXmlVersion.invoke(ownerDoc, new Object[] { version });
648: }
649: } catch (IllegalAccessException iae) {
650: throw new XMLStreamException(iae);
651: } catch (InvocationTargetException ite) {
652: throw new XMLStreamException(ite);
653: }
654: //TODO: What to do with encoding.-Venu
655: }
656:
657: /**
658: * creates a DOM Element and appends it to the current element in the tree.
659: * @param localName {@inheritDoc}
660: * @throws javax.xml.stream.XMLStreamException {@inheritDoc}
661: */
662: public void writeStartElement(String localName)
663: throws XMLStreamException {
664: if (ownerDoc != null) {
665: Element element = ownerDoc.createElement(localName);
666: if (currentNode != null) {
667: currentNode.appendChild(element);
668: } else {
669: ownerDoc.appendChild(element);
670: }
671: currentNode = element;
672: }
673: if (needContextPop[depth]) {
674: namespaceContext.pushContext();
675: }
676: depth++;
677: }
678:
679: /**
680: * creates a DOM Element and appends it to the current element in the tree.
681: * @param namespaceURI {@inheritDoc}
682: * @param localName {@inheritDoc}
683: * @throws javax.xml.stream.XMLStreamException {@inheritDoc}
684: */
685: public void writeStartElement(String namespaceURI, String localName)
686: throws XMLStreamException {
687: if (ownerDoc != null) {
688: String qualifiedName = null;
689: String prefix = null;
690:
691: if (namespaceURI == null) {
692: throw new XMLStreamException(
693: "NamespaceURI cannot be null");
694: }
695: if (localName == null) {
696: throw new XMLStreamException(
697: "Local name cannot be null");
698: }
699:
700: if (namespaceContext != null) {
701: prefix = namespaceContext.getPrefix(namespaceURI);
702: }
703: if (prefix == null) {
704: throw new XMLStreamException("Namespace URI "
705: + namespaceURI + "is not bound to any prefix");
706: }
707: if ("".equals(prefix)) {
708: qualifiedName = localName;
709: } else {
710: qualifiedName = getQName(prefix, localName);
711: }
712:
713: Element element = ownerDoc.createElementNS(namespaceURI,
714: qualifiedName);
715:
716: if (currentNode != null) {
717: currentNode.appendChild(element);
718: } else {
719: ownerDoc.appendChild(element);
720: }
721: currentNode = element;
722: }
723: if (needContextPop[depth]) {
724: namespaceContext.pushContext();
725: }
726: depth++;
727: }
728:
729: /**
730: * creates a DOM Element and appends it to the current element in the tree.
731: * @param prefix {@inheritDoc}
732: * @param localName {@inheritDoc}
733: * @param namespaceURI {@inheritDoc}
734: * @throws javax.xml.stream.XMLStreamException {@inheritDoc}
735: */
736: public void writeStartElement(String prefix, String localName,
737: String namespaceURI) throws XMLStreamException {
738:
739: if (ownerDoc != null) {
740: String qname = null;
741: if (namespaceURI == null) {
742: throw new XMLStreamException(
743: "NamespaceURI cannot be null");
744: }
745: if (localName == null) {
746: throw new XMLStreamException(
747: "Local name cannot be null");
748: }
749: if (prefix == null) {
750: throw new XMLStreamException("Prefix cannot be null");
751: }
752:
753: if (prefix.equals("")) {
754: qname = localName;
755: } else {
756: qname = getQName(prefix, localName);
757: }
758:
759: Element el = ownerDoc.createElementNS(namespaceURI, qname);
760:
761: if (currentNode != null) {
762: currentNode.appendChild(el);
763: } else {
764: ownerDoc.appendChild(el);
765: }
766: currentNode = el;
767: if (needContextPop[depth]) {
768: namespaceContext.pushContext();
769: }
770: depth++;
771:
772: }
773: }
774:
775: private String getQName(String prefix, String localName) {
776: stringBuffer.setLength(0);
777: stringBuffer.append(prefix);
778: stringBuffer.append(":");
779: stringBuffer.append(localName);
780: return stringBuffer.toString();
781: }
782:
783: private Node getNode() {
784: if (currentNode == null) {
785: return ownerDoc;
786: } else {
787: return currentNode;
788: }
789: }
790: }
|