Example usage for javax.xml.stream XMLStreamWriter writeCharacters

List of usage examples for javax.xml.stream XMLStreamWriter writeCharacters

Introduction

In this page you can find the example usage for javax.xml.stream XMLStreamWriter writeCharacters.

Prototype

public void writeCharacters(String text) throws XMLStreamException;

Source Link

Document

Write text to the output

Usage

From source file:org.unitedinternet.cosmo.model.text.XhtmlSubscriptionFormat.java

private String format(CollectionSubscription sub, boolean isCollectionProvided, CollectionItem collection,
        boolean isTicketProvided, Ticket ticket) {
    try {//from ww w  .  j av a  2 s  .co m
        StringWriter sw = new StringWriter();
        XMLStreamWriter writer = createXmlWriter(sw);

        writer.writeStartElement("div");
        writer.writeAttribute("class", "local-subscription");

        if (sub.getDisplayName() != null) {
            writer.writeCharacters("Subscription: ");
            writer.writeStartElement("span");
            writer.writeAttribute("class", "name");
            writer.writeCharacters(sub.getDisplayName());
            writer.writeEndElement();
        }

        if (sub.getCollectionUid() != null) {
            writer.writeStartElement("div");
            writer.writeAttribute("class", "collection");
            writer.writeCharacters("Collection: ");
            writer.writeStartElement("span");
            writer.writeAttribute("class", "uuid");
            writer.writeCharacters(sub.getCollectionUid());
            writer.writeEndElement();
            if (isCollectionProvided) {
                writer.writeCharacters(" Exists? ");
                writer.writeStartElement("span");
                writer.writeAttribute("class", "exists");
                writer.writeCharacters(Boolean.valueOf(collection != null).toString());
                writer.writeEndElement();
            }
            writer.writeEndElement();
        }

        if (sub.getTicketKey() != null) {
            writer.writeStartElement("div");
            writer.writeAttribute("class", "ticket");
            writer.writeCharacters("Ticket: ");
            writer.writeStartElement("span");
            writer.writeAttribute("class", "key");
            writer.writeCharacters(sub.getTicketKey());
            writer.writeEndElement();
            if (isTicketProvided) {
                writer.writeCharacters(" Exists? ");
                writer.writeStartElement("span");
                writer.writeAttribute("class", "exists");
                writer.writeCharacters(Boolean.valueOf(ticket != null).toString());
                writer.writeEndElement();
            }
            writer.writeEndElement();
        }

        writer.writeEndElement();
        writer.close();

        return sw.toString();
    } catch (XMLStreamException e) {
        throw new CosmoXMLStreamException("Error formatting XML", e);
    }
}

From source file:org.unitedinternet.cosmo.model.text.XhtmlTicketFormat.java

public String format(Ticket ticket) {
    try {//  w ww  .  j  a v  a2s . c o  m
        StringWriter sw = new StringWriter();
        XMLStreamWriter writer = createXmlWriter(sw);

        writer.writeStartElement("div");
        writer.writeAttribute("class", "ticket");

        if (ticket.getKey() != null) {
            writer.writeCharacters("Key: ");
            writer.writeStartElement("span");
            writer.writeAttribute("class", "key");
            writer.writeCharacters(ticket.getKey());
            writer.writeEndElement();
        }

        if (ticket.getType() != null) {
            writer.writeCharacters("Type: ");
            writer.writeStartElement("span");
            writer.writeAttribute("class", "type");
            writer.writeAttribute("title", ticket.getType().toString());
            writer.writeCharacters(ticket.getType().toString());
            writer.writeEndElement();
        }

        if (ticket.getTimeout() != null) {
            writer.writeCharacters("Timeout: ");
            writer.writeStartElement("span");
            writer.writeAttribute("class", "timeout");
            writer.writeAttribute("title", ticket.getTimeout());
            writer.writeCharacters(ticket.getTimeout());
            writer.writeEndElement();
        }

        writer.writeEndElement();
        writer.close();

        return sw.toString();
    } catch (XMLStreamException e) {
        throw new CosmoXMLStreamException("Error formatting XML", e);
    }
}

From source file:org.vanbest.xmltv.Horizon.java

/**
 * @param args/*from  ww w.  j a v a 2s .c  o  m*/
 */
