KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > message > SOAPDocumentImpl


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.axis.message;
18
19 import javax.xml.namespace.QName JavaDoc;
20 import org.apache.axis.AxisFault;
21 import org.apache.axis.Constants;
22 import org.apache.axis.SOAPPart;
23 import org.apache.axis.utils.Mapping;
24 import org.apache.axis.utils.XMLUtils;
25 import org.w3c.dom.Attr JavaDoc;
26 import org.w3c.dom.CDATASection JavaDoc;
27 import org.w3c.dom.Comment JavaDoc;
28 import org.w3c.dom.DOMException JavaDoc;
29 import org.w3c.dom.DOMImplementation JavaDoc;
30 import org.w3c.dom.Document JavaDoc;
31 import org.w3c.dom.DocumentFragment JavaDoc;
32 import org.w3c.dom.DocumentType JavaDoc;
33 import org.w3c.dom.Element JavaDoc;
34 import org.w3c.dom.EntityReference JavaDoc;
35 import org.w3c.dom.NamedNodeMap JavaDoc;
36 import org.w3c.dom.Node JavaDoc;
37 import org.w3c.dom.NodeList JavaDoc;
38 import org.w3c.dom.ProcessingInstruction JavaDoc;
39
40 import javax.xml.parsers.ParserConfigurationException JavaDoc;
41 import javax.xml.soap.SOAPException JavaDoc;
42
43 /**
44  * SOAPDcoumentImpl implements the Document API for SOAPPART. At the moment, it
45  * again delgate the XERCES DOM Implementation Here is my argument on it: I
46  * guess that there is 3 way to implement this. - fully implement the DOM API
47  * here myself. => This is too much and duplicated work. - extends XERCES
48  * Implementation => this makes we are fixed to one Implementation - choose
49  * delgate depends on the user's parser preference => This is the practically
50  * best solution I have now
51  *
52  * @author Heejune Ahn (cityboy@tmax.co.kr)
53  *
54  */

