Example usage for org.xml.sax ContentHandler startElement

List of usage examples for org.xml.sax ContentHandler startElement

Introduction

In this page you can find the example usage for org.xml.sax ContentHandler startElement.

Prototype

public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException;

Source Link

Document

Receive notification of the beginning of an element.

Usage

From source file:org.syncope.core.report.UserReportlet.java

private void doExtractAttributes(final ContentHandler handler, final AbstractAttributableTO attributableTO,
        final Collection<String> attrs, final Collection<String> derAttrs, final Collection<String> virAttrs)
        throws SAXException {

    AttributesImpl atts = new AttributesImpl();
    if (!attrs.isEmpty()) {
        Map<String, AttributeTO> attrMap = attributableTO.getAttributeMap();

        handler.startElement("", "", "attributes", null);
        for (String attrName : attrs) {
            atts.clear();//  w w w .j a  v  a 2 s.co m

            atts.addAttribute("", "", ATTR_NAME, XSD_STRING, attrName);
            handler.startElement("", "", "attribute", atts);

            if (attrMap.containsKey(attrName)) {
                for (String value : attrMap.get(attrName).getValues()) {
                    handler.startElement("", "", "value", null);
                    handler.characters(value.toCharArray(), 0, value.length());
                    handler.endElement("", "", "value");
                }
            } else {
                LOG.debug("{} not found for {}[{}]", new Object[] { attrName,
                        attributableTO.getClass().getSimpleName(), attributableTO.getId() });
            }

            handler.endElement("", "", "attribute");
        }
        handler.endElement("", "", "attributes");
    }

    if (!derAttrs.isEmpty()) {
        Map<String, AttributeTO> derAttrMap = attributableTO.getDerivedAttributeMap();

        handler.startElement("", "", "derivedAttributes", null);
        for (String attrName : derAttrs) {
            atts.clear();

            atts.addAttribute("", "", ATTR_NAME, XSD_STRING, attrName);
            handler.startElement("", "", "derivedAttribute", atts);

            if (derAttrMap.containsKey(attrName)) {
                for (String value : derAttrMap.get(attrName).getValues()) {
                    handler.startElement("", "", "value", null);
                    handler.characters(value.toCharArray(), 0, value.length());
                    handler.endElement("", "", "value");
                }
            } else {
                LOG.debug("{} not found for {}[{}]", new Object[] { attrName,
                        attributableTO.getClass().getSimpleName(), attributableTO.getId() });
            }

            handler.endElement("", "", "derivedAttribute");
        }
        handler.endElement("", "", "derivedAttributes");
    }

    if (!virAttrs.isEmpty()) {
        Map<String, AttributeTO> virAttrMap = attributableTO.getVirtualAttributeMap();

        handler.startElement("", "", "virtualAttributes", null);
        for (String attrName : virAttrs) {
            atts.clear();

            atts.addAttribute("", "", ATTR_NAME, XSD_STRING, attrName);
            handler.startElement("", "", "virtualAttribute", atts);

            if (virAttrMap.containsKey(attrName)) {
                for (String value : virAttrMap.get(attrName).getValues()) {
                    handler.startElement("", "", "value", null);
                    handler.characters(value.toCharArray(), 0, value.length());
                    handler.endElement("", "", "value");
                }
            } else {
                LOG.debug("{} not found for {}[{}]", new Object[] { attrName,
                        attributableTO.getClass().getSimpleName(), attributableTO.getId() });
            }

            handler.endElement("", "", "virtualAttribute");
        }
        handler.endElement("", "", "virtualAttributes");
    }
}

From source file:com.mirth.connect.plugins.datatypes.delimited.DelimitedReader.java

