Example usage for javax.xml.transform.sax TransformerHandler characters

List of usage examples for javax.xml.transform.sax TransformerHandler characters

Introduction

In this page you can find the example usage for javax.xml.transform.sax TransformerHandler 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:Main.java

/**
 * Creates a {@link TransformerHandler}.
 * /*from   w w w  .ja  va 2s .c  o m*/
 * @param commentHeader the comment header
 * @param rootTag the root tag
 * @param streamResult stream result
 */
public static TransformerHandler createTransformerHandler(String commentHeader, String rootTag,
        StreamResult streamResult, Charset charset)
        throws TransformerFactoryConfigurationError, TransformerConfigurationException, SAXException {
    SAXTransformerFactory tf = (SAXTransformerFactory) TransformerFactory.newInstance();
    try {
        tf.setAttribute("indent-number", new Integer(2));
    } catch (Exception e) {
        // ignore, workaround for JDK 1.5 bug, see http://forum.java.sun.com/thread.jspa?threadID=562510
    }
    TransformerHandler transformerHandler = tf.newTransformerHandler();
    Transformer serializer = transformerHandler.getTransformer();
    serializer.setOutputProperty(OutputKeys.ENCODING, charset.name());
    serializer.setOutputProperty(OutputKeys.METHOD, "xml");
    serializer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformerHandler.setResult(streamResult);
    transformerHandler.startDocument();
    String newline = System.getProperty("line.separator");
    if (newline == null) {
        newline = "\n";
    }
    commentHeader = (newline + commentHeader).replaceAll("\\n--", newline + " ");
    transformerHandler.characters("\n".toCharArray(), 0, 1);
    transformerHandler.comment(commentHeader.toCharArray(), 0, commentHeader.toCharArray().length);
    transformerHandler.characters("\n".toCharArray(), 0, 1);
    if (rootTag.length() > 0) {
        transformerHandler.startElement("", "", rootTag, null);
    }
    return transformerHandler;
}

From source file:com.smartitengineering.exim.impl.xml.ResourceObjectExporter.java

public void exportElement(AssociationType type, Object object, TransformerHandler handler) throws IOException {
    if (type == null || object == null || handler == null) {
        throw new IOException("All parameters are required!");
    }/*from   www  . j av  a2 s. c o  m*/
    EximResourceConfig config = ConfigRegistrar.getConfigForClass(object.getClass());
    if (config == null) {
        try {
            handler.startCDATA();
            String value = object == null ? null : object.toString();
            if (StringUtils.isBlank(value)) {
                value = "";
            }
            char[] chars = value.toCharArray();
            handler.characters(chars, 0, chars.length);
            handler.endCDATA();
        } catch (Exception ex) {
            throw new IOException(ex);
        }
    } else {
        //Handle resources
    }
}

From source file:com.smartitengineering.exim.impl.xml.GenericDataTypeExporter.java

public void exportElement(AssociationType type, Object object, TransformerHandler handler) throws IOException {
    if (type == null || object == null || handler == null) {
        throw new IOException("All parameters are required!");
    }/*from w  w  w  .ja  v  a2  s  .  c  om*/
    final String elementName = type.getSimpleName();
    final String value;
    switch (type) {
    case TYPE_DATE:
        value = DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.format((Date) object);
        break;
    case TYPE_STRING:
    case TYPE_INTEGER:
    case TYPE_LONG:
    case TYPE_FLOAT:
    case TYPE_DOUBLE:
    case TYPE_BOOLEAN:
    default:
        value = object == null ? null : object.toString();
        break;
    }
    try {
        AttributesImpl atts = new AttributesImpl();
        handler.startElement(EXIM_COLLECN_URI, EXIM_COLLECTION_NS, elementName, atts);
        if (AssociationType.TYPE_STRING.equals(type)) {
            handler.startCDATA();
        }
        final char[] chars;
        if (StringUtils.isBlank(value)) {
            chars = "".toCharArray();
        } else {
            chars = value.toCharArray();
        }
        handler.characters(chars, 0, chars.length);
        if (AssociationType.TYPE_STRING.equals(type)) {
            handler.endCDATA();
        }
        handler.endElement(EXIM_COLLECN_URI, EXIM_COLLECTION_NS, elementName);
    } catch (Exception exception) {
        throw new IOException(exception);
    }
}