55
56 public class SOAPDocumentImpl
57 implements org.w3c.dom.Document JavaDoc, java.io.Serializable JavaDoc {
58
59     // Depending on the user's parser preference
60
protected Document JavaDoc delegate = null;
61     protected SOAPPart soapPart = null;
62
63     /**
64      * Construct the Document
65      *
66      * @param sp the soap part
67      */

68     public SOAPDocumentImpl(SOAPPart sp) {
69         try {
70             delegate = XMLUtils.newDocument();
71         } catch (ParserConfigurationException JavaDoc e) {
72             // Do nothing
73
}
74         soapPart = sp;
75     }
76
77     /**
78      * @todo : link with SOAP
79      *
80      * @return
81      */

82     public DocumentType JavaDoc getDoctype() {
83         return delegate.getDoctype();
84     }
85
86     public DOMImplementation JavaDoc getImplementation() {
87         return delegate.getImplementation();
88     }
89
90     /**
91      * should not be called, the method will be handled in SOAPPart
92      *
93      * @return
94      */

95     public Element JavaDoc getDocumentElement() {
96         return soapPart.getDocumentElement();
97     }
98
99     /**
100      * based on the tagName, we will make different kind SOAP Elements Instance
101      * Is really we can determine the Type by the Tagname???
102      *
103      * @todo : verify this method
104      *
105      * @param tagName
106      * @return @throws
107      * DOMException
108      */

109
110     public org.w3c.dom.Element JavaDoc createElement(String JavaDoc tagName)
111     throws DOMException JavaDoc {
112         int index = tagName.indexOf(":");
113         String JavaDoc prefix, localname;
114         if (index < 0) {
115             prefix = "";
116             localname = tagName;
117         } else {
118             prefix = tagName.substring(0, index);
119             localname = tagName.substring(index + 1);
120         }
121
122         try {
123             SOAPEnvelope soapenv =
124                 (org.apache.axis.message.SOAPEnvelope) soapPart.getEnvelope();
125             if (soapenv != null) {
126                 if (tagName.equalsIgnoreCase(Constants.ELEM_ENVELOPE))
127                     new SOAPEnvelope();
128                 if (tagName.equalsIgnoreCase(Constants.ELEM_HEADER))
129                     return new SOAPHeader(soapenv, soapenv.getSOAPConstants());
130                 if (tagName.equalsIgnoreCase(Constants.ELEM_BODY))
131                     return new SOAPBody(soapenv, soapenv.getSOAPConstants());
132                 if (tagName.equalsIgnoreCase(Constants.ELEM_FAULT))
133                     return new SOAPEnvelope();
134                 if (tagName.equalsIgnoreCase(Constants.ELEM_FAULT_DETAIL))
135                     return new SOAPFault(new AxisFault(tagName));
136                 else {
137                     return new MessageElement("", prefix, localname);
138                 }
139             } else {
140                 return new MessageElement("", prefix, localname);
141             }
142
143         } catch (SOAPException JavaDoc se) {
144             throw new DOMException JavaDoc(DOMException.INVALID_STATE_ERR, "");
145         }
146     }
147
148     /**
149      *
150      * Creates an empty <code>DocumentFragment</code> object. @todo not
151      * implemented yet
152      *
153      * @return A new <code>DocumentFragment</code>.
154      */

155     public DocumentFragment JavaDoc createDocumentFragment() {
156         return delegate.createDocumentFragment();
157     }
158     /**
159      * Creates a <code>Text</code> node given the specified string.
160      *
161      * @param data
162      * The data for the node.
163      * @return The new <code>Text</code> object.
164      */

165     public org.w3c.dom.Text JavaDoc createTextNode(String JavaDoc data) {
166         org.apache.axis.message.Text me =
167             new org.apache.axis.message.Text(delegate.createTextNode(data));
168         me.setOwnerDocument(soapPart);
169         return me;
170
171     }
172
173     /**
174      * Creates a <code>Comment</code> node given the specified string.
175      *
176      * @param data
177      * The data for the node.
178      * @return The new <code>Comment</code> object.
179      */

180     public Comment JavaDoc createComment(String JavaDoc data) {
181         return new org.apache.axis.message.CommentImpl(data);
182     }
183
184     /**
185      * Creates a <code>CDATASection</code> node whose value is the specified
186      * string.
187      *
188      * @param data
189      * The data for the <code>CDATASection</code> contents.
190      * @return The new <code>CDATASection</code> object.
191      * @exception DOMException
192      * NOT_SUPPORTED_ERR: Raised if this document is an HTML
193      * document.
194      */

195     public CDATASection JavaDoc createCDATASection(String JavaDoc data) throws DOMException JavaDoc {
196         return new CDATAImpl(data);
197     }
198
199     /**
200      * Creates a <code>ProcessingInstruction</code> node given the specified
201      * name and data strings.
202      *
203      * @param target
204      * The target part of the processing instruction.
205      * @param data
206      * The data for the node.
207      * @return The new <code>ProcessingInstruction</code> object.
208      * @exception DOMException
209      * INVALID_CHARACTER_ERR: Raised if the specified target
210      * contains an illegal character. <br>NOT_SUPPORTED_ERR:
211      * Raised if this document is an HTML document.
212      */

213     public ProcessingInstruction JavaDoc createProcessingInstruction(
214             String JavaDoc target,
215             String JavaDoc data)
216     throws DOMException JavaDoc {
217         throw new java.lang.UnsupportedOperationException JavaDoc(
218         "createProcessingInstruction");
219     }
220
221     /**
222      * @todo: How Axis will maintain the Attribute representation ?
223      */

224     public Attr JavaDoc createAttribute(String JavaDoc name) throws DOMException JavaDoc {
225         return delegate.createAttribute(name);
226     }
227
228     /**
229      * @param name
230      * @return @throws
231      * DOMException
232      */

233     public EntityReference JavaDoc createEntityReference(String JavaDoc name)
234     throws DOMException JavaDoc {
235         throw new java.lang.UnsupportedOperationException JavaDoc(
236         "createEntityReference");
237     }
238
239     // implemented by yoonforh 2004-07-30 02:48:50
240
public Node JavaDoc importNode(Node JavaDoc importedNode, boolean deep) throws DOMException JavaDoc {
241         Node JavaDoc targetNode = null;
242     
243         int type = importedNode.getNodeType();
244         switch (type) {
245         case ELEMENT_NODE :
246             Element JavaDoc el = (Element JavaDoc) importedNode;
247             if (deep) {
248                 targetNode = new SOAPBodyElement(el);
249                 break;
250             }
251     
252             SOAPBodyElement target = new SOAPBodyElement();
253             org.w3c.dom.NamedNodeMap JavaDoc attrs = el.getAttributes();
254             for (int i = 0; i < attrs.getLength(); i++) {
255                 org.w3c.dom.Node JavaDoc att = attrs.item(i);
256                 if (att.getNamespaceURI() != null &&
257                     att.getPrefix() != null &&
258                     att.getNamespaceURI().equals(Constants.NS_URI_XMLNS) &&
259                     att.getPrefix().equals("xmlns")) {
260                     Mapping map = new Mapping(att.getNodeValue(), att.getLocalName());
261                     target.addMapping(map);
262                 }
263                 if (att.getLocalName() != null) {
264                     target.addAttribute(att.getPrefix(),
265                             att.getNamespaceURI(),
266                             att.getLocalName(),
267                             att.getNodeValue());
268                 } else if (att.getNodeName() != null) {
269                     target.addAttribute(att.getPrefix(),
270                             att.getNamespaceURI(),
271                             att.getNodeName(),
272                             att.getNodeValue());
273                 }
274             }
275     
276             if (el.getLocalName() == null) {
277                 target.setName(el.getNodeName());
278             } else {
279                 target.setQName(new QName JavaDoc(el.getNamespaceURI(), el.getLocalName()));
280             }
281             targetNode = target;
282             break;
283     
284         case ATTRIBUTE_NODE :
285             if (importedNode.getLocalName() == null) {
286                 targetNode = createAttribute(importedNode.getNodeName());
287             } else {
288                 targetNode = createAttributeNS(importedNode.getNamespaceURI(),
289                                importedNode.getLocalName());
290             }
291             break;
292     
293         case TEXT_NODE :
294             targetNode = createTextNode(importedNode.getNodeValue());
295             break;
296     
297         case CDATA_SECTION_NODE :
298             targetNode = createCDATASection(importedNode.getNodeValue());
299             break;
300     
301         case COMMENT_NODE :
302             targetNode = createComment(importedNode.getNodeValue());
303             break;
304     
305         case DOCUMENT_FRAGMENT_NODE :
306             targetNode = createDocumentFragment();
307             if (deep) {
308                 org.w3c.dom.NodeList JavaDoc children = importedNode.getChildNodes();
309                 for (int i = 0; i < children.getLength(); i++){
310                     targetNode.appendChild(importNode(children.item(i), true));
311                 }
312             }
313             break;
314     
315         case ENTITY_REFERENCE_NODE :
316             targetNode = createEntityReference(importedNode.getNodeName());
317             break;
318     
319         case PROCESSING_INSTRUCTION_NODE :
320             ProcessingInstruction JavaDoc pi = (ProcessingInstruction JavaDoc) importedNode;
321             targetNode = createProcessingInstruction(pi.getTarget(), pi.getData());
322             break;
323     
324         case ENTITY_NODE :
325             // TODO : ...
326
throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR, "Entity nodes are not supported.");
327     
328         case NOTATION_NODE :
329             // TODO : any idea?
330
throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR, "Notation nodes are not supported.");
331     
332         case DOCUMENT_TYPE_NODE :
333             throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR, "DocumentType nodes cannot be imported.");
334     
335         case DOCUMENT_NODE :
336             throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR, "Document nodes cannot be imported.");
337     
338         default :
339             throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR, "Node type (" + type + ") cannot be imported.");
340         }
341     
342         return targetNode;
343     }
344
345     /**
346      * Return SOAPElements (what if they want SOAPEnvelope or Header/Body?)
347      *
348      * @param namespaceURI
349      * @param qualifiedName
350      * @return @throws
351      * DOMException
352      */