public static void main(String[] args) {
    Config config = Config.getDefaultConfig();
    Horizon horizon = new Horizon(config);
    horizon.clearCache();
    try {
        List<Channel> channels = horizon.getChannels();
        System.out.println("Channels: " + channels);
        XMLStreamWriter writer = XMLOutputFactory.newInstance()
                .createXMLStreamWriter(new FileWriter("horizon.xml"));
        writer.writeStartDocument();
        writer.writeCharacters("\n");
        writer.writeDTD("<!DOCTYPE tv SYSTEM \"xmltv.dtd\">");
        writer.writeCharacters("\n");
        writer.writeStartElement("tv");
        // List<Channel> my_channels = channels;
        List<Channel> my_channels = channels.subList(0, 5);
        for (Channel c : my_channels) {
            c.serialize(writer, true);
        }
        writer.flush();
        for (int day = 0; day < 5; day++) {
            List<Programme> programmes = horizon.getProgrammes(my_channels, day);
            for (Programme p : programmes) {
                p.serialize(writer);
            }
        }
        writer.writeEndElement();
        writer.writeEndDocument();
        writer.flush();
        if (!config.quiet) {
            EPGSource.Stats stats = horizon.getStats();
            System.out.println("Number of programmes from cache: " + stats.cacheHits);
            System.out.println("Number of programmes fetched: " + stats.cacheMisses);
            System.out.println("Number of fetch errors: " + stats.fetchErrors);
        }
        horizon.close();
    } catch (Exception e) {
        logger.error("Error in horizon testing", e);
    }
}

From source file:org.vanbest.xmltv.Main.java

public void fetchData() throws FactoryConfigurationError, Exception {
    if (!config.quiet) {
        showHeader();//from   www .  ja  v a 2 s . com
        logger.info("Fetching programme data for " + this.days + " days starting from day " + this.offset);
        int enabledCount = 0;
        for (Channel c : config.channels) {
            if (c.enabled)
                enabledCount++;
        }
        logger.info("... from " + enabledCount + " channels");
        logger.info("... using cache at " + config.cacheDbHandle);
    }
    if (clearCache) {
        ProgrammeCache cache = new ProgrammeCache(config);
        cache.clear();
        cache.close();
    }
    Map<String, EPGSource> guides = new HashMap<String, EPGSource>();
    EPGSourceFactory factory = EPGSourceFactory.newInstance();
    // EPGSource gids = new TvGids(config);
    // if (clearCache) gids.clearCache();

    XMLStreamWriter writer = XMLOutputFactory.newInstance().createXMLStreamWriter(outputWriter);
    writer.writeStartDocument();
    writer.writeCharacters("\n");
    writer.writeDTD("<!DOCTYPE tv SYSTEM \"xmltv.dtd\">");
    writer.writeCharacters("\n");
    writer.writeStartElement("tv");
    writer.writeAttribute("generator-info-url", "http://github.com/janpascal/tv_grab_nl_java");
    writer.writeAttribute("source-info-url", "http://tvgids.nl/");
    writer.writeAttribute("source-info-name", "TvGids.nl");
    writer.writeAttribute("generator-info-name",
            "tv_grab_nl_java release " + config.project_version + ", built " + config.build_time);
    writer.writeCharacters(System.getProperty("line.separator"));

    for (Channel c : config.channels)
        if (c.enabled)
            c.serialize(writer, config.fetchLogos);

    for (int day = offset; day < offset + days; day++) {
        if (!config.quiet)
            System.out.print("Fetching information for day " + day);
        for (Channel c : config.channels) {
            if (!c.enabled)
                continue;
            if (!config.quiet)
                System.out.print(".");
            if (!guides.containsKey(c.source)) {
                guides.put(c.source, factory.createEPGSource(c.source, config));
            }
            List<Programme> programmes = guides.get(c.source).getProgrammes(c, day);
            for (Programme p : programmes)
                p.serialize(writer);
            writer.flush();
        }
        if (!config.quiet)
            System.out.println();
    }

    writer.writeEndElement();
    writer.writeEndDocument();
    writer.flush();
    writer.close();

    for (String source : guides.keySet()) {
        guides.get(source).close();
    }

    if (!config.quiet) {
        EPGSource.Stats stats = new EPGSource.Stats();
        for (String source : guides.keySet()) {
            EPGSource.Stats part = guides.get(source).getStats();
            stats.cacheHits += part.cacheHits;
            stats.cacheMisses += part.cacheMisses;
            stats.fetchErrors += part.fetchErrors;
        }
        logger.info("Number of programmes from cache: " + stats.cacheHits);
        logger.info("Number of programmes fetched: " + stats.cacheMisses);
        logger.warn("Number of fetch errors: " + stats.fetchErrors);
    }
}

