1 29 30 package com.caucho.jaxb.skeleton; 31 32 import java.io.IOException ; 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 ; 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 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 obj, QName qname) 55 throws IOException , XMLStreamException, JAXBException 56 { 57 writeQNameStartElement(out, qname); 58 59 if (obj != null) { 60 QName name = (QName ) obj; 61 62 String namespace = name.getNamespaceURI(); 63 String prefix = name.getPrefix(); 64 65 if (namespace != null && ! "".equals(namespace)) { 67 String declaredPrefix = out.getPrefix(namespace); 68 69 72 if (declaredPrefix == null) { 73 if ("".equals(prefix)) { 74 if ("n".equals(qname.getPrefix())) 78 prefix = "d"; 79 else 80 prefix = "n"; 81 82 out.writeNamespace(prefix, namespace); 83 } 84 else { 85 out.writeNamespace(prefix, namespace); 87 } 88 } 89 else if ("".equals(declaredPrefix)) { 90 if (! "".equals(prefix)) { 91 out.writeNamespace(prefix, namespace); 93 } 94 } 96 else { 97 if ("".equals(prefix)) { 98 prefix = declaredPrefix; 100 } 101 else if (! prefix.equals(declaredPrefix)) { 102 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 public Object read(Unmarshaller u, XMLStreamReader in, QName qname) 119 throws IOException , XMLStreamException, JAXBException 120 { 121 if (in.getEventType() != in.START_ELEMENT || ! in.getName().equals(qname)) 122 throw new IOException (L.l("Expected <{0}>, not <{1}>", 123 qname.getLocalPart(), in.getLocalName())); 124 125 in.next(); 126 127 Object ret = null; 128 129 if (in.getEventType() == in.CHARACTERS) { 130 String text = in.getText(); 131 int colon = text.indexOf(':'); 132 133 if (colon < 0) 134 ret = new QName (text); 135 else { 136 String prefix = text.substring(0, colon); 137 138 String namespace = in.getNamespaceURI(prefix); 139 140 if (namespace == null) 141 throw new IOException (L.l("No known namespace for prefix {0}", 142 prefix)); 143 144 String localName = text.substring(colon + 1); 145 146 if ("".equals(localName)) 147 throw new IOException (L.l("Malformed QName: empty localName")); 148 149 ret = new QName (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 (L.l("Expected </{0}>, not </{1}>", 158 qname.getLocalPart(), in.getLocalName())); 159 160 in.next(); 161 162 return ret; 163 } 164 165 public String getSchemaType() 166 { 167 return "xsd:QName"; 168 } 169 } 170 | Popular Tags |