Example usage for org.xml.sax ContentHandler characters

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

Introduction

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

Prototype

public void characters(char ch[], int start, int length) throws SAXException;

Source Link

Document

Receive notification of character data.

Usage

From source file:org.apache.cocoon.forms.formmodel.Output.java

protected void generateItemSaxFragment(ContentHandler contentHandler, Locale locale) throws SAXException {
    // the value/*from   w  ww .  ja  v a  2 s . co m*/
    if (value != null) {
        contentHandler.startElement(FormsConstants.INSTANCE_NS, VALUE_EL,
                FormsConstants.INSTANCE_PREFIX_COLON + VALUE_EL, XMLUtils.EMPTY_ATTRIBUTES);
        String stringValue;
        stringValue = definition.getDatatype().convertToString(value, locale);
        contentHandler.characters(stringValue.toCharArray(), 0, stringValue.length());
        contentHandler.endElement(FormsConstants.INSTANCE_NS, VALUE_EL,
                FormsConstants.INSTANCE_PREFIX_COLON + VALUE_EL);
    }
}

From source file:org.apache.cocoon.forms.formmodel.Upload.java

public void generateItemSaxFragment(ContentHandler contentHandler, Locale locale) throws SAXException {
    if (this.part != null) {
        String name = (String) this.part.getHeaders().get("filename");
        contentHandler.startElement(FormsConstants.INSTANCE_NS, VALUE_EL,
                FormsConstants.INSTANCE_PREFIX_COLON + VALUE_EL, XMLUtils.EMPTY_ATTRIBUTES);
        contentHandler.characters(name.toCharArray(), 0, name.length());
        contentHandler.endElement(FormsConstants.INSTANCE_NS, VALUE_EL,
                FormsConstants.INSTANCE_PREFIX_COLON + VALUE_EL);
    }//  www.j  ava 2 s  .  c  o  m

    // validation message element: only present if the value is not valid
    if (this.validationError != null) {
        contentHandler.startElement(FormsConstants.INSTANCE_NS, VALIDATION_MSG_EL,
                FormsConstants.INSTANCE_PREFIX_COLON + VALIDATION_MSG_EL, XMLUtils.EMPTY_ATTRIBUTES);
        this.validationError.generateSaxFragment(contentHandler);
        contentHandler.endElement(FormsConstants.INSTANCE_NS, VALIDATION_MSG_EL,
                FormsConstants.INSTANCE_PREFIX_COLON + VALIDATION_MSG_EL);
    }
}

From source file:org.apache.cocoon.forms.util.I18nMessage.java

public void toSAX(ContentHandler contentHandler) throws SAXException {
    contentHandler.startPrefixMapping("i18n", I18nTransformer.I18N_NAMESPACE_URI);
    if (parameters != null) {
        contentHandler.startElement(I18nTransformer.I18N_NAMESPACE_URI, I18nTransformer.I18N_TRANSLATE_ELEMENT,
                "i18n:" + I18nTransformer.I18N_TRANSLATE_ELEMENT, XMLUtils.EMPTY_ATTRIBUTES);
    }//from w w  w.  jav a  2  s .co  m

    AttributesImpl i18nAttrs = new AttributesImpl();
    if (catalogue != null) {
        i18nAttrs.addCDATAAttribute(I18nTransformer.I18N_NAMESPACE_URI,
                I18nTransformer.I18N_CATALOGUE_ATTRIBUTE, "i18n:" + I18nTransformer.I18N_CATALOGUE_ATTRIBUTE,
                catalogue);
    }

    contentHandler.startElement(I18nTransformer.I18N_NAMESPACE_URI, I18nTransformer.I18N_TEXT_ELEMENT,
            "i18n:" + I18nTransformer.I18N_TEXT_ELEMENT, i18nAttrs);
    contentHandler.characters(key.toCharArray(), 0, key.length());
    contentHandler.endElement(I18nTransformer.I18N_NAMESPACE_URI, I18nTransformer.I18N_TEXT_ELEMENT,
            "i18n:" + I18nTransformer.I18N_TEXT_ELEMENT);

    // the parameters
    if (parameters != null) {
        for (int i = 0; i < parameters.length; i++) {
            contentHandler.startElement(I18nTransformer.I18N_NAMESPACE_URI, I18nTransformer.I18N_PARAM_ELEMENT,
                    "i18n:" + I18nTransformer.I18N_PARAM_ELEMENT, XMLUtils.EMPTY_ATTRIBUTES);
            if (keys != null && keys[i]) {
                contentHandler.startElement(I18nTransformer.I18N_NAMESPACE_URI,
                        I18nTransformer.I18N_TEXT_ELEMENT, "i18n:" + I18nTransformer.I18N_TEXT_ELEMENT,
                        i18nAttrs);
            }
            final String aParam = String.valueOf(parameters[i]);
            contentHandler.characters(aParam.toCharArray(), 0, aParam.length());
            if (keys != null && keys[i]) {
                contentHandler.endElement(I18nTransformer.I18N_NAMESPACE_URI, I18nTransformer.I18N_TEXT_ELEMENT,
                        "i18n:" + I18nTransformer.I18N_TEXT_ELEMENT);
            }
            contentHandler.endElement(I18nTransformer.I18N_NAMESPACE_URI, I18nTransformer.I18N_PARAM_ELEMENT,
                    "i18n:" + I18nTransformer.I18N_PARAM_ELEMENT);
        }
        contentHandler.endElement(I18nTransformer.I18N_NAMESPACE_URI, I18nTransformer.I18N_TRANSLATE_ELEMENT,
                "i18n:" + I18nTransformer.I18N_TRANSLATE_ELEMENT);
    }
    contentHandler.endPrefixMapping("i18n");
}