public void parse(InputSource input) throws SAXException, IOException {

    // Parsing overview
    ////from w w  w  .jav a 2  s. c o  m
    // The incoming stream is a single message which is a collection of one
    // or more records.
    // Each record is a collection of columns. Columns can be either fixed
    // width, or delimited.
    // Records are delimited.
    // Each record is assumed to contain the same number and type of
    // columns.
    //
    // The following user configurable options affect the behavior of the
    // parser:
    // o columnWidths The array of fixed column widths.
    // o columnDelimiter The characters that delimit (separate) the
    // columns.
    // o recordDelimiter The characters that delimit (separate) each
    // record.
    // o quoteToken The characters that are used to quote a column value.
    // o escapeWithDoubleQuote Iff true, embedded quotes are escaped with
    // two consecutive quotes.
    // Otherwise, the quote escape characters escape embedded quote
    // characters.
    // o quoteEscapeToken The characters used to escape a quote (or itself).
    // o ignoreCR Iff true, all incoming \r characters are ignored and not
    // processed.
    //
    // The following user configurable options affect the behavior of the
    // output:
    // o columnNames A list of column names (taken from either file header,
    // or supplied by user).
    BufferedReader in = new BufferedReader(input.getCharacterStream());

    // Start the document
    String documentHead = "delimited";
    ContentHandler contentHandler = getContentHandler();
    contentHandler.startDocument();

    // Output <delimited>
    contentHandler.startElement("", documentHead, "", null);

    // While the parser gets records from the message
    ArrayList<String> record;
    int recordNo = 1;
    while ((record = getRecord(in, null)) != null) {

        // Output <rowN>
        if (serializationProperties.isNumberedRows()) {
            contentHandler.startElement("", "row" + recordNo, "", null);
        } else {
            contentHandler.startElement("", "row", "", null);
        }

        // For each column
        for (int i = 0; i < record.size(); i++) {

            String columnName;
            if (serializationProperties.getColumnNames() != null
                    && i < serializationProperties.getColumnNames().length) {
                // User specified column name
                columnName = serializationProperties.getColumnNames()[i];
            } else {
                // Default column name
                columnName = "column" + (i + 1);
            }
            // Output <columnN>
            contentHandler.startElement("", columnName, "", null);

            // Output column value
            contentHandler.characters(record.get(i).toCharArray(), 0, record.get(i).length());

            // Output </columnN>
            contentHandler.endElement("", columnName, "");
        }

        // Output </rowN>
        if (serializationProperties.isNumberedRows()) {
            contentHandler.endElement("", "row" + recordNo, "");
        } else {
            contentHandler.endElement("", "row", "");
        }

        recordNo++;
    }

    // Output </delimited>
    contentHandler.endElement("", documentHead, "");

    // End the document
    contentHandler.endDocument();
}

From source file:nl.architolk.ldt.processors.HttpClientProcessor.java

private void parseJSONObject(ContentHandler contentHandler, JSONObject json) throws SAXException {
    Object[] names = json.names().toArray();
    for (int i = 0; i < names.length; i++) {
        String name = (String) names[i];
        Object value = json.get(name);
        String safeName = name.replace('@', '_');
        contentHandler.startElement("", safeName, safeName, new AttributesImpl());

        if (value instanceof JSONObject) {
            parseJSONObject(contentHandler, (JSONObject) value);
        } else if (value instanceof JSONArray) {
            Iterator<?> jsonit = ((JSONArray) value).iterator();
            while (jsonit.hasNext()) {
                Object arrayValue = jsonit.next();
                if (arrayValue instanceof JSONObject) {
                    parseJSONObject(contentHandler, (JSONObject) arrayValue);
                } else {
                    String textValue = String.valueOf(arrayValue);
                    contentHandler.characters(textValue.toCharArray(), 0, textValue.length());
                }//w  w w.  j  a v a  2 s .  c o  m
                //Array means repeating the XML node, so end the current node, and start a new one
                if (jsonit.hasNext()) {
                    contentHandler.endElement("", safeName, safeName);
                    contentHandler.startElement("", safeName, safeName, new AttributesImpl());
                }
            }
        } else {
            String textValue = String.valueOf(value);
            contentHandler.characters(textValue.toCharArray(), 0, textValue.length());
        }

        contentHandler.endElement("", safeName, safeName);
    }
}

