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:org.apache.syncope.core.util.ImportExport.java

public void export(final OutputStream os) throws SAXException, TransformerConfigurationException {

    StreamResult streamResult = new StreamResult(os);
    final SAXTransformerFactory transformerFactory = (SAXTransformerFactory) SAXTransformerFactory
            .newInstance();/*w  ww . j  a v  a  2 s  .c om*/

    TransformerHandler handler = transformerFactory.newTransformerHandler();
    Transformer serializer = handler.getTransformer();
    serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    serializer.setOutputProperty(OutputKeys.INDENT, "yes");
    handler.setResult(streamResult);
    handler.startDocument();
    handler.startElement("", "", ROOT_ELEMENT, new AttributesImpl());

    final Connection conn = DataSourceUtils.getConnection(dataSource);

    ResultSet rs = null;

    try {
        final DatabaseMetaData meta = conn.getMetaData();

        final String schema = readSchema();

        rs = meta.getTables(null, schema, null, new String[] { "TABLE" });

        final Set<String> tableNames = new HashSet<String>();

        while (rs.next()) {
            String tableName = rs.getString("TABLE_NAME");

            // these tables must be ignored
            if (!tableName.toUpperCase().startsWith("QRTZ_")
                    && !tableName.toUpperCase().startsWith("LOGGING_")) {
                tableNames.add(tableName);
            }
        }

        // then sort tables based on foreign keys and dump
        for (String tableName : sortByForeignKeys(conn, tableNames, schema)) {
            doExportTable(handler, conn, tableName);
        }
    } catch (SQLException e) {
        LOG.error("While exporting database content", e);
    } finally {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                LOG.error("While closing tables result set", e);
            }
        }
        DataSourceUtils.releaseConnection(conn, dataSource);
    }

    handler.endElement("", "", ROOT_ELEMENT);
    handler.endDocument();
}

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