From source file:org.apache.cocoon.generation.ExceptionGenerator.java

private static void simpleElement(String name, Attributes attr, String value, ContentHandler handler)
        throws SAXException {
    handler.startElement(EXCEPTION_NS, name, "ex:" + name, attr);
    if (value != null && value.length() > 0) {
        handler.characters(value.toCharArray(), 0, value.length());
    }/*from  w w w.jav  a2 s .c  om*/
    handler.endElement(EXCEPTION_NS, name, "ex:" + name);
}

From source file:org.apache.cocoon.webapps.session.context.RequestSessionContext.java

/**
 * Stream the XML directly to the handler. This streams the contents of getXML()
 * to the given handler without creating a DocumentFragment containing a copy
 * of the data//w  ww.java2s. c  o m
 */
public boolean streamXML(String path, ContentHandler contentHandler, LexicalHandler lexicalHandler)
        throws SAXException, ProcessingException {
    boolean result = false;
    NodeList list;

    try {
        list = DOMUtil.selectNodeList(this.contextData, this.createPath(path), this.xpathProcessor);
    } catch (javax.xml.transform.TransformerException local) {
        throw new ProcessingException("TransformerException: " + local, local);
    }
    if (list != null && list.getLength() > 0) {
        result = true;
        for (int i = 0; i < list.getLength(); i++) {

            // the found node is either an attribute or an element
            if (list.item(i).getNodeType() == Node.ATTRIBUTE_NODE) {
                // if it is an attribute simple create a new text node with the value of the attribute
                String value = list.item(i).getNodeValue();
                contentHandler.characters(value.toCharArray(), 0, value.length());
            } else {
                // now we have an element
                // stream all children of this element to the resulting tree
                NodeList childs = list.item(i).getChildNodes();
                if (childs != null) {
                    for (int m = 0; m < childs.getLength(); m++) {
                        IncludeXMLConsumer.includeNode(childs.item(m), contentHandler, lexicalHandler);
                    }
                }
            }
        }
    }

    return result;
}

From source file:org.apache.cocoon.woody.datatype.FlowJXPathSelectionList.java