From source file:com.mirth.connect.plugins.datatypes.hl7v2.ER7Reader.java

private void handleComponents(ContentHandler contentHandler, String componentSeparator,
        String subcomponentSeparator, String segmentId, int fieldId, int componentId,
        StringTokenizer componentTokenizer) throws SAXException {
    boolean atLastComponent = true;

    while (componentTokenizer.hasMoreTokens()) {
        String component = componentTokenizer.nextToken();

        if (component.equals(componentSeparator)) {
            if (atLastComponent) {
                contentHandler.startElement("", segmentId + "." + fieldId + "." + componentId, "", null);
                contentHandler.characters(EMPTY_CHAR_ARRAY, 0, 0);
                contentHandler.endElement("", segmentId + "." + fieldId + "." + componentId, "");
            }//from   w w w.  ja  va2 s  .com

            componentId++;
            atLastComponent = true;
        } else {
            atLastComponent = false;
            handleComponent(contentHandler, subcomponentSeparator, segmentId, fieldId, componentId, component);
        }
    }

    if (atLastComponent) {
        contentHandler.startElement("", segmentId + "." + fieldId + "." + componentId, "", null);
        contentHandler.characters(EMPTY_CHAR_ARRAY, 0, 0);
        contentHandler.endElement("", segmentId + "." + fieldId + "." + componentId, "");
    }
}

From source file:CSVXMLReader.java

private void parseLine(String curLine, ContentHandler ch) throws IOException, SAXException {

    String firstToken = null;//from   www. ja  va2s . com
    String remainderOfLine = null;
    int commaIndex = locateFirstDelimiter(curLine);
    if (commaIndex > -1) {
        firstToken = curLine.substring(0, commaIndex).trim();
        remainderOfLine = curLine.substring(commaIndex + 1).trim();
    } else {
        // no commas, so the entire line is the token
        firstToken = curLine;
    }

    // remove redundant quotes
    firstToken = cleanupQuotes(firstToken);

    // emit the <value> element
    ch.startElement("", "", "value", EMPTY_ATTR);
    ch.characters(firstToken.toCharArray(), 0, firstToken.length());
    ch.endElement("", "", "value");

    // recursively process the remainder of the line
    if (remainderOfLine != null) {
        parseLine(remainderOfLine, ch);
    }
}

From source file:com.mirth.connect.plugins.datatypes.hl7v2.ER7Reader.java

private void handleSubcomponents(ContentHandler contentHandler, String subcomponentSeparator, String segmentId,
        int fieldId, int componentId, int subcomponentId, StringTokenizer subcomponentTokenizer)
        throws SAXException {
    boolean atLastSubcomponent = true;

    while (subcomponentTokenizer.hasMoreTokens()) {
        String subcomponent = subcomponentTokenizer.nextToken();

        if (subcomponent.equals(subcomponentSeparator)) {
            if (atLastSubcomponent) {
                contentHandler.startElement("",
                        segmentId + "." + fieldId + "." + componentId + "." + subcomponentId, "", null);
                contentHandler.characters(EMPTY_CHAR_ARRAY, 0, 0);
                contentHandler.endElement("",
                        segmentId + "." + fieldId + "." + componentId + "." + subcomponentId, "");
            }//from  w  w  w  .j  a  v  a 2s.  c o  m

            subcomponentId++;
            atLastSubcomponent = true;
        } else {
            logger.trace("handling subcomponent: " + subcomponentId);
            atLastSubcomponent = false;
            // the naming is SEG.<field#>.<component#>.<subcomponent#>
            contentHandler.startElement("",
                    segmentId + "." + fieldId + "." + componentId + "." + subcomponentId, "", null);
            contentHandler.characters(subcomponent.toCharArray(), 0, subcomponent.length());
            contentHandler.endElement("", segmentId + "." + fieldId + "." + componentId + "." + subcomponentId,
                    "");
        }
    }

    if (atLastSubcomponent) {
        contentHandler.startElement("", segmentId + "." + fieldId + "." + componentId + "." + subcomponentId,
                "", null);
        contentHandler.characters(EMPTY_CHAR_ARRAY, 0, 0);
        contentHandler.endElement("", segmentId + "." + fieldId + "." + componentId + "." + subcomponentId, "");
    }
}