353     public Element JavaDoc createElementNS(String JavaDoc namespaceURI, String JavaDoc qualifiedName)
354     throws DOMException JavaDoc {
355         org.apache.axis.soap.SOAPConstants soapConstants = null;
356         if (Constants.URI_SOAP11_ENV.equals(namespaceURI)) {
357             soapConstants = org.apache.axis.soap.SOAPConstants.SOAP11_CONSTANTS;
358         } else if (Constants.URI_SOAP12_ENV.equals(namespaceURI)) {
359             soapConstants = org.apache.axis.soap.SOAPConstants.SOAP12_CONSTANTS;
360         }
361
362         // For special SOAP Element
363
MessageElement me = null;
364         if (soapConstants != null) {
365             if (qualifiedName.equals(Constants.ELEM_ENVELOPE)) {
366                 // TODO: confirm SOAP 1.1!
367
me = new SOAPEnvelope(soapConstants);
368             } else if (qualifiedName.equals(Constants.ELEM_HEADER)) {
369                 me = new SOAPHeader(null, soapConstants);
370                 // Dummy SOAPEnv required?
371
} else if (qualifiedName.equals(Constants.ELEM_BODY)) {
372                 me = new SOAPBody(null, soapConstants);
373             } else if (qualifiedName.equals(Constants.ELEM_FAULT)) {
374                 me = null;
375             } else if (qualifiedName.equals(Constants.ELEM_FAULT_DETAIL)) {
376                 // TODO:
377
me = null;
378             } else {
379                 throw new DOMException JavaDoc(
380                         DOMException.INVALID_STATE_ERR,
381                 "No such Localname for SOAP URI");
382             }
383             // TODO:
384
return null;
385             // general Elements
386
} else {
387             me = new MessageElement(namespaceURI, qualifiedName);
388         }
389
390         if (me != null)
391             me.setOwnerDocument(soapPart);
392
393         return me;
394
395     }
396
397     /**
398      * Attribute is not particularly dealt with in SAAJ.
399      *
400      */