@Test
public void testJavaScriptClientLibraryWithInvalidExtension() throws Exception {

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

    final AttributesImpl in = new AttributesImpl();
    in.addAttribute("", "src", "", "CDATA", PATH + ".vbs");
    in.addAttribute("", "type", "", "CDATA", "text/javascript");

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

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

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

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

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

@Test(expected = SAXParseException.class)
public void processFieldLocatorExceptionTest() throws Exception {
    ConfigParserHandler test = Mockito.mock(ConfigParserHandler.class, Mockito.CALLS_REAL_METHODS);
    test.startDocument();/*  w  w  w. j  a  v  a2  s.com*/
    AttributesImpl attrs = new AttributesImpl();
    attrs.addAttribute("", "", "name", "", "Stream attr name"); // NON-NLS
    attrs.addAttribute("", "", "class", "", "com.jkoolcloud.tnt4j.streams.parsers.ActivityTokenParser"); // NON-NLS
    attrs.addAttribute("", "", "datatype", "", "DateTime"); // NON-NLS
    attrs.addAttribute("", "", "format", "", null);
    test.startElement("TEST_URL", "TEST_LOCALNAME", "parser", attrs); // NON-NLS//NON-NLS
    test.startElement("TEST_URL", "TEST_LOCALNAME", "field", attrs); // NON-NLS
    attrs.addAttribute("", "", "value", "", ""); // NON-NLS
    test.startElement("TEST_URL", "TEST_LOCALNAME", "field-locator", attrs); // NON-NLS
}

From source file:biz.taoconsulting.oxf.processor.converter.FromPdfConverter.java

/**
 * @param xmlReceiver/*w w w.  j  a  va2s  . com*/
 * @param doc
 */
private boolean extractPagesFromPDF(XMLReceiver xmlReceiver, PDDocument doc) {

    // This extracts all pages with the text per page
    boolean tmpReturn = true; // Benefit of the doubt
    PDFTextStripper stripper;
    try {
        stripper = new PDFTextStripper();

        AttributesImpl atts;

        // Capture the number of pages
        int pageCount = doc.getPageCount();

        //Loop through all the pages;
        for (int i = 1; i <= pageCount; i++) {
            atts = new AttributesImpl();
            atts.addAttribute("", ATT_PAGENUM, ATT_PAGENUM, ATT_CDATA, String.valueOf(i));
            stripper.setStartPage(i);
            stripper.setEndPage(i);
            String textBetweenBookmarks = stripper.getText(doc);
            xmlReceiver.startElement("", TAG_PAGE, TAG_PAGE, atts);
            textBetweenBookmarks = MassageTextResult(textBetweenBookmarks);
            xmlReceiver.characters(textBetweenBookmarks.toCharArray(), 0, textBetweenBookmarks.length());
            xmlReceiver.endElement("", TAG_PAGE, TAG_PAGE);
        }
    } catch (IOException e) {
        // If it goes wrong
        logger.error(e);
        addErrorTagToOutput(xmlReceiver, e.toString());
        tmpReturn = false;
    } catch (SAXException e) {
        // If it goes wrong
        logger.error(e);
        addErrorTagToOutput(xmlReceiver, e.toString());
        tmpReturn = false;
    }
    return tmpReturn;
}

From source file:com.rapidminer.gui.properties.OperatorPropertyPanel.java

/**
 * Starts a progress thread which parses the parameter descriptions for the provided operator ,
 * cleans the {@link #parameterDescriptionCache}, and stores parsed descriptions in the
 * {@link #parameterDescriptionCache}.//from  w w w. j a  va 2  s . co  m
 */
private void parseParameterDescriptions(final Operator operator) {
    parameterDescriptionCache.clear();
    URL documentationURL = OperatorDocumentationBrowser.getDocResourcePath(operator);
    if (documentationURL != null) {
        try (InputStream documentationStream = documentationURL.openStream()) {
            XMLStreamReader reader = XML_STREAM_FACTORY.createXMLStreamReader(documentationStream);
            String parameterKey = null;

            // The builder that stores the parameter description text
            StringBuilder parameterTextBuilder = null;
            boolean inParameters = false;
            while (reader.hasNext()) {
                switch (reader.next()) {
                case XMLStreamReader.START_ELEMENT:
                    if (!inParameters && reader.getLocalName().equals(TAG_PARAMETERS)) {
                        inParameters = true;
                    } else {
                        AttributesImpl attributes = new AttributesImpl();
                        for (int i = 0; i < reader.getAttributeCount(); i++) {
                            attributes.addAttribute("", reader.getAttributeLocalName(i),
                                    reader.getAttributeName(i).toString(), reader.getAttributeType(i),
                                    reader.getAttributeValue(i));
                        }

                        // Check if no parameter was found
                        if (reader.getLocalName().equals(TAG_PARAMETER)) {
                            parameterKey = attributes.getValue(ATTRIBUTE_PARAMETER_KEY);

                            // In case a parameter key was found, create a new string
                            // builder
                            if (parameterKey != null) {
                                parameterTextBuilder = new StringBuilder();
                            }
                        }

                        if (parameterTextBuilder != null) {
                            appendParameterStartTag(reader.getLocalName(), attributes, parameterTextBuilder);
                        }
                    }
                    break;
                case XMLStreamReader.END_ELEMENT:
                    // end parsing when end of parameters element is reached
                    if (reader.getLocalName().equals(TAG_PARAMETERS)) {
                        return;
                    }

                    if (parameterTextBuilder != null) {

                        // otherwise add element to description text
                        parameterTextBuilder.append("</");
                        parameterTextBuilder.append(reader.getLocalName());
                        parameterTextBuilder.append(">");

                        // Store description when parameter element ends
                        if (reader.getLocalName().equals(TAG_PARAMETER)) {
                            final String parameterDescription = parameterTextBuilder.toString();
                            final String key = parameterKey;
                            if (!parameterDescriptionCache.containsKey(parameterKey)) {
                                Source xmlSource = new StreamSource(new StringReader(parameterDescription));
                                try {
                                    String desc = OperatorDocToHtmlConverter.applyXSLTTransformation(xmlSource);
                                    parameterDescriptionCache.put(key, StringEscapeUtils.unescapeHtml(desc));
                                } catch (TransformerException e) {
                                    // ignore
                                }
                            }
                        }
                    }
                    break;
                case XMLStreamReader.CHARACTERS:
                    if (parameterTextBuilder != null) {
                        parameterTextBuilder.append(StringEscapeUtils.escapeHtml(reader.getText()));
                    }
                    break;
                default:
                    // ignore other events
                    break;
                }
            }
        } catch (IOException | XMLStreamException e) {
            // ignore
        }
    }
}

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

@Test
public void testJavaScriptClientLibraryWithRelativePath() throws Exception {

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

    final AttributesImpl in = new AttributesImpl();
    in.addAttribute("", "src", "", "CDATA", "relative/script.js");
    in.addAttribute("", "type", "", "CDATA", "text/javascript");

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

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

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

    assertEquals("relative/script.js", attributesCaptor.getValue().getValue(0));
}

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

@Test
public void processFieldLocatorNotNullFromatTest() throws Exception {
    ConfigParserHandler test = Mockito.mock(ConfigParserHandler.class, Mockito.CALLS_REAL_METHODS);
    test.startDocument();//from w w w  .  j  a v  a 2  s .  c om
    AttributesImpl attrs = new AttributesImpl();
    attrs.addAttribute("", "", "name", "", "Stream attr name");// NON-NLS
    attrs.addAttribute("", "", "class", "", "com.jkoolcloud.tnt4j.streams.parsers.ActivityTokenParser"); // NON-NLS
    attrs.addAttribute("", "", "datatype", "", "DateTime"); // NON-NLS
    attrs.addAttribute("", "", "format", "", "YYYY-mm-dd"); // NON-NLS
    test.startElement("TEST_URL", "TEST_LOCALNAME", "parser", attrs); // NON-NLS
    test.startElement("TEST_URL", "TEST_LOCALNAME", "field", attrs); // NON-NLS
    attrs.addAttribute("", "", "value", "", null); // NON-NLS
    test.startElement("TEST_URL", "TEST_LOCALNAME", "field-locator", attrs); // NON-NLS
}

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

@Test
public void testJavaScriptClientLibraryWithSameSchemePath() throws Exception {

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

    final AttributesImpl in = new AttributesImpl();
    in.addAttribute("", "src", "", "CDATA", "//example.com/same/scheme/script.js");
    in.addAttribute("", "type", "", "CDATA", "text/javascript");

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

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

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

    assertEquals("//example.com/same/scheme/script.js", attributesCaptor.getValue().getValue(0));
}

From source file:de.gbv.ole.Marc21ToOleBulk.java

/**
 * Writes a Record object to the result.
 *
 * @param ppn    PPN of the record//w  w w  .  ja  va  2  s.co m
 * @param record -
 *               the <code>Record</code> object
 * @throws SAXException     
 */
protected final void toXml(final String ppn, final Record record) throws SAXException, IOException {
    if (!marcFactory.validateRecord(record)) {
        throw new MarcException("Marc record didn't validate");
    }

    startElement(COLLECTION);
    startElement(RECORD);
    startElement(LEADER);
    write(record.getLeader().toString());
    endElement(LEADER);

    for (ControlField field : record.getControlFields()) {
        AttributesImpl atts = new AttributesImpl();
        add(atts, "tag", field.getTag());
        startElement(CONTROL_FIELD, atts);
        write(field.getData());
        endElement(CONTROL_FIELD);
    }

    Map<String, Exemplar> exemplare = new HashMap<String, Exemplar>();

    for (DataField field : record.getDataFields()) {
        if (field == null) {
            throw new MarcException("DataField is null");
        }

        try {
            {
                AttributesImpl atts = new AttributesImpl();
                add(atts, "tag", field.getTag());
                add(atts, "ind1", field.getIndicator1());
                add(atts, "ind2", field.getIndicator2());
                startElement(DATA_FIELD, atts);
            }

            for (Subfield subfield : field.getSubfields()) {
                AttributesImpl atts = new AttributesImpl();
                add(atts, "code", subfield.getCode());
                startElement(SUBFIELD, atts);
                write(subfield.getData());
                endElement(SUBFIELD);
            }

            endElement(DATA_FIELD);

            exemplar(field, exemplare);
        } catch (Exception e) {
            throw new MarcException("Marc data field: " + print(field), e);
        }
    }

    endElement(RECORD);
    endElement(COLLECTION);

    write(ppn, record, exemplare);
}

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

@Test(expected = SAXParseException.class)
public void processFieldMapSourceExceptionTest() throws Exception {
    ConfigParserHandler test = Mockito.mock(ConfigParserHandler.class, Mockito.CALLS_REAL_METHODS);
    test.startDocument();//from www  .  j a v a  2  s .c o m
    AttributesImpl attrs = new AttributesImpl();
    attrs.addAttribute("", "", "name", "", "Stream attr name"); // NON-NLS
    attrs.addAttribute("", "", "source", "", null); // NON-NLS
    attrs.addAttribute("", "", "target", "", "Stream target value"); // NON-NLS
    attrs.addAttribute("", "", "value", "", ""); // NON-NLS
    attrs.addAttribute("", "", "class", "", "com.jkoolcloud.tnt4j.streams.parsers.ActivityTokenParser"); // NON-NLS
    test.startElement("TEST_URL", "TEST_LOCALNAME", "parser", attrs); // NON-NLS
    test.startElement("TEST_URL", "TEST_LOCALNAME", "field", attrs); // NON-NLS
    test.startElement("TEST_URL", "TEST_LOCALNAME", "field-map", attrs); // NON-NLS
}