KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > 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.jboss.axis.message;
18
19 import org.jboss.axis.AxisFault;
20 import org.jboss.axis.Constants;
21 import org.jboss.axis.MessagePart;
22 import org.jboss.axis.utils.XMLUtils;
23 import org.w3c.dom.Attr JavaDoc;
24 import org.w3c.dom.CDATASection JavaDoc;
25 import org.w3c.dom.Comment JavaDoc;
26 import org.w3c.dom.DOMException JavaDoc;
27 import org.w3c.dom.DOMImplementation JavaDoc;
28 import org.w3c.dom.Document JavaDoc;
29 import org.w3c.dom.DocumentFragment JavaDoc;
30 import org.w3c.dom.DocumentType JavaDoc;
31 import org.w3c.dom.Element JavaDoc;
32 import org.w3c.dom.EntityReference JavaDoc;
33 import org.w3c.dom.NamedNodeMap JavaDoc;
34 import org.w3c.dom.Node JavaDoc;
35 import org.w3c.dom.NodeList JavaDoc;
36 import org.w3c.dom.ProcessingInstruction JavaDoc;
37 import org.w3c.dom.DOMConfiguration JavaDoc;
38 import org.w3c.dom.UserDataHandler 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 public class SOAPDocumentImpl implements Document JavaDoc
56 {
57    // Depending on the user's parser preference
58
Document JavaDoc delegate = null;
59    MessagePart soapPart = null;
60
61    /**
62     * Construct the Document
63     *
64     * @param sp
65     */

66    public SOAPDocumentImpl(MessagePart sp)
67    {
68       try
69       {
70          delegate = XMLUtils.newDocument();
71       }
72       catch (ParserConfigurationException JavaDoc e)
73       {
74          // Do nothing
75
}
76       soapPart = sp;
77    }
78
79    public DocumentType JavaDoc getDoctype()
80    {
81       return delegate.getDoctype();
82    }
83
84    public DOMImplementation JavaDoc getImplementation()
85    {
86       return delegate.getImplementation();
87    }
88
89    /**
90     * should not be called, the method will be handled in SOAPPart
91     *
92     * @return
93     */

94    public Element JavaDoc getDocumentElement()
95    {
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     * @param tagName
104     * @return @throws
105     * DOMException
106     */

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

162    public DocumentFragment JavaDoc createDocumentFragment()
163    {
164       return delegate.createDocumentFragment();
165    }
166
167    /**
168     * Creates a <code>Text</code> node given the specified string.
169     *
170     * @param data The data for the node.
171     * @return The new <code>Text</code> object.
172     */

173    public org.w3c.dom.Text JavaDoc createTextNode(String JavaDoc data)
174    {
175       return new TextImpl(delegate.createTextNode(data));
176    }
177
178    /**
179     * Creates a <code>Comment</code> node given the specified string.
180     *
181     * @param data The data for the node.
182     * @return The new <code>Comment</code> object.
183     */

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

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

214    public ProcessingInstruction JavaDoc createProcessingInstruction(String JavaDoc target,
215                                                             String JavaDoc data)
216            throws DOMException JavaDoc
217    {
218       throw new java.lang.UnsupportedOperationException JavaDoc("createProcessingInstruction");
219    }
220
221    public Attr JavaDoc createAttribute(String JavaDoc name) throws DOMException JavaDoc
222    {
223       return delegate.createAttribute(name);
224    }
225
226    /**
227     * @param name
228     * @return @throws
229     * DOMException
230     */

231    public EntityReference JavaDoc createEntityReference(String JavaDoc name)
232            throws DOMException JavaDoc
233    {
234       throw new java.lang.UnsupportedOperationException JavaDoc("createEntityReference");
235    }
236
237    public Node JavaDoc importNode(Node JavaDoc importedNode, boolean deep)
238            throws DOMException JavaDoc
239    {
240       throw new java.lang.UnsupportedOperationException JavaDoc("importNode");
241    }
242
243    /**
244     * Return SOAPElements (what if they want SOAPEnvelope or Header/Body?)
245     *
246     * @param namespaceURI
247     * @param qualifiedName
248     * @return @throws
249     * DOMException
250     */

251    public Element JavaDoc createElementNS(String JavaDoc namespaceURI, String JavaDoc qualifiedName)
252            throws DOMException JavaDoc
253    {
254       org.jboss.axis.soap.SOAPConstants soapConstants = null;
255       if (Constants.URI_SOAP11_ENV.equals(namespaceURI))
256       {
257          soapConstants = org.jboss.axis.soap.SOAPConstants.SOAP11_CONSTANTS;
258       }
259       else if (Constants.URI_SOAP12_ENV.equals(namespaceURI))
260       {
261          soapConstants = org.jboss.axis.soap.SOAPConstants.SOAP12_CONSTANTS;
262       }
263
264       // For special SOAP Element
265
SOAPElementAxisImpl me = null;
266       if (soapConstants != null)
267       {
268          if (qualifiedName.equals(Constants.ELEM_ENVELOPE))
269          {
270             // TODO: confirm SOAP 1.1!
271
me = new SOAPEnvelopeAxisImpl(soapConstants);
272          }
273          else if (qualifiedName.equals(Constants.ELEM_HEADER))
274          {
275             me = new SOAPHeaderAxisImpl(null, soapConstants);
276             // Dummy SOAPEnv required?
277
}
278          else if (qualifiedName.equals(Constants.ELEM_BODY))
279          {
280             me = new SOAPBodyAxisImpl(null, soapConstants);
281          }
282          else if (qualifiedName.equals(Constants.ELEM_FAULT))
283          {
284             me = null;
285          }
286          else if (qualifiedName.equals(Constants.ELEM_FAULT_DETAIL))
287          {
288             // TODO:
289
me = null;
290          }
291          else
292          {
293             throw new DOMException JavaDoc(DOMException.INVALID_STATE_ERR,
294                     "No such Localname for SOAP URI");
295          }
296          // TODO:
297
return null;
298          // general Elements
299
}
300       else
301       {
302          me = new SOAPElementAxisImpl(namespaceURI, qualifiedName);
303       }
304
305       if (me != null)
306          me.setOwnerDocument(soapPart);
307
308       return me;
309
310    }
311
312    /**
313     * Attribute is not particularly dealt with in SAAJ.
314     */

315    public Attr JavaDoc createAttributeNS(String JavaDoc namespaceURI, String JavaDoc qualifiedName)
316            throws DOMException JavaDoc
317    {
318       return delegate.createAttributeNS(namespaceURI, qualifiedName);
319    }
320
321    /**
322     * search the SOAPPart in order of SOAPHeader and SOAPBody for the
323     * requested Element name
324     */

325    public NodeList JavaDoc getElementsByTagNameNS(String JavaDoc namespaceURI,
326                                           String JavaDoc localName)
327    {
328       try
329       {
330          if (soapPart != null)
331          {
332             SOAPEnvelopeAxisImpl soapEnv =
333                     (org.jboss.axis.message.SOAPEnvelopeAxisImpl)soapPart
334                     .getEnvelope();
335             SOAPHeaderAxisImpl header =
336                     (org.jboss.axis.message.SOAPHeaderAxisImpl)soapEnv.getHeader();
337             if (header != null)
338             {
339                return header.getElementsByTagNameNS(namespaceURI,
340                        localName);
341             }
342             SOAPBodyAxisImpl body =
343                     (org.jboss.axis.message.SOAPBodyAxisImpl)soapEnv.getHeader();
344             if (body != null)
345             {
346                return header.getElementsByTagNameNS(namespaceURI,
347                        localName);
348             }
349          }
350          return null;
351       }
352       catch (SOAPException JavaDoc se)
353       {
354          throw new DOMException JavaDoc(DOMException.INVALID_STATE_ERR, "");
355       }
356    }
357
358    /**
359     * search the SOAPPart in order of SOAPHeader and SOAPBody for the
360     * requested Element name
361     */

362    public NodeList JavaDoc getElementsByTagName(String JavaDoc localName)
363    {
364
365       try
366       {
367          if (soapPart != null)
368          {
369             SOAPEnvelopeAxisImpl soapEnv =
370                     (org.jboss.axis.message.SOAPEnvelopeAxisImpl)soapPart
371                     .getEnvelope();
372             SOAPHeaderAxisImpl header =
373                     (org.jboss.axis.message.SOAPHeaderAxisImpl)soapEnv.getHeader();
374             if (header != null)
375             {
376                return header.getElementsByTagName(localName);
377             }
378             SOAPBodyAxisImpl body =
379                     (org.jboss.axis.message.SOAPBodyAxisImpl)soapEnv.getHeader();
380             if (body != null)
381             {
382                return header.getElementsByTagName(localName);
383             }
384          }
385          return null;
386       }
387       catch (SOAPException JavaDoc se)
388       {
389          throw new DOMException JavaDoc(DOMException.INVALID_STATE_ERR, "");
390       }
391    }
392
393    /**
394     * Returns the <code>Element</code> whose <code>ID</code> is given by
395     * <code>elementId</code>. If no such element exists, returns <code>null</code>.
396     * Behavior is not defined if more than one element has this <code>ID</code>.
397     * The DOM implementation must have information that says which attributes
398     * are of type ID. Attributes with the name "ID" are not of type ID unless
399     * so defined. Implementations that do not know whether attributes are of
400     * type ID or not are expected to return <code>null</code>.
401     *
402     * @param elementId The unique <code>id</code> value for an element.
403     * @return The matching element.
404     * @since DOM Level 2
405     */

406    public Element JavaDoc getElementById(String JavaDoc elementId)
407    {
408       return delegate.getElementById(elementId);
409    }
410
411    /**
412     * Node Implementation
413     */

414
415    public String JavaDoc getNodeName()
416    {
417       return null;
418    }
419
420    public String JavaDoc getNodeValue() throws DOMException JavaDoc
421    {
422       throw new DOMException JavaDoc(DOMException.NO_DATA_ALLOWED_ERR,
423               "Cannot use TextNode.get in " + this);
424    }
425
426    public void setNodeValue(String JavaDoc nodeValue) throws DOMException JavaDoc
427    {
428       throw new DOMException JavaDoc(DOMException.NO_DATA_ALLOWED_ERR,
429               "Cannot use TextNode.set in " + this);
430    }
431
432    /**
433     * override it in sub-classes
434     *
435     * @return
436     */

437    public short getNodeType()
438    {
439       return Node.DOCUMENT_NODE;
440    }
441
442    public Node JavaDoc getParentNode()
443    {
444       return null;
445    }
446
447    public NodeList JavaDoc getChildNodes()
448    {
449       throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR, "");
450    }
451
452    /**
453     * Do we have to count the Attributes as node ????
454     *
455     * @return
456     */

457    public Node JavaDoc getFirstChild()
458    {
459       try
460       {
461          if (soapPart != null)
462             return (org.jboss.axis.message.SOAPEnvelopeAxisImpl)soapPart
463                     .getEnvelope();
464          else
465             return null;
466       }
467       catch (SOAPException JavaDoc se)
468       {
469          throw new DOMException JavaDoc(DOMException.INVALID_STATE_ERR, "");
470       }
471
472    }
473
474    /**
475     * @return
476     */

477    public Node JavaDoc getLastChild()
478    {
479       try
480       {
481          if (soapPart != null)
482             return (org.jboss.axis.message.SOAPEnvelopeAxisImpl)soapPart
483                     .getEnvelope();
484          else
485             return null;
486       }
487       catch (SOAPException JavaDoc se)
488       {
489          throw new DOMException JavaDoc(DOMException.INVALID_STATE_ERR, "");
490       }
491
492    }
493
494    public Node JavaDoc getPreviousSibling()
495    {
496       return null;
497    }
498
499    public Node JavaDoc getNextSibling()
500    {
501
502       return null;
503    }
504
505    public NamedNodeMap JavaDoc getAttributes()
506    {
507       return null;
508    }
509
510    /**
511     * we have to have a link to them...
512     */

513    public Document JavaDoc getOwnerDocument()
514    {
515       return null;
516    }
517
518    /**
519     */

520    public Node JavaDoc insertBefore(Node JavaDoc newChild, Node JavaDoc refChild)
521            throws DOMException JavaDoc
522    {
523       throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR, "");
524    }
525
526    public Node JavaDoc replaceChild(Node JavaDoc newChild, Node JavaDoc oldChild)
527            throws DOMException JavaDoc
528    {
529       throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR, "");
530    }
531
532    public Node JavaDoc removeChild(Node JavaDoc oldChild) throws DOMException JavaDoc
533    {
534       try
535       {
536          Node JavaDoc envNode;
537          if (soapPart != null)
538          {
539             envNode = soapPart.getEnvelope();
540             if (envNode.equals(oldChild))
541             {
542                return envNode;
543             }
544          }
545          throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR, "");
546       }
547       catch (SOAPException JavaDoc se)
548       {
549          throw new DOMException JavaDoc(DOMException.INVALID_STATE_ERR, "");
550       }
551    }
552
553    public Node JavaDoc appendChild(Node JavaDoc newChild) throws DOMException JavaDoc
554    {
555       throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR, "");
556    }
557
558    public boolean hasChildNodes()
559    {
560       try
561       {
562          if (soapPart != null)
563          {
564             if (soapPart.getEnvelope() != null)
565             {
566                return true;
567             }
568          }
569          return false;
570       }
571       catch (SOAPException JavaDoc se)
572       {
573          throw new DOMException JavaDoc(DOMException.INVALID_STATE_ERR, "");
574       }
575
576    }
577
578    /**
579     * [todo] Study it more.... to implement the deep mode correctly.
580     */