401     public Attr JavaDoc createAttributeNS(String JavaDoc namespaceURI, String JavaDoc qualifiedName)
402     throws DOMException JavaDoc {
403         return delegate.createAttributeNS(namespaceURI, qualifiedName);
404     }
405
406     /**
407      * search the SOAPPart in order of SOAPHeader and SOAPBody for the
408      * requested Element name
409      *
410      */

411     public NodeList JavaDoc getElementsByTagNameNS(
412             String JavaDoc namespaceURI,
413             String JavaDoc localName) {
414         try {
415             NodeListImpl list = new NodeListImpl();
416             if (soapPart != null) {
417                 SOAPEnvelope soapEnv =
418                     (org.apache.axis.message.SOAPEnvelope) soapPart
419                     .getEnvelope();
420                 SOAPHeader header =
421                     (org.apache.axis.message.SOAPHeader) soapEnv.getHeader();
422                 if (header != null) {
423                     list.addNodeList(header.getElementsByTagNameNS(
424                             namespaceURI,
425                             localName));
426                 }
427                 SOAPBody body =
428                     (org.apache.axis.message.SOAPBody) soapEnv.getBody();
429                 if (body != null) {
430                     list.addNodeList(body.getElementsByTagNameNS(
431                             namespaceURI,
432                             localName));
433                 }
434             }
435             return list;
436         } catch (SOAPException JavaDoc se) {
437             throw new DOMException JavaDoc(DOMException.INVALID_STATE_ERR, "");
438         }
439     }
440
441     /**
442      * search the SOAPPart in order of SOAPHeader and SOAPBody for the
443      * requested Element name
444      *
445      */

446     public NodeList JavaDoc getElementsByTagName(String JavaDoc localName) {
447
448         try {
449             NodeListImpl list = new NodeListImpl();
450             if (soapPart != null) {
451                 SOAPEnvelope soapEnv =
452                     (org.apache.axis.message.SOAPEnvelope) soapPart
453                     .getEnvelope();
454                 SOAPHeader header =
455                     (org.apache.axis.message.SOAPHeader) soapEnv.getHeader();
456                 if (header != null) {
457                     list.addNodeList(header.getElementsByTagName(localName));
458                 }
459                 SOAPBody body =
460                     (org.apache.axis.message.SOAPBody) soapEnv.getBody();
461                 if (body != null) {
462                     list.addNodeList(body.getElementsByTagName(localName));
463                 }
464             }
465             return list;
466         } catch (SOAPException JavaDoc se) {
467             throw new DOMException JavaDoc(DOMException.INVALID_STATE_ERR, "");
468         }
469     }
470     /**
471      * Returns the <code>Element</code> whose <code>ID</code> is given by
472      * <code>elementId</code>. If no such element exists, returns <code>null</code>.
473      * Behavior is not defined if more than one element has this <code>ID</code>.
474      * The DOM implementation must have information that says which attributes
475      * are of type ID. Attributes with the name "ID" are not of type ID unless
476      * so defined. Implementations that do not know whether attributes are of
477      * type ID or not are expected to return <code>null</code>.
478      *
479      * @param elementId
480      * The unique <code>id</code> value for an element.
481      * @return The matching element.
482      * @since DOM Level 2
483      */

484     public Element JavaDoc getElementById(String JavaDoc elementId) {
485         return delegate.getElementById(elementId);
486     }
487
488     /**
489      * Node Implementation
490      *
491      */

492
493     public String JavaDoc getNodeName() {
494         return null;
495     }
496
497     public String JavaDoc getNodeValue() throws DOMException JavaDoc {
498         throw new DOMException JavaDoc(
499                 DOMException.NO_DATA_ALLOWED_ERR,
500                 "Cannot use TextNode.get in " + this);
501     }
502
503     public void setNodeValue(String JavaDoc nodeValue) throws DOMException JavaDoc {
504         throw new DOMException JavaDoc(
505                 DOMException.NO_DATA_ALLOWED_ERR,
506                 "Cannot use TextNode.set in " + this);
507     }
508
509     /**
510      * override it in sub-classes
511      *
512      * @return
513      */