From source file:org.vanbest.xmltv.RTL.java

/**
 * @param args//from  w  ww.  j ava  2  s.  c o m
 * @throws FileNotFoundException
 */
public static void main(String[] args) throws FileNotFoundException {
    debug = true;
    Logger.getRootLogger().setLevel(Level.TRACE);

    Config config = Config.getDefaultConfig();
    config.niceMilliseconds = 50;
    RTL rtl = new RTL(config);
    if (debug) {
        rtl.cache.clear();
        logger.info("Writing CSV to rtl.csv");
        rtl.debugWriter = new PrintWriter(new BufferedOutputStream(new FileOutputStream("rtl.csv")));
        rtl.debugWriter.print("\"zender\",\"starttime\",\"title\",\"quark1\",\"quark2\",");
        /*
        for (int k = 0; k < rtl.xmlKeys.length; k++) {
                rtl.debugWriter.print(rtl.xmlKeys[k]);
                rtl.debugWriter.print(",");
        }
        */
        rtl.debugWriter.println();
    }

    try {
        List<Channel> channels = rtl.getChannels();
        logger.info("Channels: " + channels);
        XMLStreamWriter writer = XMLOutputFactory.newInstance()
                .createXMLStreamWriter(new FileWriter("rtl.xml"));
        writer.writeStartDocument();
        writer.writeCharacters("\n");
        writer.writeDTD("<!DOCTYPE tv SYSTEM \"xmltv.dtd\">");
        writer.writeCharacters("\n");
        writer.writeStartElement("tv");
        for (Channel c : channels) {
            c.serialize(writer, true);
        }
        writer.flush();
        // List<Programme> programmes =
        // rtl.getProgrammes(channels.subList(6, 9), 0);
        for (int day = 0; day < 10; day++) {
            List<Programme> programmes = rtl.getProgrammes(channels, day);
            for (Programme p : programmes) {
                p.serialize(writer);
            }
        }
        writer.writeEndElement();
        writer.writeEndDocument();
        writer.flush();
        if (!config.quiet) {
            EPGSource.Stats stats = rtl.getStats();
            logger.info("Number of programmes from cache: " + stats.cacheHits);
            logger.info("Number of programmes fetched: " + stats.cacheMisses);
            logger.info("Number of fetch errors: " + stats.fetchErrors);
        }
        if (debug) {
            rtl.debugWriter.flush();
            rtl.debugWriter.close();
        }
        rtl.close();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        logger.debug("Exception in RTL.main()", e);
    }
}

From source file:org.vanbest.xmltv.TvGids.java

/**
 * @param args//from   w  w w. ja v  a 2s. c o  m
 */
public static void main(String[] args) {
    Config config = Config.getDefaultConfig();
    TvGids gids = new TvGids(config);
    try {
        List<Channel> channels = gids.getChannels();
        System.out.println("Channels: " + channels);
        XMLStreamWriter writer = XMLOutputFactory.newInstance()
                .createXMLStreamWriter(new FileWriter("tvgids.xml"));
        writer.writeStartDocument();
        writer.writeCharacters("\n");
        writer.writeDTD("<!DOCTYPE tv SYSTEM \"xmltv.dtd\">");
        writer.writeCharacters("\n");
        writer.writeStartElement("tv");
        // List<Channel> my_channels = channels;
        List<Channel> my_channels = channels.subList(0, 15);
        for (Channel c : my_channels) {
            c.serialize(writer, true);
        }
        writer.flush();
        List<Programme> programmes = gids.getProgrammes(my_channels, 2);
        for (Programme p : programmes) {
            p.serialize(writer);
        }
        writer.writeEndElement();
        writer.writeEndDocument();
        writer.flush();
        if (!config.quiet) {
            EPGSource.Stats stats = gids.getStats();
            System.out.println("Number of programmes from cache: " + stats.cacheHits);
            System.out.println("Number of programmes fetched: " + stats.cacheMisses);
            System.out.println("Number of fetch errors: " + stats.fetchErrors);
        }
        gids.close();
    } catch (Exception e) {
        logger.error("Error in tvgids testing", e);
    }
}

