Example usage for org.xml.sax.helpers AttributesImpl AttributesImpl

List of usage examples for org.xml.sax.helpers AttributesImpl AttributesImpl

Introduction

In this page you can find the example usage for org.xml.sax.helpers AttributesImpl AttributesImpl.

Prototype

public AttributesImpl() 

Source Link

Document

Construct a new, empty AttributesImpl object.

Usage

From source file:mj.ocraptor.extraction.tika.parser.pkg.PackageParser.java

private void parseEntry(ArchiveInputStream archive, ArchiveEntry entry, EmbeddedDocumentExtractor extractor,
        XHTMLContentHandler xhtml) throws SAXException, IOException, TikaException {
    String name = entry.getName();
    if (archive.canReadEntryData(entry)) {
        Metadata entrydata = new Metadata();
        if (name != null && name.length() > 0) {
            entrydata.set(Metadata.RESOURCE_NAME_KEY, name);
            AttributesImpl attributes = new AttributesImpl();
            attributes.addAttribute("", "class", "class", "CDATA", "embedded");
            attributes.addAttribute("", "id", "id", "CDATA", name);
            xhtml.startElement("div", attributes);
            xhtml.endElement("div");

            entrydata.set(Metadata.EMBEDDED_RELATIONSHIP_ID, name);
        }//from  w w  w .j  a v a  2s.c  o  m
        if (extractor.shouldParseEmbedded(entrydata)) {
            // For detectors to work, we need a mark/reset supporting
            // InputStream, which ArchiveInputStream isn't, so wrap
            TemporaryResources tmp = new TemporaryResources();
            try {
                TikaInputStream tis = TikaInputStream.get(archive, tmp);
                extractor.parseEmbedded(tis, xhtml, entrydata, true);
            } finally {
                tmp.dispose();
            }
        }
    } else if (name != null && name.length() > 0) {
        xhtml.element("p", name);
    }
}

From source file:com.adobe.acs.commons.rewriter.impl.VersionedClientlibsTransformerFactoryTest.java

@Test
public void testNoop() throws Exception {

    when(htmlLibraryManager.getLibrary(eq(LibraryType.JS), eq(PATH))).thenReturn(htmlLibrary);

    final AttributesImpl in = new AttributesImpl();
    in.addAttribute("", "href", "", "CDATA", PATH + ".css");

    transformer.startElement(null, "a", null, in);

    ArgumentCaptor<Attributes> attributesCaptor = ArgumentCaptor.forClass(Attributes.class);

    verify(handler, only()).startElement(isNull(String.class), eq("a"), isNull(String.class),
            attributesCaptor.capture());

    assertEquals(PATH + ".css", attributesCaptor.getValue().getValue(0));
}

From source file:com.google.enterprise.connector.sharepoint.wsclient.mock.MockClientFactory.java

/**
 * Creates a new {@link ListState}./*from  w  w  w .j a v a  2  s .c  om*/
 *
 * @param webUrl The URL of the parent web
 * @param listName The name of the new list
 * @param ws The web state of the parent web
 * @param feedType The feed type of the new list
 * @param fixedId If true created an ID from the list URL; otherwise
 *          a new random ID is created
 * @return a new {@link ListState}
 */
protected ListState createListState(final String webUrl, final String listName, final WebState ws,
        final FeedType feedType, final Boolean fixedId) throws SharepointException {
    String listUrl = webUrl + "/" + listName;
    String listId = generateId(listUrl, fixedId);

    AttributesImpl attr = new AttributesImpl();
    attr.addAttribute("", "", SPConstants.STATE_ID, "", listId);
    attr.addAttribute("", "", SPConstants.STATE_BIGGESTID, "", "0");
    attr.addAttribute("", "", SPConstants.STATE_TYPE, "", SPConstants.DOC_LIB);
    attr.addAttribute("", "", SPConstants.STATE_URL, "", listUrl);
    attr.addAttribute("", "", SPConstants.STATE_LASTMODIFIED, "", Util.formatDate(Calendar.getInstance()));
    attr.addAttribute("", "", SPConstants.STATE_CHANGETOKEN, "", "mock-change-token");
    return ListState.loadStateFromXML(ws, attr, feedType);
}

From source file:DOM2SAX.java

