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