KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > soap > AttributeHandler


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2000 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "SOAP" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation and was
52  * originally based on software copyright (c) 2000, International
53  * Business Machines, Inc., http://www.apache.org. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57
58 package org.apache.soap;
59
60 import java.io.*;
61 import java.util.*;
62 import org.w3c.dom.*;
63 import org.apache.soap.util.xml.*;
64 import org.apache.soap.rpc.SOAPContext;
65
66 /**
67  * An <code>AttributeHandler</code> maintains attributes and namespace
68  * declarations for the other <em>SOAP</em> classes.
69  *
70  * @author Matthew J. Duftler (duftler@us.ibm.com)
71  */

72 class AttributeHandler
73 {
74   private Hashtable attributes = new Hashtable();
75   private Hashtable namespaceURIs2Prefixes = new Hashtable();
76   private int nsPrefixIndex = 0;
77
78   public AttributeHandler()
79   {
80     // Make sure to use "xmlns" as the prefix for any namespace declarations.
81
namespaceURIs2Prefixes.put(Constants.NS_URI_XMLNS,
82                                Constants.NS_PRE_XMLNS);
83   }
84
85   public void setAttribute(QName attrQName, String JavaDoc value)
86   {
87     attributes.put(attrQName, value);
88
89     // If this is a namespace declaration, register the new prefix.
90
if (attrQName.getNamespaceURI().equals(Constants.NS_URI_XMLNS))
91     {
92       namespaceURIs2Prefixes.put(value, attrQName.getLocalPart());
93     }
94   }
95
96   public String JavaDoc getAttribute(QName attrQName)
97   {
98     return (String JavaDoc)attributes.get(attrQName);
99   }
100
101   public void removeAttribute(QName attrQName)
102   {
103     attributes.remove(attrQName);
104   }
105
106   private Enumeration getAttributeQNames()
107   {
108     generateNSDeclarations();
109
110     return attributes.keys();
111   }
112
113   public void declareNamespace(String JavaDoc nsPrefix, String JavaDoc namespaceURI)
114   {
115     setAttribute(new QName(Constants.NS_URI_XMLNS, nsPrefix), namespaceURI);
116   }
117
118   private void generateNSDeclarations()
119   {
120     Enumeration keys = attributes.keys();
121
122     while (keys.hasMoreElements())
123     {
124       QName qname = (QName)keys.nextElement();
125
126       // Ensure that a prefix has been associated with this namespace URI.
127
getPrefixFromURI(qname.getNamespaceURI());
128     }
129   }
130
131   private String JavaDoc getPrefixFromURI(String JavaDoc namespaceURI)
132   {
133     if ("".equals(namespaceURI)) return null ;
134
135     String JavaDoc nsPrefix = (String JavaDoc)namespaceURIs2Prefixes.get(namespaceURI);
136
137     if (nsPrefix == null)
138     {
139       nsPrefix = "ns" + nsPrefixIndex++;
140
141       setAttribute(new QName(Constants.NS_URI_XMLNS, nsPrefix), namespaceURI);
142     }
143
144     return nsPrefix;
145   }
146
147   public void populateNSStack(NSStack nsStack)
148   {
149     generateNSDeclarations();
150
151     nsStack.pushScope();
152
153     Enumeration e = namespaceURIs2Prefixes.keys();
154
155     while (e.hasMoreElements())
156     {
157       String JavaDoc namespaceURI = (String JavaDoc)e.nextElement();
158       String JavaDoc namespacePrefix = getPrefixFromURI(namespaceURI);
159
160       if (namespacePrefix != null)
161         nsStack.addNSDeclaration(namespacePrefix, namespaceURI);
162     }
163   }
164
165   public String JavaDoc getUniquePrefixFromURI(String JavaDoc namespaceURI,
166                                        String JavaDoc preferredPrefix,
167                                        NSStack nsStack)
168   {
169     String JavaDoc retPrefix = nsStack.getPrefixFromURI(namespaceURI);
170
171     if (retPrefix == null)
172     {
173       int prefixCount = 0;
174
175       if (preferredPrefix == null)
176       {
177         preferredPrefix = "ns";
178         prefixCount++;
179       }
180
181       while (retPrefix == null)
182       {
183         String JavaDoc newPrefix = preferredPrefix + (prefixCount > 0
184                                               ? prefixCount + ""
185                                               : "");
186
187         // Is this prefix free?
188
if (nsStack.getURIFromPrefix(newPrefix) == null)
189         {
190           // Have to declare the namespace, and update the namespace stack.
191
nsStack.popScope();
192           declareNamespace(newPrefix, namespaceURI);
193           populateNSStack(nsStack);
194           retPrefix = nsStack.getPrefixFromURI(namespaceURI);
195         }
196         else
197         {
198           prefixCount++;
199         }
200       }
201     }
202
203     return retPrefix;
204   }
205
206   public void marshall(Writer sink, SOAPContext ctx)
207       throws IllegalArgumentException JavaDoc, IOException
208   {
209     Enumeration attrQNames = getAttributeQNames();
210
211     while (attrQNames.hasMoreElements())
212     {
213       QName attrQName = (QName)attrQNames.nextElement();
214
215       sink.write(' ') ;
216       String JavaDoc nsPrefix ;
217       if ((nsPrefix = getPrefixFromURI(attrQName.getNamespaceURI())) != null)
218         sink.write(nsPrefix + ':') ;
219       sink.write(attrQName.getLocalPart() + "=\"" +
220                  getAttribute(attrQName) + '\"');
221     }
222   }
223
224   public static AttributeHandler unmarshall(Node src, SOAPContext ctx)
225     throws IllegalArgumentException JavaDoc
226   {
227     NamedNodeMap attrs = src.getAttributes();
228     AttributeHandler attrHandler = new AttributeHandler();
229     int size = attrs.getLength();
230
231     for (int i = 0; i < size; i++)
232     {
233       Attr attr = (Attr)attrs.item(i);
234       String JavaDoc namespaceURI = attr.getNamespaceURI();
235       String JavaDoc localName = attr.getLocalName();
236       String JavaDoc value = attr.getValue();
237
238       attrHandler.setAttribute(new QName(namespaceURI, localName), value);
239     }
240
241     return attrHandler;
242   }
243
244   public String JavaDoc toString()
245   {
246     StringWriter sw = new StringWriter();
247
248     try
249     {
250       sw.write("{");
251       marshall(sw, new SOAPContext());
252       sw.write("}");
253     }
254     catch (Exception JavaDoc e)
255     {
256     }
257
258     return sw.toString();
259   }
260 }
261
Popular Tags