public void generateSaxFragment(ContentHandler contentHandler, Locale locale) throws SAXException {
    JXPathContext ctx = null;/*  www. jav  a2s  .  co  m*/
    Iterator iter = null;
    if (model == null) {
        Object flowData = FlowHelper.getContextObject(ContextHelper.getObjectModel(this.context));
        if (flowData == null) {
            throw new SAXException("No flow data to produce selection list");
        }

        // Move to the list location
        ctx = JXPathContext.newContext(flowData);

        // Iterate on all elements of the list
        iter = ctx.iteratePointers(this.listPath);
    } else {
        // Move to the list location
        ctx = JXPathContext.newContext(model);

        // Iterate on all elements of the list
        iter = ctx.iteratePointers(".");
    }

    // Start the selection-list
    contentHandler.startElement(Constants.WI_NS, SELECTION_LIST_EL,
            Constants.WI_PREFIX_COLON + SELECTION_LIST_EL, Constants.EMPTY_ATTRS);

    while (iter.hasNext()) {
        String stringValue = "";
        Object label = null;

        // Get a context on the current item
        Pointer ptr = (Pointer) iter.next();
        if (ptr.getValue() != null) {
            JXPathContext itemCtx = ctx.getRelativeContext(ptr);

            // Get the value as a string
            Object value = itemCtx.getValue(this.valuePath);

            // List may contain null value, and (per contract with convertors),
            // convertors are not invoked on nulls.
            if (value != null) {
                stringValue = this.datatype.convertToString(value, locale);
            }

            // Get the label (can be ommitted)
            itemCtx.setLenient(true);
            label = itemCtx.getValue(this.labelPath);
            if (label == null) {
                label = stringValue;
            }
        }

        // Output this item
        AttributesImpl itemAttrs = new AttributesImpl();
        itemAttrs.addCDATAAttribute("value", stringValue);
        contentHandler.startElement(Constants.WI_NS, ITEM_EL, Constants.WI_PREFIX_COLON + ITEM_EL, itemAttrs);
        if (label != null) {
            contentHandler.startElement(Constants.WI_NS, LABEL_EL, Constants.WI_PREFIX_COLON + LABEL_EL,
                    Constants.EMPTY_ATTRS);
            if (label instanceof XMLizable) {
                ((XMLizable) label).toSAX(contentHandler);
            } else {
                String stringLabel = label.toString();
                contentHandler.characters(stringLabel.toCharArray(), 0, stringLabel.length());
            }
            contentHandler.endElement(Constants.WI_NS, LABEL_EL, Constants.WI_PREFIX_COLON + LABEL_EL);
        }
        contentHandler.endElement(Constants.WI_NS, ITEM_EL, Constants.WI_PREFIX_COLON + ITEM_EL);
    }

    // End the selection-list
    contentHandler.endElement(Constants.WI_NS, SELECTION_LIST_EL,
            Constants.WI_PREFIX_COLON + SELECTION_LIST_EL);
}

From source file:org.apache.syncope.core.logic.report.AuditReportlet.java

private void doExtractConf(final ContentHandler handler) throws SAXException {
    JdbcTemplate jdbcTemplate = new JdbcTemplate(datasource);
    jdbcTemplate.setMaxRows(conf.getSize());
    List<Map<String, Object>> rows = jdbcTemplate
            .queryForList("SELECT * FROM SYNCOPEAUDIT ORDER BY EVENT_DATE DESC");

    handler.startElement("", "", "events", null);
    AttributesImpl atts = new AttributesImpl();
    for (Map<String, Object> row : rows) {
        AuditEntry auditEntry = POJOHelper.deserialize(row.get("MESSAGE").toString(), AuditEntry.class);

        atts.clear();/*  w  ww . j  av a2 s.  co  m*/
        if (StringUtils.isNotBlank(auditEntry.getWho())) {
            atts.addAttribute("", "", "who", ReportXMLConst.XSD_STRING, auditEntry.getWho());
        }
        handler.startElement("", "", "event", atts);

        atts.clear();
        if (StringUtils.isNotBlank(auditEntry.getLogger().getCategory())) {
            atts.addAttribute("", "", "category", ReportXMLConst.XSD_STRING,
                    auditEntry.getLogger().getCategory());
        }
        if (StringUtils.isNotBlank(auditEntry.getLogger().getSubcategory())) {
            atts.addAttribute("", "", "subcategory", ReportXMLConst.XSD_STRING,
                    auditEntry.getLogger().getSubcategory());
        }
        if (StringUtils.isNotBlank(auditEntry.getLogger().getEvent())) {
            atts.addAttribute("", "", "event", ReportXMLConst.XSD_STRING, auditEntry.getLogger().getEvent());
        }
        if (auditEntry.getLogger().getResult() != null) {
            atts.addAttribute("", "", "result", ReportXMLConst.XSD_STRING,
                    auditEntry.getLogger().getResult().name());
        }
        handler.startElement("", "", "logger", atts);
        handler.endElement("", "", "logger");

        if (auditEntry.getBefore() != null) {
            char[] before = ToStringBuilder
                    .reflectionToString(auditEntry.getBefore(), ToStringStyle.MULTI_LINE_STYLE).toCharArray();
            handler.startElement("", "", "before", null);
            handler.characters(before, 0, before.length);
            handler.endElement("", "", "before");
        }

        if (auditEntry.getInput() != null) {
            handler.startElement("", "", "inputs", null);
            for (Object inputObj : auditEntry.getInput()) {
                char[] input = ToStringBuilder.reflectionToString(inputObj, ToStringStyle.MULTI_LINE_STYLE)
                        .toCharArray();
                handler.startElement("", "", "input", null);
                handler.characters(input, 0, input.length);
                handler.endElement("", "", "input");
            }
            handler.endElement("", "", "inputs");
        }

        if (auditEntry.getOutput() != null) {
            char[] output = ToStringBuilder
                    .reflectionToString(auditEntry.getOutput(), ToStringStyle.MULTI_LINE_STYLE).toCharArray();
            handler.startElement("", "", "output", null);
            handler.characters(output, 0, output.length);
            handler.endElement("", "", "output");
        }

        handler.startElement("", "", "throwable", null);
        char[] throwable = row.get("THROWABLE").toString().toCharArray();
        handler.characters(throwable, 0, throwable.length);
        handler.endElement("", "", "throwable");

        handler.endElement("", "", "event");
    }
    handler.endElement("", "", "events");
}

