1 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 ; 28 import org.xml.sax.ContentHandler ; 29 import org.xml.sax.SAXException ; 30 import org.xml.sax.helpers.AttributesImpl ; 31 32 33 37 public class JMSAXDriverController { 38 private static final Attributes ZERO_ATTRIBUTES = new AttributesImpl (); 39 private final JMMarshallerImpl marshaller; 40 private final DatatypeConverterInterface converter; 41 private final ContentHandler target; 42 private final NamespaceSupport nss = new NamespaceSupport(); 43 private int cnt; 44 45 47 public JMMarshallerImpl getJMMarshaller() { 48 return marshaller; 49 } 50 51 54 public ContentHandler getTarget() { 55 return target; 56 } 57 58 60 public NamespaceSupport getNamespaceContext() { 61 return nss; 62 } 63 64 66 public JMSAXDriverController(JMMarshallerImpl pMarshaller, ContentHandler pTarget) throws SAXException { 67 marshaller = pMarshaller; 68 target = pTarget; 69 converter = marshaller.getDatatypeConverter(); 70 } 71 72 75 public DatatypeConverterInterface getDatatypeConverter() { 76 return converter; 77 } 78 79 protected String getNewPrefix(String pURI, String 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 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 getPreferredPrefix(JMSAXDriver pDriver, String pURI) { 98 if (pDriver != null) { 99 String 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 getElementQName(JMSAXDriver pDriver, String pPrefix, 112 String pNamespaceURI, String pLocalName) throws SAXException { 113 if (pNamespaceURI == null) { 114 pNamespaceURI = ""; 115 } 116 String 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 getElementQName(JMSAXDriver pDriver, String pNamespaceURI, 134 String pLocalName) 135 throws SAXException { 136 if (pNamespaceURI == null) { 137 pNamespaceURI = ""; 138 } 139 String 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 155 public String getAttrQName(JMSAXDriver pDriver, String pNamespaceURI, 156 String pLocalName) throws SAXException { 157 if (pNamespaceURI == null) { 158 pNamespaceURI = ""; 159 } 160 String 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 pAttrs) throws SAXException { 177 JMMarshallerImpl m = getJMMarshaller(); 178 String schemaLocation = m.getSchemaLocation(); 179 String 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 qName = getAttrQName(pDriver, XMLConstants.XML_SCHEMA_URI, schemaLocationAttribute); 192 pAttrs.addAttribute(XMLConstants.XML_SCHEMA_URI, schemaLocationAttribute, 193 qName, "CDATA", schemaLocation); 194 } 195 } 196 197 203 public void marshal(JMSAXDriver pDriver, String pPrefix, 204 String pNamespaceURI, String pLocalName, 205 Object pElement) throws SAXException { 206 int context = nss.getContext(); 207 String qName = getElementQName(pDriver, pPrefix, pNamespaceURI, pLocalName); 208 AttributesImpl attrs = pDriver.getAttributes(this, pElement); 209 addSchemaLocationAttributes(pDriver, attrs); 210 ContentHandler 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 223 public void marshal(JMSAXDriver pDriver, String pNamespaceURI, 224 String pLocalName, Object pElement) throws SAXException { 225 int context = nss.getContext(); 226 String qName = getElementQName(pDriver, pNamespaceURI, pLocalName); 227 AttributesImpl attrs = pDriver.getAttributes(this, pElement); 228 ContentHandler 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 237 public void marshalSimpleChild(JMSAXDriver pDriver, String pNamespaceURI, 238 String pLocalName, String pValue) 239 throws SAXException { 240 int context = nss.getContext(); 241 String qName = getElementQName(pDriver, pNamespaceURI, pLocalName); 242 ContentHandler 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 { 252 NamespaceSupport nss = getNamespaceContext(); 253 ContentHandler h = getTarget(); 254 for (;;) { 255 String prefix = nss.checkContext(pContext); 256 if (prefix == null) { 257 return; 258 } 259 h.endPrefixMapping(prefix); 260 } 261 } 262 263 public void printConversionEvent(Object pObject, String pMsg, Exception pException) throws SAXException { 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 (pMsg, pException); 273 } 274 } 275 | Popular Tags |