From source file:org.vanbest.xmltv.ZiggoGids.java

/**
 * @param args/*from  w  w  w .  ja va 2 s. com*/
 */
public static void main(String[] args) {
    Config config = Config.getDefaultConfig();
    logger.setLevel(Level.TRACE);
    ZiggoGids gids = new ZiggoGids(config);
    try {
        List<Channel> channels = gids.getChannels();
        System.out.println("Channels: " + channels);

        XMLStreamWriter writer = XMLOutputFactory.newInstance()
                .createXMLStreamWriter(new FileWriter("ziggogids.xml"));
        writer.writeStartDocument();
        writer.writeCharacters("\n");
        writer.writeDTD("<!DOCTYPE tv SYSTEM \"xmltv.dtd\">");
        writer.writeCharacters("\n");
        writer.writeStartElement("tv");
        //List<Channel> my_channels = channels;
        //List<Channel> my_channels = channels.subList(0, 15);
        List<Channel> my_channels = channels.subList(0, 4);
        for (Channel c : my_channels) {
            c.serialize(writer, true);
        }
        writer.flush();
        List<Programme> programmes = gids.getProgrammes(my_channels, 2);
        for (Programme p : programmes) {
            p.serialize(writer);
        }
        writer.writeEndElement();
        writer.writeEndDocument();
        writer.flush();
        if (!config.quiet) {
            EPGSource.Stats stats = gids.getStats();
            System.out.println("Number of programmes from cache: " + stats.cacheHits);
            System.out.println("Number of programmes fetched: " + stats.cacheMisses);
            System.out.println("Number of fetch errors: " + stats.fetchErrors);
        }

        gids.close();
    } catch (Exception e) {
        logger.error("Error in ziggogids testing", e);
    }
}

From source file:org.webguitoolkit.ui.util.export.XMLTableExport.java

protected void writeObject(XMLStreamWriter sw, Object obj) throws XMLStreamException {
    sw.writeCharacters(obj.toString());
}

From source file:org.wso2.carbon.repository.core.ResourceStorer.java

