/** * Build the SAX attributes object based upon Java's String map. This convenience method will * build, or add to an existing attributes object, the attributes detailed in the AttributeMap. * * @param namespaces SAX Helper class to keep track of namespaces able to determine the correct * prefix for a given namespace URI. * @param attributes An existing SAX AttributesImpl object to add attributes too. If the value is * null then a new attributes object will be created to house the attributes. * @param attributeMap A map of attributes and values. * @return */ private AttributesImpl map2sax( Namespace elementNamespace, NamespaceSupport namespaces, AttributesImpl attributes, AttributeMap attributeMap) { if (attributes == null) attributes = new AttributesImpl(); if (attributeMap != null) { // Figure out the namespace issue Namespace namespace = attributeMap.getNamespace(); String URI; if (namespace != null) URI = namespace.URI; else URI = WingConstants.DRI.URI; String prefix = namespaces.getPrefix(URI); // copy each one over. for (String name : attributeMap.keySet()) { String value = attributeMap.get(name); if (value == null) continue; // If the indended namespace is the element's namespace then we // leave // off the namespace declaration because w3c say's its redundent // and breaks lots of xsl stuff. if (elementNamespace.URI.equals(URI)) attributes.addAttribute("", name, name, "CDATA", value); else attributes.addAttribute(URI, name, qName(prefix, name), "CDATA", value); } } return attributes; }
/** * Create the qName for the element with the given localName and namespace prefix. * * @param namespace (May be null) The namespace prefix. * @param localName (Required) The element's local name. * @return */ private String qName(Namespace namespace, String localName) { String prefix = namespaces.getPrefix(namespace.URI); if (prefix == null || prefix.equals("")) { return localName; } else { return prefix + ":" + localName; } }
protected void start(Element element, XSDElementDeclaration declaration) throws SAXException { String uri = element.getNamespaceURI(); String local = element.getLocalName(); String qName = element.getLocalName(); NamespaceSupport namespaces = this.namespaces; // declaration == null -> gml3 envelope encoding test failing // declaration.getSchema() == null -> wfs 2.0 feature collection encoding test failing if (namespaceAware && (declaration == null || declaration.isGlobal() || declaration.getSchema() == null || declaration.getSchema().getElementFormDefault() == XSDForm.QUALIFIED_LITERAL)) { uri = (uri != null) ? uri : namespaces.getURI(""); qName = namespaces.getPrefix(uri) + ":" + qName; } else { uri = ""; } DOMAttributes atts = new DOMAttributes(element.getAttributes(), namespaces); serializer.startElement(uri, local, qName, atts); // write out any text for (int i = 0; i < element.getChildNodes().getLength(); i++) { Node node = (Node) element.getChildNodes().item(i); if (node instanceof Text) { char[] ch = ((Text) node).getData().toCharArray(); serializer.characters(ch, 0, ch.length); } } // write out any child elements for (int i = 0; i < element.getChildNodes().getLength(); i++) { Node node = (Node) element.getChildNodes().item(i); if (node instanceof Element) { Element child = (Element) node; start( child, Schemas.getChildElementDeclaration( declaration, new QName(child.getNamespaceURI(), child.getNodeName()))); end(child); } } // push a new context for children, declaring the default prefix to be the one of this // element this.namespaces.pushContext(); if (uri != null) { this.namespaces.declarePrefix("", uri); } }
/** * Send the SAX events to start this element. * * @param contentHandler (Required) The registered contentHandler where SAX events should be * routed too. * @param namespaces (Required) SAX Helper class to keep track of namespaces able to determine the * correct prefix for a given namespace URI. * @param namespace (Required) The namespace of this element. * @param name (Required) The local name of this element. * @param attributes (May be null) Attributes for this element */ protected void startElement( ContentHandler contentHandler, NamespaceSupport namespaces, Namespace namespace, String name, AttributeMap attributes) throws SAXException { String prefix = namespaces.getPrefix(namespace.URI); contentHandler.startElement( namespace.URI, name, qName(prefix, name), map2sax(namespace, namespaces, attributes)); }
public String getQName(int index) { Node n = atts.item(index); if (namespaces != null) { String uri = n.getNamespaceURI(); String prefix = (uri != null) ? namespaces.getPrefix(uri) : null; if (prefix != null) { return prefix + ":" + n.getLocalName(); } } return n.getLocalName(); }
/** * End of an element. * * @param uri namespace of the element. * @param localName local name of the element. * @param qName qualified name of the element. * @throws SAXException passed through. */ @Override public void endElement(String uri, String localName, String qName) throws SAXException { if (allowElements) { // check if we are past the minimum level requirements if (minimumElementLevel < currentElementLevel) { if (defaultURI != null && (uri == null || "".equals(uri))) { // No namespace provided, use the default namespace. String prefix = namespaces.getPrefix(defaultURI); if (!(prefix == null || "".equals(prefix))) { qName = prefix + ":" + localName; } contentHandler.endElement(defaultURI, localName, qName); } else { // Let the event pass through unmodified. contentHandler.endElement(uri, localName, localName); } } currentElementLevel--; } }
/** * Send the SAX events to end this element. * * @param contentHandler (Required) The registered contentHandler where SAX events should be * routed too. * @param namespaces (Required) SAX Helper class to keep track of namespaces able to determine the * correct prefix for a given namespace URI. * @param namespace (Required) The namespace of this element. * @param name (Required) The local name of this element. */ protected void endElement( ContentHandler contentHandler, NamespaceSupport namespaces, Namespace namespace, String name) throws SAXException { String prefix = namespaces.getPrefix(namespace.URI); contentHandler.endElement(namespace.URI, name, qName(prefix, name)); }