/**
 * Writes a node using the given writer.
 * @param node node to serialize/*w  w w . j av  a 2  s.c  o m*/
 * @throws SAXException In case of a problem while writing XML
 */
private void writeNode(Node node) throws SAXException {
    if (node == null) {
        return;
    }

    switch (node.getNodeType()) {
    case Node.ATTRIBUTE_NODE: // handled by ELEMENT_NODE
    case Node.DOCUMENT_FRAGMENT_NODE:
    case Node.DOCUMENT_TYPE_NODE:
    case Node.ENTITY_NODE:
    case Node.ENTITY_REFERENCE_NODE:
    case Node.NOTATION_NODE:
        // These node types are ignored!!!
        break;
    case Node.CDATA_SECTION_NODE:
        final String cdata = node.getNodeValue();
        if (lexicalHandler != null) {
            lexicalHandler.startCDATA();
            contentHandler.characters(cdata.toCharArray(), 0, cdata.length());
            lexicalHandler.endCDATA();
        } else {
            // in the case where there is no lex handler, we still
            // want the text of the cdate to make its way through.
            contentHandler.characters(cdata.toCharArray(), 0, cdata.length());
        }
        break;

    case Node.COMMENT_NODE: // should be handled!!!
        if (lexicalHandler != null) {
            final String value = node.getNodeValue();
            lexicalHandler.comment(value.toCharArray(), 0, value.length());
        }
        break;
    case Node.DOCUMENT_NODE:
        contentHandler.startDocument();
        Node next = node.getFirstChild();
        while (next != null) {
            writeNode(next);
            next = next.getNextSibling();
        }
        contentHandler.endDocument();
        break;

    case Node.ELEMENT_NODE:
        String prefix;
        List pushedPrefixes = new java.util.ArrayList();
        final AttributesImpl attrs = new AttributesImpl();
        final NamedNodeMap map = node.getAttributes();
        final int length = map.getLength();

        // Process all namespace declarations
        for (int i = 0; i < length; i++) {
            final Node attr = map.item(i);
            final String qnameAttr = attr.getNodeName();

            // Ignore everything but NS declarations here
            if (qnameAttr.startsWith(XMLNS_PREFIX)) {
                final String uriAttr = attr.getNodeValue();
                final int colon = qnameAttr.lastIndexOf(':');
                prefix = (colon > 0) ? qnameAttr.substring(colon + 1) : EMPTYSTRING;
                if (startPrefixMapping(prefix, uriAttr)) {
                    pushedPrefixes.add(prefix);
                }
            }
        }

        // Process all other attributes
        for (int i = 0; i < length; i++) {
            final Node attr = map.item(i);
            final String qnameAttr = attr.getNodeName();

            // Ignore NS declarations here
            if (!qnameAttr.startsWith(XMLNS_PREFIX)) {
                final String uriAttr = attr.getNamespaceURI();

                // Uri may be implicitly declared
                if (uriAttr != null) {
                    final int colon = qnameAttr.lastIndexOf(':');
                    prefix = (colon > 0) ? qnameAttr.substring(0, colon) : EMPTYSTRING;
                    if (startPrefixMapping(prefix, uriAttr)) {
                        pushedPrefixes.add(prefix);
                    }
                }

                // Add attribute to list
                attrs.addAttribute(attr.getNamespaceURI(), getLocalName(attr), qnameAttr, "CDATA",
                        attr.getNodeValue());
            }
        }

        // Now process the element itself
        final String qname = node.getNodeName();
        final String uri = node.getNamespaceURI();
        final String localName = getLocalName(node);

        // Uri may be implicitly declared
        if (uri != null) {
            final int colon = qname.lastIndexOf(':');
            prefix = (colon > 0) ? qname.substring(0, colon) : EMPTYSTRING;
            if (startPrefixMapping(prefix, uri)) {
                pushedPrefixes.add(prefix);
            }
        }

        // Generate SAX event to start element
        contentHandler.startElement(uri, localName, qname, attrs);

        // Traverse all child nodes of the element (if any)
        next = node.getFirstChild();
        while (next != null) {
            writeNode(next);
            next = next.getNextSibling();
        }

        // Generate SAX event to close element
        contentHandler.endElement(uri, localName, qname);

        // Generate endPrefixMapping() for all pushed prefixes
        final int nPushedPrefixes = pushedPrefixes.size();
        for (int i = 0; i < nPushedPrefixes; i++) {
            endPrefixMapping((String) pushedPrefixes.get(i));
        }
        break;

    case Node.PROCESSING_INSTRUCTION_NODE:
        contentHandler.processingInstruction(node.getNodeName(), node.getNodeValue());
        break;

    case Node.TEXT_NODE:
        final String data = node.getNodeValue();
        contentHandler.characters(data.toCharArray(), 0, data.length());
        break;
    default:
        //nop
    }
}