From source file:org.programmatori.domotica.own.plugin.map.Map.java

private void createStatusFile(String fileName) {
    StreamResult streamResult = new StreamResult(new File(fileName));

    SAXTransformerFactory tf = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
    TransformerHandler hd = null;
    try {/*w ww.j  ava  2  s .  c o  m*/
        hd = tf.newTransformerHandler();
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    }
    Transformer serializer = hd.getTransformer();

    serializer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
    //serializer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "users.dtd");
    serializer.setOutputProperty(OutputKeys.INDENT, "yes");
    //serializer.setOutputProperty( XalanOutputKeys.OUTPUT_PROP_INDENT_AMOUNT, "2" );

    hd.setResult(streamResult);
    try {
        hd.startDocument();

        AttributesImpl attrs = new AttributesImpl();
        hd.startElement("", "", "home", attrs);

        hd.startElement("", "", "version", attrs);
        hd.characters("2.0".toCharArray(), 0, 3);
        hd.endElement("", "", "version");

        attrs.clear();
        attrs.addAttribute("", "", "unit", "CDATA", "min");
        hd.startElement("", "", "statusSave", attrs);
        hd.characters("10".toCharArray(), 0, 2);
        hd.endElement("", "", "statusSave");

        // ----------------------------------------- Area
        for (Iterator<Integer> iterAree = localBus.keySet().iterator(); iterAree.hasNext();) {
            Integer area = (Integer) iterAree.next();

            attrs.clear();
            attrs.addAttribute("", "", "id", "CDATA", area.toString());
            attrs.addAttribute("", "", "name", "CDATA", Config.getInstance().getRoomName(area));
            hd.startElement("", "", "area", attrs);

            // ----------------------------------------- Component
            Set<SCSComponent> rooms = localBus.get(area);
            for (Iterator<SCSComponent> iterRoom = rooms.iterator(); iterRoom.hasNext();) {
                SCSComponent c = (SCSComponent) iterRoom.next();
                log.info("PL: " + c.getStatus().getWhere().getPL() + "("
                        + Config.getInstance().getWhoDescription(c.getStatus().getWho().getMain()) + ")");

                attrs.clear();
                attrs.addAttribute("", "", "type", "CDATA",
                        Config.getInstance().getWhoDescription(c.getStatus().getWho().getMain()));
                attrs.addAttribute("", "", "pl", "CDATA", "" + c.getStatus().getWhere().getPL());
                hd.startElement("", "", "component", attrs);
                hd.characters("0".toCharArray(), 0, 1);
                hd.endElement("", "", "component");
            }
            // ----------------------------------------- Component

            hd.endElement("", "", "area");
        }
        // ----------------------------------------- End Area

        // ----------------------------------------- Scheduler
        attrs.clear();
        hd.startElement("", "", "scheduler", attrs);

        attrs.clear();
        attrs.addAttribute("", "", "time", "CDATA", "-1");
        hd.startElement("", "", "command2", attrs);
        hd.characters("*1*1*11##".toCharArray(), 0, 9);
        hd.endElement("", "", "command2");

        hd.endElement("", "", "scheduler");
        // ----------------------------------------- End Scheduler

        hd.endElement("", "", "home");
        hd.endDocument();
    } catch (SAXException e) {
        e.printStackTrace();
    }

    //      for (Iterator<Integer> iterAree = localBus.keySet().iterator(); iterAree.hasNext();) {
    //         Integer area = (Integer) iterAree.next();
    //
    //         log.info("Room: " + area + " - " + Config.getInstance().getRoomName(area));
    //         Set<SCSComponent> rooms = localBus.get(area);
    //         for (Iterator<SCSComponent> iterRoom = rooms.iterator(); iterRoom.hasNext();) {
    //            SCSComponent c = (SCSComponent) iterRoom.next();
    //            log.info("PL: " + c.getStatus().getWhere().getPL() + "(" + Config.getInstance().getWhoDescription(c.getStatus().getWho().getMain()) + ")");
    //         }
    //      }
}