From source file:org.syncope.core.report.UserReportlet.java

@Override
@Transactional(readOnly = true)//from ww w . jav a 2s. c  om
public void extract(final ContentHandler handler) throws SAXException, ReportException {

    if (getConf() == null || !(getConf() instanceof UserReportletConf)) {
        throw new ReportException(new IllegalArgumentException(
                "Expected " + UserReportletConf.class.getName() + ", got " + getConf()));
    }

    UserReportletConf conf = (UserReportletConf) getConf();

    AttributesImpl atts = new AttributesImpl();
    atts.addAttribute("", "", ATTR_NAME, XSD_STRING, getConf().getName());
    atts.addAttribute("", "", ATTR_CLASS, XSD_STRING, getClass().getName());
    handler.startElement("", "", ELEMENT_REPORTLET, atts);

    for (int i = 1; i <= (count(conf) / PAGE_SIZE) + 1; i++) {
        doExtract(handler, conf, getPagedUsers(conf, i));
    }

    handler.endElement("", "", ELEMENT_REPORTLET);
}

From source file:com.mirth.connect.plugins.datatypes.hl7v2.ER7Reader.java

private void handleFieldRepetitions(ContentHandler contentHandler, String componentSeparator,
        String repetitionSeparator, String subcomponentSeparator, String segmentId, int fieldId, String field)
        throws SAXException {
    StringTokenizer fieldRepetitionTokenizer = new StringTokenizer(field, repetitionSeparator, true);
    boolean atLastRepetition = true;

    while (fieldRepetitionTokenizer.hasMoreTokens()) {
        field = fieldRepetitionTokenizer.nextToken();

        if (field.equals(repetitionSeparator)) {
            // check for ~~
            if (atLastRepetition) {
                contentHandler.startElement("", segmentId + "." + fieldId, "", null);
                contentHandler.characters(EMPTY_CHAR_ARRAY, 0, 0);
                contentHandler.endElement("", segmentId + "." + fieldId, "");
            }//from   w  ww. j a v a  2s  .c  o  m

            atLastRepetition = true;
        } else {
            logger.trace("handling repetition: " + field);
            atLastRepetition = false;
            handleField(contentHandler, componentSeparator, subcomponentSeparator, segmentId, fieldId, field);
        }
    }

    if (atLastRepetition) {
        contentHandler.startElement("", segmentId + "." + fieldId, "", null);
        contentHandler.characters(EMPTY_CHAR_ARRAY, 0, 0);
        contentHandler.endElement("", segmentId + "." + fieldId, "");
    }
}

From source file:net.ontopia.topicmaps.db2tm.RelationMapping.java