581    public Node JavaDoc cloneNode(boolean deep)
582    {
583       throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR, "");
584    }
585
586    /**
587     * [todo] is it OK to simply call the superclass?
588     */

589    public void normalize()
590    {
591       throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR, "");
592    }
593
594    // TODO: fill appropriate features
595
private String JavaDoc[] features = {"foo", "bar"};
596    private String JavaDoc version = "version 2.0";
597
598    public boolean isSupported(String JavaDoc feature, String JavaDoc version)
599    {
600       if (!version.equalsIgnoreCase(version))
601          return false;
602       else
603          return true;
604    }
605
606    public String JavaDoc getPrefix()
607    {
608       throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR, "");
609    }
610
611    public void setPrefix(String JavaDoc prefix)
612    {
613       throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR, "");
614    }
615
616    public String JavaDoc getNamespaceURI()
617    {
618       throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR, "");
619    }
620
621    public void setNamespaceURI(String JavaDoc nsURI)
622    {
623       throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR, "");
624    }
625
626    public String JavaDoc getLocalName()
627    {
628       throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR, "");
629    }
630
631    public boolean hasAttributes()
632    {
633       throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR, "");
634    }
635
636    // DOM3-API start ***************************************************************************************************
637
public String JavaDoc getInputEncoding()
638    {
639       return null;
640    }
641
642    public String JavaDoc getXmlEncoding()
643    {
644       return null;
645    }
646
647    public boolean getXmlStandalone()
648    {
649       return false;
650    }
651
652    public void setXmlStandalone(boolean xmlStandalone) throws DOMException JavaDoc
653    {
654
655    }
656
657    public String JavaDoc getXmlVersion()
658    {
659       return null;
660    }
661
662    public void setXmlVersion(String JavaDoc xmlVersion) throws DOMException JavaDoc
663    {
664
665    }
666
667    public boolean getStrictErrorChecking()
668    {
669       return false;
670    }
671
672    public void setStrictErrorChecking(boolean strictErrorChecking)
673    {
674
675    }
676
677    public String JavaDoc getDocumentURI()
678    {
679       return null;
680    }
681
682    public void setDocumentURI(String JavaDoc documentURI)
683    {
684
685    }
686
687    public Node JavaDoc adoptNode(Node JavaDoc source) throws DOMException JavaDoc
688    {
689       return null;
690    }
691
692    public DOMConfiguration JavaDoc getDomConfig()
693    {
694       return null;
695    }
696
697    public void normalizeDocument()
698    {
699
700    }
701
702    public Node JavaDoc renameNode(Node JavaDoc n, String JavaDoc namespaceURI, String JavaDoc qualifiedName) throws DOMException JavaDoc
703    {
704       return null;
705    }
706    
707    public String JavaDoc getBaseURI()
708    {
709       return null;
710    }
711
712    public short compareDocumentPosition(Node JavaDoc other) throws DOMException JavaDoc
713    {
714       return 0;
715    }
716
717    public String JavaDoc getTextContent() throws DOMException JavaDoc
718    {
719       return null;
720    }
721
722    public void setTextContent(String JavaDoc textContent) throws DOMException JavaDoc
723    {
724
725    }
726
727    public boolean isSameNode(Node JavaDoc other)
728    {
729       return false;
730    }
731
732    public String JavaDoc lookupPrefix(String JavaDoc namespaceURI)
733    {
734       return null;
735    }
736
737    public boolean isDefaultNamespace(String JavaDoc namespaceURI)
738    {
739       return false;
740    }
741
742    public String JavaDoc lookupNamespaceURI(String JavaDoc prefix)
743    {
744       return null;
745    }
746
747    public boolean isEqualNode(Node JavaDoc arg)
748    {
749       return false;
750    }
751
752    public Object JavaDoc getFeature(String JavaDoc feature, String JavaDoc version)
753    {
754       return null;
755    }
756
757    public Object JavaDoc setUserData(String JavaDoc key, Object JavaDoc data, UserDataHandler JavaDoc handler)
758    {
759       return null;
760    }
761
762    public Object JavaDoc getUserData(String JavaDoc key)
763    {
764       return null;
765    }
766    // DOM3-API end *****************************************************************************************************
767
}
Popular Tags