From source file:org.apache.syncope.core.logic.report.GroupReportlet.java

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

    AttributesImpl atts = new AttributesImpl();
    if (!attrs.isEmpty()) {
        Map<String, AttrTO> attrMap = anyTO.getPlainAttrMap();

        handler.startElement("", "", "attributes", null);
        for (String attrName : attrs) {
            atts.clear();/*from ww  w. ja  v a2 s  .  c om*/

            atts.addAttribute("", "", ReportXMLConst.ATTR_NAME, ReportXMLConst.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 {}[{}]", attrName, anyTO.getClass().getSimpleName(),
                        anyTO.getKey());
            }

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

    if (!derAttrs.isEmpty()) {
        Map<String, AttrTO> derAttrMap = anyTO.getDerAttrMap();

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

            atts.addAttribute("", "", ReportXMLConst.ATTR_NAME, ReportXMLConst.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 {}[{}]", attrName, anyTO.getClass().getSimpleName(),
                        anyTO.getKey());
            }

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

    if (!virAttrs.isEmpty()) {
        Map<String, AttrTO> virAttrMap = anyTO.getVirAttrMap();

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

            atts.addAttribute("", "", ReportXMLConst.ATTR_NAME, ReportXMLConst.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 {}[{}]", attrName, anyTO.getClass().getSimpleName(),
                        anyTO.getKey());
            }

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

From source file:org.apache.syncope.core.logic.report.GroupReportlet.java

private void doExtractConf(final ContentHandler handler) throws SAXException {
    if (conf == null) {
        LOG.debug("Report configuration is not present");
    }/* w  w w.  j ava 2  s.  com*/

    AttributesImpl atts = new AttributesImpl();
    handler.startElement("", "", "configurations", null);
    handler.startElement("", "", "groupAttributes", atts);

    for (Feature feature : conf.getFeatures()) {
        atts.clear();
        handler.startElement("", "", "feature", atts);
        handler.characters(feature.name().toCharArray(), 0, feature.name().length());
        handler.endElement("", "", "feature");
    }

    for (String attr : conf.getPlainAttrs()) {
        atts.clear();
        handler.startElement("", "", "attribute", atts);
        handler.characters(attr.toCharArray(), 0, attr.length());
        handler.endElement("", "", "attribute");
    }

    for (String derAttr : conf.getDerAttrs()) {
        atts.clear();
        handler.startElement("", "", "derAttribute", atts);
        handler.characters(derAttr.toCharArray(), 0, derAttr.length());
        handler.endElement("", "", "derAttribute");
    }

    for (String virAttr : conf.getVirAttrs()) {
        atts.clear();
        handler.startElement("", "", "virAttribute", atts);
        handler.characters(virAttr.toCharArray(), 0, virAttr.length());
        handler.endElement("", "", "virAttribute");
    }

    handler.endElement("", "", "groupAttributes");
    handler.endElement("", "", "configurations");
}

From source file:org.apache.syncope.core.logic.report.ReconciliationReportlet.java

private void doExtract(final ContentHandler handler, final Any<?> any, final Set<Missing> missing,
        final Set<Misaligned> misaligned) throws SAXException {

    AttributesImpl atts = new AttributesImpl();

    for (Feature feature : conf.getFeatures()) {
        String type = null;// w ww  . j  a  va  2 s .c om
        String value = null;
        switch (feature) {
        case key:
            type = ReportXMLConst.XSD_STRING;
            value = any.getKey();
            break;

        case username:
            if (any instanceof User) {
                type = ReportXMLConst.XSD_STRING;
                value = User.class.cast(any).getUsername();
            }
            break;

        case groupName:
            if (any instanceof Group) {
                type = ReportXMLConst.XSD_STRING;
                value = Group.class.cast(any).getName();
            }
            break;

        case workflowId:
            type = ReportXMLConst.XSD_STRING;
            value = any.getWorkflowId();
            break;

        case status:
            type = ReportXMLConst.XSD_STRING;
            value = any.getStatus();
            break;

        case creationDate:
            type = ReportXMLConst.XSD_DATETIME;
            value = any.getCreationDate() == null ? StringUtils.EMPTY
                    : FormatUtils.format(any.getCreationDate());
            break;

        case lastLoginDate:
            if (any instanceof User) {
                type = ReportXMLConst.XSD_DATETIME;
                value = User.class.cast(any).getLastLoginDate() == null ? StringUtils.EMPTY
                        : FormatUtils.format(User.class.cast(any).getLastLoginDate());
            }
            break;

        case changePwdDate:
            if (any instanceof User) {
                type = ReportXMLConst.XSD_DATETIME;
                value = User.class.cast(any).getChangePwdDate() == null ? StringUtils.EMPTY
                        : FormatUtils.format(User.class.cast(any).getChangePwdDate());
            }
            break;

        case passwordHistorySize:
            if (any instanceof User) {
                type = ReportXMLConst.XSD_INT;
                value = String.valueOf(User.class.cast(any).getPasswordHistory().size());
            }
            break;

        case failedLoginCount:
            if (any instanceof User) {
                type = ReportXMLConst.XSD_INT;
                value = String.valueOf(User.class.cast(any).getFailedLogins());
            }
            break;

        default:
        }

        if (type != null && value != null) {
            atts.addAttribute("", "", feature.name(), type, value);
        }
    }

    handler.startElement("", "", getAnyElementName(any.getType().getKind()), atts);

    for (Missing item : missing) {
        atts.clear();
        atts.addAttribute("", "", "resource", ReportXMLConst.XSD_STRING, item.getResource());
        atts.addAttribute("", "", "connObjectKeyValue", ReportXMLConst.XSD_STRING,
                item.getConnObjectKeyValue());

        handler.startElement("", "", "missing", atts);
        handler.endElement("", "", "missing");
    }
    for (Misaligned item : misaligned) {
        atts.clear();
        atts.addAttribute("", "", "resource", ReportXMLConst.XSD_STRING, item.getResource());
        atts.addAttribute("", "", "connObjectKeyValue", ReportXMLConst.XSD_STRING,
                item.getConnObjectKeyValue());
        atts.addAttribute("", "", ReportXMLConst.ATTR_NAME, ReportXMLConst.XSD_STRING, item.getName());

        handler.startElement("", "", "misaligned", atts);

        handler.startElement("", "", "onSyncope", null);
        if (item.getOnSyncope() != null) {
            for (Object value : item.getOnSyncope()) {
                char[] asChars = value.toString().toCharArray();

                handler.startElement("", "", "value", null);
                handler.characters(asChars, 0, asChars.length);
                handler.endElement("", "", "value");
            }
        }
        handler.endElement("", "", "onSyncope");

        handler.startElement("", "", "onResource", null);
        if (item.getOnResource() != null) {
            for (Object value : item.getOnResource()) {
                char[] asChars = value.toString().toCharArray();

                handler.startElement("", "", "value", null);
                handler.characters(asChars, 0, asChars.length);
                handler.endElement("", "", "value");
            }
        }
        handler.endElement("", "", "onResource");

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

    handler.endElement("", "", getAnyElementName(any.getType().getKind()));
}