protected void outputField(Field field, ContentHandler dh) throws SAXException {
    AttributesImpl atts = new AttributesImpl();
    if (field.getFieldType() == Field.TYPE_SUBJECT_LOCATOR) {
        addAttribute(atts, "column", "CDATA", field.getColumn());
        dh.startElement("", "", "subject-locator", atts);
        char[] c = field.getPattern().toCharArray();
        dh.characters(c, 0, c.length);/*w ww  .  j  a va 2  s.c o  m*/
        dh.endElement("", "", "subject-locator");
        atts.clear();
    } else if (field.getFieldType() == Field.TYPE_SUBJECT_IDENTIFIER) {
        addAttribute(atts, "column", "CDATA", field.getColumn());
        dh.startElement("", "", "subject-identifier", atts);
        char[] c = field.getPattern().toCharArray();
        dh.characters(c, 0, c.length);
        dh.endElement("", "", "subject-identifier");
        atts.clear();
    } else if (field.getFieldType() == Field.TYPE_ITEM_IDENTIFIER) {
        addAttribute(atts, "column", "CDATA", field.getColumn());
        dh.startElement("", "", "item-identifier", atts);
        char[] c = field.getPattern().toCharArray();
        dh.characters(c, 0, c.length);
        dh.endElement("", "", "item-identifier");
        atts.clear();
    } else if (field.getFieldType() == Field.TYPE_OCCURRENCE) {
        addAttribute(atts, "column", "CDATA", field.getColumn());
        addAttribute(atts, "type", "CDATA", field.getType());
        addAttribute(atts, "scope", "CDATA", field.getScope());
        addAttribute(atts, "datatype", "CDATA", field.getDatatype());
        dh.startElement("", "", "occurrence", atts);
        dh.endElement("", "", "occurrence");
        atts.clear();
    } else if (field.getFieldType() == Field.TYPE_TOPIC_NAME) {
        addAttribute(atts, "column", "CDATA", field.getColumn());
        addAttribute(atts, "type", "CDATA", field.getType());
        addAttribute(atts, "scope", "CDATA", field.getScope());
        dh.startElement("", "", "topic-name", atts);
        dh.endElement("", "", "topic-name");
        atts.clear();
    } else if (field.getFieldType() == Field.TYPE_PLAYER) {
        addAttribute(atts, "rtype", "CDATA", field.getRoleType());
        addAttribute(atts, "atype", "CDATA", field.getAssociationType());
        addAttribute(atts, "scope", "CDATA", field.getScope());

        dh.startElement("", "", "player", atts);
        atts.clear();

        for (Field orole : field.getOtherRoleFields()) {
            addAttribute(atts, "rtype", "CDATA", orole.getRoleType());
            addAttribute(atts, "player", "CDATA", orole.getPlayer());
            dh.startElement("", "", "other", atts);
            dh.endElement("", "", "other");
            atts.clear();
        }

        dh.endElement("", "", "player");
        atts.clear();
    } else if (field.getFieldType() == Field.TYPE_ASSOCIATION_ROLE) {
        addAttribute(atts, "type", "CDATA", field.getRoleType());
        addAttribute(atts, "player", "CDATA", field.getPlayer());
        dh.startElement("", "", "role", atts);
        dh.endElement("", "", "role");
        atts.clear();
    } else
        throw new OntopiaRuntimeException("Unknown field type: " + field.getType());
}

From source file:net.ontopia.topicmaps.db2tm.RelationMapping.java

protected void write(ContentHandler dh) throws SAXException {

    // initialize attributes
    AttributesImpl atts = new AttributesImpl();

    // <db2tm name="...">
    if (name != null)
        addAttribute(atts, "name", "CDATA", name);

    dh.startDocument();/*from  w  ww  .ja va  2  s . c  o  m*/
    dh.startElement("", "", "db2tm", atts);
    atts.clear();

    // prefixes
    for (Prefix prefix : iprefixes.values()) {
        addAttribute(atts, "prefix", "CDATA", prefix.getId());

        switch (prefix.getType()) {
        case Prefix.TYPE_SUBJECT_IDENTIFIER:
            addAttribute(atts, "subject-identifier", "CDATA", prefix.getLocator());
            break;
        case Prefix.TYPE_ITEM_IDENTIFIER:
            addAttribute(atts, "item-identifier", "CDATA", prefix.getLocator());
            break;
        case Prefix.TYPE_SUBJECT_LOCATOR:
            addAttribute(atts, "subject-locator", "CDATA", prefix.getLocator());
            break;
        }

        dh.startElement("", "", "using", atts);
        atts.clear();
        dh.endElement("", "", "using");
    }

    // relations
    for (Relation rel : getRelations()) {
        // <relation>
        addAttribute(atts, "name", "CDATA", rel.getName());
        addAttribute(atts, "columns", "CDATA", rel.getColumns());
        dh.startElement("", "", "relation", atts);
        atts.clear();

        outputEntities(rel, dh);

        // </relation>
        dh.endElement("", "", "relation");
    }

    // </db2tm>
    dh.endElement("", "", "db2tm");
    dh.endDocument();
}