From source file:com.adobe.acs.commons.rewriter.impl.VersionedClientlibsTransformerFactoryTest.java

@Test
public void testCssClientLibrary() throws Exception {

    when(htmlLibraryManager.getLibrary(eq(LibraryType.CSS), eq(PATH))).thenReturn(htmlLibrary);

    final AttributesImpl in = new AttributesImpl();
    in.addAttribute("", "href", "", "CDATA", PATH + ".css");
    in.addAttribute("", "type", "", "CDATA", "text/css");
    in.addAttribute("", "rel", "", "CDATA", "stylesheet");

    transformer.startElement(null, "link", null, in);

    ArgumentCaptor<Attributes> attributesCaptor = ArgumentCaptor.forClass(Attributes.class);

    verify(handler, only()).startElement(isNull(String.class), eq("link"), isNull(String.class),
            attributesCaptor.capture());

    assertEquals(PATH + "." + FAKE_STREAM_CHECKSUM + ".css", attributesCaptor.getValue().getValue(0));
}

From source file:no.kantega.commons.util.XMLHelper.java

public static AttributesImpl getAttributesImpl(Attributes attributes) {
    AttributesImpl impl = new AttributesImpl();

    for (int i = 0; i < attributes.getLength(); i++) {
        impl.addAttribute(attributes.getURI(i), attributes.getLocalName(i), attributes.getQName(i),
                attributes.getType(i), attributes.getValue(i));
    }/*  w  w w.j  av a 2 s .c  o  m*/
    return impl;
}

From source file:com.jkoolcloud.tnt4j.streams.configure.sax.ConfigParserHandlerTest.java

@Test(expected = SAXParseException.class)
public void processParserTest1() throws Exception {
    ConfigParserHandler test = Mockito.mock(ConfigParserHandler.class, Mockito.CALLS_REAL_METHODS);
    AttributesImpl attrs = new AttributesImpl();
    attrs.addAttribute("", "", "name", "", "Stream attr name"); // NON-NLS
    attrs.addAttribute("", "", "type", "", "Stream attr type"); // NON-NLS
    attrs.addAttribute("", "", "filter", "", "Stream attr filter"); // NON-NLS
    // attrs.addAttribute("", "", "rule", "", "Stream attr rule"); // NON-NLS
    // attrs.addAttribute("", "", "step", "", "Stream attr step"); // NON-NLS
    attrs.addAttribute("", "", "tnt4j-properties", "", "Stream attr tnt4j-properties"); // NON-NLS
    attrs.addAttribute("", "", "java-object", "", "Stream attr java-object"); // NON-NLS
    attrs.addAttribute("", "", "param", "", "Stream attr param"); // NON-NLS
    attrs.addAttribute("", "", "tags", "", "Stream attr tags"); // NON-NLS
    test.startElement("TEST_URL", "TEST_LOCALNAME", "parser", attrs); // NON-NLS
}

From source file:com.adobe.acs.commons.rewriter.impl.VersionedClientlibsTransformerFactoryTest.java

@Test
public void testCssClientLibraryWithMd5Enforce() throws Exception {
    PrivateAccessor.setField(factory, "enforceMd5", true);

    when(htmlLibraryManager.getLibrary(eq(LibraryType.CSS), eq(PATH))).thenReturn(htmlLibrary);

    final AttributesImpl in = new AttributesImpl();
    in.addAttribute("", "href", "", "CDATA", PATH + ".css");
    in.addAttribute("", "type", "", "CDATA", "text/css");
    in.addAttribute("", "rel", "", "CDATA", "stylesheet");

    transformer.startElement(null, "link", null, in);

    ArgumentCaptor<Attributes> attributesCaptor = ArgumentCaptor.forClass(Attributes.class);

    verify(handler, only()).startElement(isNull(String.class), eq("link"), isNull(String.class),
            attributesCaptor.capture());

    assertEquals(PATH + ".ACSHASH" + FAKE_STREAM_CHECKSUM + ".css", attributesCaptor.getValue().getValue(0));
}