private void dumpRecursively(String path, XMLStreamWriter xmlWriter, Writer writer)
        throws RepositoryException, XMLStreamException {
    // adding resource meta data
    ResourceImpl resource = resourceDAO.getResourceMetaData(path);

    if (resource == null) {
        return;//from  ww w  .j a  v  a  2s  . co m
    }

    xmlWriter.writeStartElement(DumpConstants.RESOURCE);

    // adding path as an attribute, updated dump has name instead of path
    xmlWriter.writeAttribute(DumpConstants.RESOURCE_NAME, RepositoryUtils.getResourceName(path));

    //adding dump attribute
    xmlWriter.writeAttribute(DumpConstants.RESOURCE_STATUS, DumpConstants.RESOURCE_DUMP);

    // adding isCollection as an attribute
    xmlWriter.writeAttribute(DumpConstants.RESOURCE_IS_COLLECTION,
            (resource instanceof CollectionImpl) ? DumpConstants.RESOURCE_IS_COLLECTION_TRUE
                    : DumpConstants.RESOURCE_IS_COLLECTION_FALSE);

    // set media type
    String mediaType = resource.getMediaType();

    xmlWriter.writeStartElement(DumpConstants.MEDIA_TYPE);
    xmlWriter.writeCharacters(mediaType != null ? mediaType : "");
    xmlWriter.writeEndElement();

    // set version
    long version = resource.getVersionNumber();

    xmlWriter.writeStartElement(DumpConstants.VERSION);
    xmlWriter.writeCharacters(Long.toString(version) != null ? Long.toString(version) : "");
    xmlWriter.writeEndElement();

    // set creator
    String creator = resource.getAuthorUserName();

    xmlWriter.writeStartElement(DumpConstants.CREATOR);
    xmlWriter.writeCharacters(creator != null ? creator : "");
    xmlWriter.writeEndElement();

    // set createdTime
    Date createdTime = resource.getCreatedTime();

    xmlWriter.writeStartElement(DumpConstants.CREATED_TIME);
    xmlWriter.writeCharacters(
            Long.toString(createdTime.getTime()) != null ? Long.toString(createdTime.getTime()) : "");
    xmlWriter.writeEndElement();

    // set updater
    String updater = resource.getLastUpdaterUserName();

    xmlWriter.writeStartElement(DumpConstants.LAST_UPDATER);
    xmlWriter.writeCharacters(updater != null ? updater : "");
    xmlWriter.writeEndElement();

    // set LastModified
    Date lastModified = resource.getLastModified();

    xmlWriter.writeStartElement(DumpConstants.LAST_MODIFIED);
    xmlWriter.writeCharacters(
            Long.toString(lastModified.getTime()) != null ? Long.toString(lastModified.getTime()) : "");
    xmlWriter.writeEndElement();

    // set UUID
    String uuid = resource.getUUID();

    xmlWriter.writeStartElement(DumpConstants.UUID);
    xmlWriter.writeCharacters(uuid != null ? uuid : "");
    xmlWriter.writeEndElement();

    // set Description
    String description = resource.getDescription();

    xmlWriter.writeStartElement(DumpConstants.DESCRIPTION);
    xmlWriter.writeCharacters(description != null ? description : "");
    xmlWriter.writeEndElement();

    // fill properties
    resourceDAO.fillResourceProperties(resource);
    xmlWriter.writeStartElement(DumpConstants.PROPERTIES);

    for (Object keyObject : resource.getPropertyKeys()) {
        String key = (String) keyObject;
        List<String> propValues = resource.getPropertyValues(key);
        for (String value : propValues) {
            xmlWriter.writeStartElement(DumpConstants.PROPERTY_ENTRY);
            // adding the key and value as attributes

            xmlWriter.writeAttribute(DumpConstants.PROPERTY_ENTRY_KEY, key);

            if (value != null) {
                xmlWriter.writeCharacters(value != null ? value : "");
            }

            xmlWriter.writeEndElement();
        }
    }
    xmlWriter.writeEndElement();

    // adding contents..
    if (!(resource instanceof CollectionImpl)) {
        resourceDAO.fillResourceContent(resource);

        byte[] content = (byte[]) resource.getContent();
        if (content != null) {
            xmlWriter.writeStartElement(DumpConstants.CONTENT);
            xmlWriter.writeCharacters(DatatypeConverter.printBase64Binary(content) != null
                    ? DatatypeConverter.printBase64Binary(content)
                    : "");
            xmlWriter.writeEndElement();
        }
    }

    // getting children and applying dump recursively
    if (resource instanceof CollectionImpl) {
        CollectionImpl collection = (CollectionImpl) resource;
        resourceDAO.fillChildren(collection, 0, -1);
        String childPaths[] = collection.getChildPaths();

        xmlWriter.writeStartElement(DumpConstants.CHILDREN);
        xmlWriter.writeCharacters("");

        xmlWriter.flush();

        for (String childPath : childPaths) {
            // we would be writing the start element of the child and its name here.
            try {
                String resourceName = RepositoryUtils.getResourceName(childPath);
                writer.write("<resource name=\"" + resourceName + "\"");
                writer.flush();
            } catch (IOException e) {
                String msg = "Error in writing the start element for the path: " + childPath + ".";
                log.error(msg, e);
                throw new RepositoryException(msg, e);
            }

            recursionRepository.dumpRecursively(childPath, new DumpWriter(writer));
        }
        xmlWriter.writeEndElement();
    }

    xmlWriter.writeEndElement();
    xmlWriter.flush();
}

From source file:org.xmlsh.commands.internal.json2xml.java

private void writeBoolean(Boolean b, XMLStreamWriter writer) throws XMLStreamException {
    writer.writeStartElement("", "boolean", kJXML_URI);
    // writer.writeAttribute("value", b.booleanValue() ? "true" : "false");
    writer.writeCharacters(b.booleanValue() ? "true" : "false");
    writer.writeEndElement();//  w  w w. j  ava2 s .  c o m

}