514     public short getNodeType() {
515         return Node.DOCUMENT_NODE;
516     }
517
518     public Node JavaDoc getParentNode() {
519         return null;
520     }
521
522     public NodeList JavaDoc getChildNodes() {
523         try {
524             if (soapPart != null) {
525                 NodeListImpl children = new NodeListImpl();
526                 children.addNode(soapPart.getEnvelope());
527                 return children;
528             } else {
529                 return NodeListImpl.EMPTY_NODELIST;
530             }
531         } catch (SOAPException JavaDoc se) {
532             throw new DOMException JavaDoc(DOMException.INVALID_STATE_ERR, "");
533         }
534
535     }
536
537     /**
538      * Do we have to count the Attributes as node ????
539      *
540      * @return
541      */

542     public Node JavaDoc getFirstChild() {
543         try {
544             if (soapPart != null)
545                 return (org.apache.axis.message.SOAPEnvelope) soapPart
546                 .getEnvelope();
547             else
548                 return null;
549         } catch (SOAPException JavaDoc se) {
550             throw new DOMException JavaDoc(DOMException.INVALID_STATE_ERR, "");
551         }
552
553     }
554
555     /**
556      * @return
557      */

558     public Node JavaDoc getLastChild() {
559         try {
560             if (soapPart != null)
561                 return (org.apache.axis.message.SOAPEnvelope) soapPart
562                 .getEnvelope();
563             else
564                 return null;
565         } catch (SOAPException JavaDoc se) {
566             throw new DOMException JavaDoc(DOMException.INVALID_STATE_ERR, "");
567         }
568
569     }
570
571     public Node JavaDoc getPreviousSibling() {
572         return null;
573     }
574     public Node JavaDoc getNextSibling() {
575
576         return null;
577     }
578
579     public NamedNodeMap JavaDoc getAttributes() {
580         return null;
581     }
582
583     /**
584      *
585      * we have to have a link to them...
586      */

587     public Document JavaDoc getOwnerDocument() {
588         return null;
589     }
590
591     /**
592      */

593     public Node JavaDoc insertBefore(Node JavaDoc newChild, Node JavaDoc refChild)
594     throws DOMException JavaDoc {
595         throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR, "");
596     }
597
598     public Node JavaDoc replaceChild(Node JavaDoc newChild, Node JavaDoc oldChild)
599     throws DOMException JavaDoc {
600         throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR, "");
601     }
602
603     public Node JavaDoc removeChild(Node JavaDoc oldChild) throws DOMException JavaDoc {
604         try {
605             Node JavaDoc envNode;
606             if (soapPart != null) {
607                 envNode = soapPart.getEnvelope();
608                 if (envNode.equals(oldChild)) {
609                     return envNode;
610                 }
611             }
612             throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR, "");
613         } catch (SOAPException JavaDoc se) {
614             throw new DOMException JavaDoc(DOMException.INVALID_STATE_ERR, "");
615         }
616     }
617
618     public Node JavaDoc appendChild(Node JavaDoc newChild) throws DOMException JavaDoc {
619         throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR, "");
620     }
621
622     public boolean hasChildNodes() {
623         try {
624             if (soapPart != null) {
625                 if (soapPart.getEnvelope() != null) {
626                     return true;
627                 }
628             }
629             return false;
630         } catch (SOAPException JavaDoc se) {
631             throw new DOMException JavaDoc(DOMException.INVALID_STATE_ERR, "");
632         }
633
634     }
635
636     /**
637      * @todo: Study it more.... to implement the deep mode correctly.
638      *
639      */

640     public Node JavaDoc cloneNode(boolean deep) {
641         throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR, "");
642     }
643
644     /**
645      * @todo: is it OK to simply call the superclass?
646      *
647      */

648     public void normalize() {
649         throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR, "");
650     }
651
652     // TODO: fill appropriate features
653
private String JavaDoc[] features = { "foo", "bar" };
654     private String JavaDoc version = "version 2.0";
655
656     public boolean isSupported(String JavaDoc feature, String JavaDoc version) {
657         if (!version.equalsIgnoreCase(version))
658             return false;
659         else
660             return true;
661     }
662
663     public String JavaDoc getPrefix() {
664         throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR, "");
665     }
666     public void setPrefix(String JavaDoc prefix) {
667         throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR, "");
668     }
669
670     public String JavaDoc getNamespaceURI() {
671         throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR, "");
672     }
673     public void setNamespaceURI(String JavaDoc nsURI) {
674         throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR, "");
675     }
676
677     public String JavaDoc getLocalName() {
678         throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR, "");
679     }
680
681     public boolean hasAttributes() {
682         throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR, "");
683     }
684 }
685
Popular Tags