示例#1
0
  @Test
  public void testAddOrSetAttributeAttributesImplNode() throws ParserConfigurationException {
    final AttributesImpl atts = new AttributesImpl();
    final DOMImplementation dom =
        DocumentBuilderFactory.newInstance().newDocumentBuilder().getDOMImplementation();
    final Document doc = dom.createDocument(null, "foo", null);

    doc.getDocumentElement().setAttribute("foo", "foo");
    final Attr att = (Attr) doc.getDocumentElement().getAttributeNode("foo");
    XMLUtils.addOrSetAttribute(atts, att);

    final int i = atts.getIndex(NULL_NS_URI, "foo");
    assertEquals(NULL_NS_URI, atts.getURI(i));
    assertEquals("foo", atts.getQName(i));
    assertEquals("foo", atts.getLocalName(i));
    assertEquals("foo", atts.getValue(i));

    doc.getDocumentElement().setAttributeNS(XML_NS_URI, "xml:lang", "en");
    final Attr lang = (Attr) doc.getDocumentElement().getAttributeNodeNS(XML_NS_URI, "lang");
    XMLUtils.addOrSetAttribute(atts, lang);

    final int l = atts.getIndex(XML_NS_URI, "lang");
    assertEquals(XML_NS_URI, atts.getURI(l));
    assertEquals("xml:lang", atts.getQName(l));
    assertEquals("lang", atts.getLocalName(l));
    assertEquals("en", atts.getValue(l));
  }
    @Override
    public void startElement(String uri, String localName, String qName, Attributes origAttrs)
        throws SAXException {
      // If we have an image tag, re-write the src attribute
      //  if required
      if ("img".equals(localName)) {
        AttributesImpl attrs;
        if (origAttrs instanceof AttributesImpl) {
          attrs = (AttributesImpl) origAttrs;
        } else {
          attrs = new AttributesImpl(origAttrs);
        }

        for (int i = 0; i < attrs.getLength(); i++) {
          if ("src".equals(attrs.getLocalName(i))) {
            String src = attrs.getValue(i);
            if (src.startsWith("embedded:")) {
              String newSrc = "";
              if (imageFolder != null) newSrc += imageFolder + "/";
              if (imagePrefix != null) newSrc += imagePrefix;
              newSrc += src.substring(src.indexOf(':') + 1);
              attrs.setValue(i, newSrc);
            }
          }
        }
        super.startElement(uri, localName, qName, attrs);
      } else {
        // For any other tag, pass through as-is
        super.startElement(uri, localName, qName, origAttrs);
      }
    }