From source file:dicoogle.ua.dim.DIMGeneric.java

public String getXML() {

    StringWriter writer = new StringWriter();

    StreamResult streamResult = new StreamResult(writer);
    SAXTransformerFactory tf = (SAXTransformerFactory) TransformerFactory.newInstance();
    //      SAX2.0 ContentHandler.
    TransformerHandler hd = null;
    try {/*w  ww .j  a  v a 2  s . c om*/
        hd = tf.newTransformerHandler();
    } catch (TransformerConfigurationException ex) {
        Logger.getLogger(DIMGeneric.class.getName()).log(Level.SEVERE, null, ex);
    }
    Transformer serializer = hd.getTransformer();
    serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    serializer.setOutputProperty(OutputKeys.METHOD, "xml");
    serializer.setOutputProperty(OutputKeys.INDENT, "yes");
    serializer.setOutputProperty(OutputKeys.STANDALONE, "yes");
    hd.setResult(streamResult);
    try {
        hd.startDocument();

        AttributesImpl atts = new AttributesImpl();
        hd.startElement("", "", "DIM", atts);

        for (Patient p : this.patients) {
            atts.clear();
            atts.addAttribute("", "", "name", "", p.getPatientName().trim());
            atts.addAttribute("", "", "id", "", p.getPatientID().trim());
            hd.startElement("", "", "Patient", atts);

            for (Study s : p.getStudies()) {
                atts.clear();
                atts.addAttribute("", "", "date", "", s.getStudyData().trim());
                atts.addAttribute("", "", "id", "", s.getStudyInstanceUID().trim());

                hd.startElement("", "", "Study", atts);

                for (Serie serie : s.getSeries()) {
                    atts.clear();
                    atts.addAttribute("", "", "modality", "", serie.getModality().trim());
                    atts.addAttribute("", "", "id", "", serie.getSerieInstanceUID().trim());

                    hd.startElement("", "", "Serie", atts);

                    ArrayList<URI> img = serie.getImageList();
                    ArrayList<String> uid = serie.getSOPInstanceUIDList();
                    int size = img.size();
                    for (int i = 0; i < size; i++) {
                        atts.clear();
                        atts.addAttribute("", "", "path", "", img.get(i).toString().trim());
                        atts.addAttribute("", "", "uid", "", uid.get(i).trim());

                        hd.startElement("", "", "Image", atts);
                        hd.endElement("", "", "Image");
                    }
                    hd.endElement("", "", "Serie");
                }
                hd.endElement("", "", "Study");
            }
            hd.endElement("", "", "Patient");
        }
        hd.endElement("", "", "DIM");

    } catch (SAXException ex) {
        Logger.getLogger(DIMGeneric.class.getName()).log(Level.SEVERE, null, ex);
    }

    return writer.toString();
}

From source file:com.jkoolcloud.tnt4j.streams.configure.sax.ConfigParserHandlerTest.java

@Test(expected = SAXParseException.class)
public void processParserTest2() throws Exception {
    ConfigParserHandler test = Mockito.mock(ConfigParserHandler.class, Mockito.CALLS_REAL_METHODS);
    AttributesImpl attrs = new AttributesImpl();
    attrs.addAttribute("", "", "type", "", "Stream attr type"); // NON-NLS
    attrs.addAttribute("", "", "class", "", "Stream attr class"); // NON-NLS
    attrs.addAttribute("", "", "filter", "", "Stream attr filter"); // NON-NLS
    // attrs.addAttribute("", "", "rule", "", "Stream attr rule"); // NON-NLS
    // attrs.addAttribute("", "", "step", "", "Stream attr step"); // NON-NLS
    attrs.addAttribute("", "", "tnt4j-properties", "", "Stream attr tnt4j-properties"); // NON-NLS
    attrs.addAttribute("", "", "java-object", "", "Stream attr java-object"); // NON-NLS
    attrs.addAttribute("", "", "param", "", "Stream attr param"); // NON-NLS
    attrs.addAttribute("", "", "tags", "", "Stream attr tags"); // NON-NLS
    test.startElement("TEST_URL", "TEST_LOCALNAME", "parser", attrs); // NON-NLS
}