KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ws > jaxme > impl > JMSAXDriverController


1 /*
2  * Copyright 2005 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 package org.apache.ws.jaxme.impl;
17
18 import javax.xml.bind.DatatypeConverterInterface;
19 import javax.xml.bind.ValidationEvent;
20 import javax.xml.bind.ValidationEventHandler;
21 import javax.xml.bind.ValidationEventLocator;
22 import javax.xml.bind.helpers.PrintConversionEventImpl;
23 import javax.xml.bind.helpers.ValidationEventLocatorImpl;
24
25 import org.apache.ws.jaxme.XMLConstants;
26 import org.apache.ws.jaxme.util.NamespaceSupport;
27 import org.xml.sax.Attributes JavaDoc;
28 import org.xml.sax.ContentHandler JavaDoc;
29 import org.xml.sax.SAXException JavaDoc;
30 import org.xml.sax.helpers.AttributesImpl JavaDoc;
31
32
33 /** The controller is created by the
34  * {@link org.apache.ws.jaxme.JMMarshaller} for
35  * marshalling a given element.
36  */

37 public class JMSAXDriverController {
38     private static final Attributes JavaDoc ZERO_ATTRIBUTES = new AttributesImpl JavaDoc();
39     private final JMMarshallerImpl marshaller;
40     private final DatatypeConverterInterface converter;
41     private final ContentHandler JavaDoc target;
42     private final NamespaceSupport nss = new NamespaceSupport();
43     private int cnt;
44
45     /** Returns the Marshaller, which created the controller.
46      */

47     public JMMarshallerImpl getJMMarshaller() {
48         return marshaller;
49     }
50
51     /** Returns the target handler, to which SAX events are
52      * being fired.
53      */

54     public ContentHandler JavaDoc getTarget() {
55         return target;
56     }
57
58     /** Returns an instance of NamespaceSupport.
59      */

60     public NamespaceSupport getNamespaceContext() {
61         return nss;
62     }
63
64     /** Creates a new instance with the given marshaller and target.
65      */

66     public JMSAXDriverController(JMMarshallerImpl pMarshaller, ContentHandler JavaDoc pTarget) throws SAXException JavaDoc {
67         marshaller = pMarshaller;
68         target = pTarget;
69         converter = marshaller.getDatatypeConverter();
70     }
71
72     /** Returns the {@link DatatypeConverterInterface} being used for
73      * conversion of atomic values.
74      */

75     public DatatypeConverterInterface getDatatypeConverter() {
76         return converter;
77     }
78
79     protected String JavaDoc getNewPrefix(String JavaDoc pURI, String JavaDoc pSuggestedPrefix) {
80         if (pSuggestedPrefix == null || pSuggestedPrefix.length() == 0) {
81             if (!nss.isPrefixDeclared("")) {
82                 nss.declarePrefix("", pURI);
83                 return "";
84             }
85             pSuggestedPrefix = "p";
86         }
87         String JavaDoc pc = pSuggestedPrefix;
88         for (;;) {
89             if (!nss.isPrefixDeclared(pc)) {
90                 nss.declarePrefix(pc, pURI);
91                 return pc;
92             }
93             pc = pSuggestedPrefix + ++cnt;
94         }
95     }
96
97     protected String JavaDoc getPreferredPrefix(JMSAXDriver pDriver, String JavaDoc pURI) {
98         if (pDriver != null) {
99             String JavaDoc prefix = pDriver.getPreferredPrefix(pURI);
100             if (prefix != null) {
101                 return prefix;
102             }
103         }
104         if (XMLConstants.XML_SCHEMA_URI.equals(pURI)) {
105             return "xsi";
106         } else {
107             return null;
108         }
109     }
110
111     protected String JavaDoc getElementQName(JMSAXDriver pDriver, String JavaDoc pPrefix,
112                                     String JavaDoc pNamespaceURI, String JavaDoc pLocalName) throws SAXException JavaDoc {
113         if (pNamespaceURI == null) {
114             pNamespaceURI = "";
115         }
116         String JavaDoc prefix = nss.getPrefix(pNamespaceURI);
117         if (prefix == null) {
118             if (pPrefix != null) {
119                 prefix = pPrefix;
120             } else {
121                 prefix = getNewPrefix(pNamespaceURI, getPreferredPrefix(pDriver, pNamespaceURI));
122             }
123             nss.declarePrefix(prefix, pNamespaceURI);
124             getTarget().startPrefixMapping(prefix, pNamespaceURI);
125         }
126         if (prefix == null || "".equals(prefix)) {
127             return pLocalName;
128         } else {
129             return prefix + ":" + pLocalName;
130         }
131     }
132
133     protected String JavaDoc getElementQName(JMSAXDriver pDriver, String JavaDoc pNamespaceURI,
134                                      String JavaDoc pLocalName)
135             throws SAXException JavaDoc {
136         if (pNamespaceURI == null) {
137             pNamespaceURI = "";
138         }
139         String JavaDoc prefix = nss.getPrefix(pNamespaceURI);
140         if (prefix == null) {
141             prefix = getNewPrefix(pNamespaceURI, getPreferredPrefix(pDriver, pNamespaceURI));
142             nss.declarePrefix(prefix, pNamespaceURI);
143             getTarget().startPrefixMapping(prefix, pNamespaceURI);
144         }
145         if (prefix == null || "".equals(prefix)) {
146             return pLocalName;
147         } else {
148             return prefix + ":" + pLocalName;
149         }
150     }
151
152     /** Returns the qualified name of the attribute <code>pLocalName</code>.
153      * In other words, attachs a prefix, if required.
154      */

155     public String JavaDoc getAttrQName(JMSAXDriver pDriver, String JavaDoc pNamespaceURI,
156                                String JavaDoc pLocalName) throws SAXException JavaDoc {
157         if (pNamespaceURI == null) {
158             pNamespaceURI = "";
159         }
160         String JavaDoc prefix = nss.getAttributePrefix(pNamespaceURI);
161         if (prefix == null) {
162             prefix = getPreferredPrefix(pDriver, pNamespaceURI);
163             if (prefix == null) {
164                 prefix = getNewPrefix(pNamespaceURI, "p");
165             }
166             getTarget().startPrefixMapping(prefix, pNamespaceURI);
167         }
168         if (prefix == null || "".equals(prefix)) {
169             return pLocalName;
170         } else {
171             return prefix + ":" + pLocalName;
172         }
173     }
174
175     protected void addSchemaLocationAttributes(JMSAXDriver pDriver,
176                                                AttributesImpl JavaDoc pAttrs) throws SAXException JavaDoc {
177         JMMarshallerImpl m = getJMMarshaller();
178         String JavaDoc schemaLocation = m.getSchemaLocation();
179         String JavaDoc schemaLocationAttribute;
180         if (schemaLocation != null) {
181             schemaLocationAttribute = XMLConstants.XML_SCHEMA_NS_ATTR;
182         } else {
183             schemaLocation = m.getNoNamespaceSchemaLocation();
184             if (schemaLocation != null) {
185                 schemaLocationAttribute = XMLConstants.XML_SCHEMA_NO_NS_ATTR;
186             } else {
187                 schemaLocationAttribute = null;
188             }
189         }
190         if (schemaLocation != null) {
191             String JavaDoc qName = getAttrQName(pDriver, XMLConstants.XML_SCHEMA_URI, schemaLocationAttribute);
192             pAttrs.addAttribute(XMLConstants.XML_SCHEMA_URI, schemaLocationAttribute,
193                                 qName, "CDATA", schemaLocation);
194         }
195     }
196
197     /** Marshals the given object, creating a root element with
198      * the given namespace URI and local name.
199      * @param pElement The element being marshalled. It must be
200      * an instance of the class associated to this specific
201      * JMXmlSerializer.
202      */

203     public void marshal(JMSAXDriver pDriver, String JavaDoc pPrefix,
204                         String JavaDoc pNamespaceURI, String JavaDoc pLocalName,
205                         Object JavaDoc pElement) throws SAXException JavaDoc {
206         int context = nss.getContext();
207         String JavaDoc qName = getElementQName(pDriver, pPrefix, pNamespaceURI, pLocalName);
208         AttributesImpl JavaDoc attrs = pDriver.getAttributes(this, pElement);
209         addSchemaLocationAttributes(pDriver, attrs);
210         ContentHandler JavaDoc h = getTarget();
211         h.startElement(pNamespaceURI, pLocalName, qName, attrs);
212         pDriver.marshalChilds(this, h, pElement);
213         h.endElement(pNamespaceURI, pLocalName, qName);
214         restoreContext(context);
215     }
216     
217     /** Marshals the given object, creating an element with
218      * the given namespace URI and local name.
219      * @param pElement The element being marshalled. It must be
220      * an instance of the class associated to this specific
221      * JMXmlSerializer.
222      */

223     public void marshal(JMSAXDriver pDriver, String JavaDoc pNamespaceURI,
224                         String JavaDoc pLocalName, Object JavaDoc pElement) throws SAXException JavaDoc {
225         int context = nss.getContext();
226         String JavaDoc qName = getElementQName(pDriver, pNamespaceURI, pLocalName);
227         AttributesImpl JavaDoc attrs = pDriver.getAttributes(this, pElement);
228         ContentHandler JavaDoc h = getTarget();
229         h.startElement(pNamespaceURI, pLocalName, qName, attrs);
230         pDriver.marshalChilds(this, h, pElement);
231         h.endElement(pNamespaceURI, pLocalName, qName);
232         restoreContext(context);
233     }
234
235     /** Called by the driver for creating a simple child.
236      */

237     public void marshalSimpleChild(JMSAXDriver pDriver, String JavaDoc pNamespaceURI,
238                                    String JavaDoc pLocalName, String JavaDoc pValue)
239             throws SAXException JavaDoc {
240         int context = nss.getContext();
241         String JavaDoc qName = getElementQName(pDriver, pNamespaceURI, pLocalName);
242         ContentHandler JavaDoc h = getTarget();
243         h.startElement(pNamespaceURI, pLocalName, qName, ZERO_ATTRIBUTES);
244         if (pValue != null && pValue.length() > 0) {
245             h.characters(pValue.toCharArray(), 0, pValue.length());
246         }
247         h.endElement(pNamespaceURI, pLocalName, qName);
248         restoreContext(context);
249     }
250
251     private void restoreContext(int pContext) throws SAXException JavaDoc {
252         NamespaceSupport nss = getNamespaceContext();
253         ContentHandler JavaDoc h = getTarget();
254         for (;;) {
255             String JavaDoc prefix = nss.checkContext(pContext);
256             if (prefix == null) {
257                 return;
258             }
259             h.endPrefixMapping(prefix);
260         }
261     }
262
263     public void printConversionEvent(Object JavaDoc pObject, String JavaDoc pMsg, Exception JavaDoc pException) throws SAXException JavaDoc {
264         ValidationEventHandler handler = getJMMarshaller().getEventHandler();
265         if (handler != null) {
266             ValidationEventLocator locator = new ValidationEventLocatorImpl(pObject);
267             PrintConversionEventImpl event = new PrintConversionEventImpl(ValidationEvent.FATAL_ERROR, pMsg, locator);
268             if (handler.handleEvent(event)) {
269                 return;
270             }
271         }
272         throw new SAXException JavaDoc(pMsg, pException);
273     }
274 }
275
Popular Tags