KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jaxb > skeleton > QNameProperty


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Emil Ong
28  */

29
30 package com.caucho.jaxb.skeleton;
31
32 import java.io.IOException JavaDoc;
33
34 import javax.xml.bind.JAXBException;
35 import javax.xml.bind.Marshaller;
36 import javax.xml.bind.Unmarshaller;
37 import javax.xml.namespace.QName JavaDoc;
38 import javax.xml.stream.XMLStreamReader;
39 import javax.xml.stream.XMLStreamWriter;
40 import javax.xml.stream.XMLStreamException;
41
42 import com.caucho.util.L10N;
43
44 /**
45  * a qname property
46  */

47 public class QNameProperty extends Property {
48   private static final L10N L = new L10N(QNameProperty.class);
49
50   public static final QNameProperty PROPERTY = new QNameProperty();
51
52   private int _nsCounter = 0;
53
54   public void write(Marshaller m, XMLStreamWriter out, Object JavaDoc obj, QName JavaDoc qname)
55     throws IOException JavaDoc, XMLStreamException, JAXBException
56   {
57     writeQNameStartElement(out, qname);
58
59     if (obj != null) {
60       QName JavaDoc name = (QName JavaDoc) obj;
61
62       String JavaDoc namespace = name.getNamespaceURI();
63       String JavaDoc prefix = name.getPrefix();
64
65       // check if we need to declare this namespace prefix
66
if (namespace != null && ! "".equals(namespace)) {
67         String JavaDoc declaredPrefix = out.getPrefix(namespace);
68         
69         // 6 cases: the given prefix can be "" or not ""
70
// and the declared prefix can be null, default (""), or not ""
71

72         if (declaredPrefix == null) {
73           if ("".equals(prefix)) {
74             // use a dummy prefix... can use "n" all the time since we enter
75
// and leave the element without any children... unless the name
76
// of the element itself has a namespace with prefix "n"
77
if ("n".equals(qname.getPrefix()))
78               prefix = "d";
79             else
80               prefix = "n";
81
82             out.writeNamespace(prefix, namespace);
83           }
84           else {
85             // just write the given prefix
86
out.writeNamespace(prefix, namespace);
87           }
88         }
89         else if ("".equals(declaredPrefix)) {
90           if (! "".equals(prefix)) {
91             // need to declare this prefix
92
out.writeNamespace(prefix, namespace);
93           }
94           // else if prefix == "" or prefix == null, do nothing
95
}
96         else {
97           if ("".equals(prefix)) {
98             // take on existing prefix
99
prefix = declaredPrefix;
100           }
101           else if (! prefix.equals(declaredPrefix)) {
102             // the given prefix doesn't match the existing one, so declare it
103
out.writeNamespace(prefix, namespace);
104           }
105         }
106       }
107
108       if (prefix == null || "".equals(prefix))
109         out.writeCharacters(name.getLocalPart());
110       else
111         out.writeCharacters(prefix + ":" + name.getLocalPart());
112     }
113
114     writeQNameEndElement(out, qname);
115   }
116
117   // Can't use CDataProperty because we need access to the namespace map
118
public Object JavaDoc read(Unmarshaller u, XMLStreamReader in, QName JavaDoc qname)
119     throws IOException JavaDoc, XMLStreamException, JAXBException
120   {
121     if (in.getEventType() != in.START_ELEMENT || ! in.getName().equals(qname))
122       throw new IOException JavaDoc(L.l("Expected <{0}>, not <{1}>",
123                                 qname.getLocalPart(), in.getLocalName()));
124
125     in.next();
126  
127     Object JavaDoc ret = null;
128
129     if (in.getEventType() == in.CHARACTERS) {
130       String JavaDoc text = in.getText();
131       int colon = text.indexOf(':');
132
133       if (colon < 0)
134         ret = new QName JavaDoc(text);
135       else {
136         String JavaDoc prefix = text.substring(0, colon);
137
138         String JavaDoc namespace = in.getNamespaceURI(prefix);
139
140         if (namespace == null)
141           throw new IOException JavaDoc(L.l("No known namespace for prefix {0}",
142                                     prefix));
143
144         String JavaDoc localName = text.substring(colon + 1);
145
146         if ("".equals(localName))
147           throw new IOException JavaDoc(L.l("Malformed QName: empty localName"));
148
149         ret = new QName JavaDoc(namespace, localName, prefix);
150       }
151     }
152
153     while(in.getEventType() != in.END_ELEMENT)
154       in.next();
155
156     if (! in.getName().equals(qname))
157       throw new IOException JavaDoc(L.l("Expected </{0}>, not </{1}>",
158                                 qname.getLocalPart(), in.getLocalName()));
159
160     in.next();
161
162     return ret;
163   }
164
165   public String JavaDoc getSchemaType()
166   {
167     return "xsd:QName";
168   }
169 }
170
Popular Tags