Example usage for org.dom4j.io OutputFormat setEncoding

List of usage examples for org.dom4j.io OutputFormat setEncoding

Introduction

In this page you can find the example usage for org.dom4j.io OutputFormat setEncoding.

Prototype

public void setEncoding(String encoding) 

Source Link

Document

DOCUMENT ME!

Usage

From source file:com.oubeichen.gefexp.ShapesEditor.java

License:Open Source License

private void createRawOutput(ByteArrayOutputStream out) throws IOException {
    /*StringBuffer sb = new StringBuffer();
    Iterator it = getModel().getChildren()
    .iterator(), conit;/*w  w w .  ja v  a2  s  .co m*/
    HashSet<Connection> hs = new HashSet<Connection>();
    sb.append("Shapes:\n"
    + getModel().getChildren()
          .size() + "\n");// shape
    while (it.hasNext()) {
       Shape sp = (Shape) it.next();
       sb.append(sp.toString() + "\n");
       sb.append("height:\n"
       + sp.getSize().height
       + "\nwidth:\n"
       + sp.getSize().width + "\n");
       sb.append("location:\n"
       + sp.getLocation().x + " "
       + sp.getLocation().y + "\n");
       conit = sp.getSourceConnections()
       .iterator();
       while (conit.hasNext()) {
    hs.add((Connection) conit.next());
       }
       conit = sp.getTargetConnections()
       .iterator();
       while (conit.hasNext()) {
    hs.add((Connection) conit.next());
       }
       sb.append("\n");// ?
    }
            
    sb.append("Connections:\n" + hs.size()
    + "\n");
    it = hs.iterator();
    while (it.hasNext()) {
       Connection con = (Connection) it
       .next();
       sb.append(con.getSource()
       .toString() + "\n");
       sb.append(con.getTarget()
       .toString() + "\n");
       sb.append("\n");// ?
    }
    out.write(sb.toString().getBytes());*/

    Document document = DocumentHelper.createDocument();
    Element root = document.addElement("diagram");// 
    Element shaperoot = root.addElement("shapes");
    Iterator it = getModel().getChildren().iterator(), conit;
    HashSet<Connection> hs = new HashSet<Connection>();
    while (it.hasNext()) {//shape
        Shape sp = (Shape) it.next();
        Element shapeElm = shaperoot.addElement("shape");
        shapeElm.addElement("name").addText(sp.toString());
        shapeElm.addElement("height").addText(String.valueOf(sp.getSize().height));
        shapeElm.addElement("width").addText(String.valueOf(sp.getSize().width));
        shapeElm.addElement("locx").addText(String.valueOf(sp.getLocation().x));
        shapeElm.addElement("locy").addText(String.valueOf(sp.getLocation().y));
        conit = sp.getSourceConnections().iterator();
        while (conit.hasNext()) {
            hs.add((Connection) conit.next());
        }
        conit = sp.getTargetConnections().iterator();
        while (conit.hasNext()) {
            hs.add((Connection) conit.next());
        }
    }
    Element connroot = root.addElement("connections");
    it = hs.iterator();
    while (it.hasNext()) {
        Element connElm = connroot.addElement("connection");
        Connection con = (Connection) it.next();
        connElm.addElement("source").addText(con.getSource().toString());
        connElm.addElement("target").addText(con.getTarget().toString());
    }
    OutputFormat format = OutputFormat.createPrettyPrint();
    format.setEncoding("UTF-8"); // XML?        
    XMLWriter writer;
    try {
        writer = new XMLWriter(out, format);
        writer.write(document);
        writer.close();
    } catch (IOException ex) {
        System.err.println("Cannot create output file!");
        ex.printStackTrace();
    }
    out.close();
}

From source file:com.panet.imeta.trans.steps.rssoutput.RssOutput.java

License:Open Source License

public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException {
    meta = (RssOutputMeta) smi;//from  w  w w. j  a  v a  2s .c om
    data = (RssOutputData) sdi;

    Object[] r = getRow(); // this also waits for a previous step to be finished.

    if (r == null) // no more input to be expected...
    {
        if (!first) {
            if (!meta.isCustomRss()) {
                // No more input..so write and close the file.
                WriteToFile(data.channeltitlevalue, data.channellinkvalue, data.channeldescriptionvalue,
                        data.channelpubdatevalue, data.channelcopyrightvalue, data.channelimagelinkvalue,
                        data.channelimagedescriptionvalue, data.channelimagelinkvalue,
                        data.channelimageurlvalue, data.channellanguagevalue, data.channelauthorvalue);
            } else {

                // Write to document
                OutputFormat format = org.dom4j.io.OutputFormat.createPrettyPrint();
                // Set encoding ...          
                if (Const.isEmpty(meta.getEncoding()))
                    format.setEncoding("iso-8859-1");
                else
                    format.setEncoding(meta.getEncoding());

                try {
                    XMLWriter writer = new XMLWriter(new FileWriter(new File(data.filename)), format);
                    writer.write(data.document);
                    writer.close();
                } catch (Exception e) {
                } finally {
                    data.document = null;
                }

            }
        }
        setOutputDone();
        return false;
    }

    if (first) {
        first = false;
        data.inputRowMeta = getInputRowMeta();
        data.outputRowMeta = data.inputRowMeta.clone();
        meta.getFields(data.outputRowMeta, getStepname(), null, null, this);
        // Let's check for filename...

        if (meta.isFilenameInField()) {
            if (Const.isEmpty(meta.getFileNameField())) {
                logError(Messages.getString("RssOutput.Log.FilenameFieldMissing"));
                setErrors(1);
                stopAll();
                return false;
            }

            // get filename field index
            data.indexOfFieldfilename = data.inputRowMeta.indexOfValue(meta.getFileNameField());
            if (data.indexOfFieldfilename < 0) {
                // The field is unreachable !
                logError(Messages.getString("RssOutput.Log.ErrorFindingField", meta.getFileNameField()));
                throw new KettleException(
                        Messages.getString("RssOutput.Log.ErrorFindingField", meta.getFileNameField()));
            }

        } else {
            data.filename = buildFilename();
        }

        // Check if filename is empty..
        if (Const.isEmpty(data.filename)) {
            logError(Messages.getString("RssOutput.Log.FilenameEmpty"));
            throw new KettleException(Messages.getString("RssOutput.Log.FilenameEmpty"));
        }

        // Do we need to create parent folder ?
        if (meta.isCreateParentFolder()) {
            // Check for parent folder
            FileObject parentfolder = null;
            try {
                // Get parent folder
                parentfolder = KettleVFS.getFileObject(data.filename).getParent();
                if (!parentfolder.exists()) {
                    if (log.isDetailed())
                        log.logDetailed(toString(), Messages.getString("RssOutput.Log.ParentFolderExists",
                                parentfolder.getName().toString()));
                    parentfolder.createFolder();
                    if (log.isDetailed())
                        log.logDetailed(toString(), Messages.getString("RssOutput.Log.CanNotCreateParentFolder",
                                parentfolder.getName().toString()));
                }
            } catch (Exception e) {
                // The field is unreachable !
                logError(Messages.getString("RssOutput.Log.CanNotCreateParentFolder",
                        parentfolder.getName().toString()));
                throw new KettleException(Messages.getString("RssOutput.Log.CanNotCreateParentFolder",
                        parentfolder.getName().toString()));

            } finally {
                if (parentfolder != null) {
                    try {
                        parentfolder.close();
                    } catch (Exception ex) {
                    }
                    ;
                }
            }
        }

        if (!meta.isCustomRss()) {
            // Let's check for mandatory fields ...
            if (Const.isEmpty(meta.getChannelTitle())) {
                logError(Messages.getString("RssOutput.Log.ChannelTitleMissing"));
                setErrors(1);
                stopAll();
                return false;
            }
            if (Const.isEmpty(meta.getChannelDescription())) {
                logError(Messages.getString("RssOutput.Log.ChannelDescription"));
                setErrors(1);
                stopAll();
                return false;
            }
            if (Const.isEmpty(meta.getChannelLink())) {
                logError(Messages.getString("RssOutput.Log.ChannelLink"));
                setErrors(1);
                stopAll();
                return false;
            }

            // Let's take the index of channel title field ...
            data.indexOfFieldchanneltitle = data.inputRowMeta.indexOfValue(meta.getChannelTitle());
            if (data.indexOfFieldchanneltitle < 0) {
                // The field is unreachable !
                logError(Messages.getString("RssOutput.Log.ErrorFindingField", meta.getChannelTitle()));
                throw new KettleException(
                        Messages.getString("RssOutput.Log.ErrorFindingField", meta.getChannelTitle()));
            }
            data.channeltitlevalue = data.inputRowMeta.getString(r, data.indexOfFieldchanneltitle);

            // Let's take the index of channel description field ...   
            data.indexOfFieldchanneldescription = data.inputRowMeta.indexOfValue(meta.getChannelDescription());
            if (data.indexOfFieldchanneldescription < 0) {
                // The field is unreachable !
                logError(Messages.getString("RssOutput.Log.ErrorFindingField", meta.getChannelDescription()));
                throw new KettleException(
                        Messages.getString("RssOutput.Log.ErrorFindingField", meta.getChannelDescription()));
            }

            data.channeldescriptionvalue = data.inputRowMeta.getString(r, data.indexOfFieldchanneldescription);

            // Let's take the index of channel link field ...   
            data.indexOfFieldchannellink = data.inputRowMeta.indexOfValue(meta.getChannelLink());
            if (data.indexOfFieldchannellink < 0) {
                // The field is unreachable !
                logError(Messages.getString("RssOutput.Log.ErrorFindingField", meta.getChannelLink()));
                throw new KettleException(
                        Messages.getString("RssOutput.Log.ErrorFindingField", meta.getChannelLink()));
            }

            data.channellinkvalue = data.inputRowMeta.getString(r, data.indexOfFieldchannellink);

            if (!Const.isEmpty(meta.getItemTitle())) {
                // Let's take the index of item title field ...   
                data.indexOfFielditemtitle = data.inputRowMeta.indexOfValue(meta.getItemTitle());
                if (data.indexOfFielditemtitle < 0) {
                    // The field is unreachable !
                    logError(Messages.getString("RssOutput.Log.ErrorFindingField", meta.getItemTitle()));
                    throw new KettleException(
                            Messages.getString("RssOutput.Log.ErrorFindingField", meta.getItemTitle()));
                }
            }

            if (!Const.isEmpty(meta.getItemDescription())) {
                // Let's take the index of item description field ...
                data.indexOfFielditemdescription = data.inputRowMeta.indexOfValue(meta.getItemDescription());
                if (data.indexOfFielditemdescription < 0) {
                    // The field is unreachable !
                    logError(Messages.getString("RssOutput.Log.ErrorFindingField", meta.getItemDescription()));
                    throw new KettleException(
                            Messages.getString("RssOutput.Log.ErrorFindingField", meta.getItemDescription()));
                }
            }
            if (meta.AddGeoRSS()) {
                if (Const.isEmpty(meta.getGeoPointLong()))
                    throw new KettleException(Messages.getString("RssOutput.Log.GeoPointLatEmpty"));
                if (Const.isEmpty(meta.getGeoPointLong()))
                    throw new KettleException(Messages.getString("RssOutput.Log.GeoPointLongEmpty"));

                // Let's take the index of item geopointX field ...
                data.indexOfFielditempointx = data.inputRowMeta.indexOfValue(meta.getGeoPointLat());
                if (data.indexOfFielditempointx < 0) {
                    // The field is unreachable !
                    logError(Messages.getString("RssOutput.Log.ErrorFindingField", meta.getGeoPointLat()));
                    throw new KettleException(
                            Messages.getString("RssOutput.Log.ErrorFindingField", meta.getGeoPointLat()));
                }
                // Let's take the index of item geopointY field ...
                data.indexOfFielditempointy = data.inputRowMeta.indexOfValue(meta.getGeoPointLong());
                if (data.indexOfFielditempointy < 0) {
                    // The field is unreachable !
                    logError(Messages.getString("RssOutput.Log.ErrorFindingField", meta.getGeoPointLong()));
                    throw new KettleException(
                            Messages.getString("RssOutput.Log.ErrorFindingField", meta.getGeoPointLong()));
                }
            }

            //It's time to check non empty fields !
            // Channel PubDate field ...
            if (!Const.isEmpty(meta.getChannelPubDate())) {
                data.indexOfFieldchannelpubdate = data.inputRowMeta.indexOfValue(meta.getChannelPubDate());
                if (data.indexOfFieldchannelpubdate < 0) {
                    // The field is unreachable !
                    logError(Messages.getString("RssOutput.Log.ErrorFindingField", meta.getChannelPubDate()));
                    throw new KettleException(
                            Messages.getString("RssOutput.Log.ErrorFindingField", meta.getChannelPubDate()));
                }

                data.channelpubdatevalue = data.inputRowMeta.getDate(r, data.indexOfFieldchannelpubdate);
            }
            // Channel Language field ...
            if (!Const.isEmpty(meta.getChannelLanguage())) {
                data.indexOfFieldchannellanguage = data.inputRowMeta.indexOfValue(meta.getChannelLanguage());
                if (data.indexOfFieldchannellanguage < 0) {
                    // The field is unreachable !
                    logError(Messages.getString("RssOutput.Log.ErrorFindingField", meta.getChannelLanguage()));
                    throw new KettleException(
                            Messages.getString("RssOutput.Log.ErrorFindingField", meta.getChannelLanguage()));
                }

                data.channellanguagevalue = data.inputRowMeta.getString(r, data.indexOfFieldchannellanguage);
            }

            // Channel Copyright field ...
            if (!Const.isEmpty(meta.getChannelCopyright())) {
                data.indexOfFieldchannelcopyright = data.inputRowMeta.indexOfValue(meta.getChannelCopyright());
                if (data.indexOfFieldchannelcopyright < 0) {
                    // The field is unreachable !
                    logError(Messages.getString("RssOutput.Log.ErrorFindingField", meta.getChannelCopyright()));
                    throw new KettleException(
                            Messages.getString("RssOutput.Log.ErrorFindingField", meta.getChannelCopyright()));
                }

                data.channelcopyrightvalue = data.inputRowMeta.getString(r, data.indexOfFieldchannelcopyright);
            }

            // Channel Author field ...
            if (!Const.isEmpty(meta.getChannelAuthor())) {
                data.indexOfFieldchannelauthor = data.inputRowMeta.indexOfValue(meta.getChannelAuthor());
                if (data.indexOfFieldchannelauthor < 0) {
                    // The field is unreachable !
                    logError(Messages.getString("RssOutput.Log.ErrorFindingField", meta.getChannelAuthor()));
                    throw new KettleException(
                            Messages.getString("RssOutput.Log.ErrorFindingField", meta.getChannelAuthor()));
                }

                data.channelauthorvalue = data.inputRowMeta.getString(r, data.indexOfFieldchannelauthor);
            }

            // Channel Image field ...
            if (meta.AddImage()) {
                // Channel image title
                if (!Const.isEmpty(meta.getChannelImageTitle())) {
                    data.indexOfFieldchannelimagetitle = data.inputRowMeta
                            .indexOfValue(meta.getChannelImageTitle());
                    if (data.indexOfFieldchannelimagetitle < 0) {
                        // The field is unreachable !
                        logError(Messages.getString("RssOutput.Log.ErrorFindingField",
                                meta.getChannelImageTitle()));
                        throw new KettleException(Messages.getString("RssOutput.Log.ErrorFindingField",
                                meta.getChannelImageTitle()));
                    }

                    data.channelimagetitlevalue = data.inputRowMeta.getString(r,
                            data.indexOfFieldchannelimagetitle);
                }

                // Channel link title
                if (!Const.isEmpty(meta.getChannelImageLink())) {
                    data.indexOfFieldchannelimagelink = data.inputRowMeta
                            .indexOfValue(meta.getChannelImageLink());
                    if (data.indexOfFieldchannelimagelink < 0) {
                        // The field is unreachable !
                        logError(Messages.getString("RssOutput.Log.ErrorFindingField",
                                meta.getChannelImageLink()));
                        throw new KettleException(Messages.getString("RssOutput.Log.ErrorFindingField",
                                meta.getChannelImageLink()));
                    }

                    data.channelimagelinkvalue = data.inputRowMeta.getString(r,
                            data.indexOfFieldchannelimagelink);
                }

                // Channel url title
                if (!Const.isEmpty(meta.getChannelImageUrl())) {
                    data.indexOfFieldchannelimageurl = data.inputRowMeta
                            .indexOfValue(meta.getChannelImageUrl());
                    if (data.indexOfFieldchannelimageurl < 0) {
                        // The field is unreachable !
                        logError(Messages.getString("RssOutput.Log.ErrorFindingField",
                                meta.getChannelImageUrl()));
                        throw new KettleException(Messages.getString("RssOutput.Log.ErrorFindingField",
                                meta.getChannelImageUrl()));
                    }

                    data.channelimageurlvalue = data.inputRowMeta.getString(r,
                            data.indexOfFieldchannelimageurl);
                }

                // Channel description title
                if (!Const.isEmpty(meta.getChannelImageDescription())) {
                    data.indexOfFieldchannelimagedescription = data.inputRowMeta
                            .indexOfValue(meta.getChannelImageDescription());
                    if (data.indexOfFieldchannelimagedescription < 0) {
                        // The field is unreachable !
                        logError(Messages.getString("RssOutput.Log.ErrorFindingField",
                                meta.getChannelImageDescription()));
                        throw new KettleException(Messages.getString("RssOutput.Log.ErrorFindingField",
                                meta.getChannelImageDescription()));
                    }

                    data.channelimagedescriptionvalue = data.inputRowMeta.getString(r,
                            data.indexOfFieldchannelimagedescription);
                }

            }

            // Item link field ...
            if (!Const.isEmpty(meta.getItemLink())) {
                data.indexOfFielditemlink = data.inputRowMeta.indexOfValue(meta.getItemLink());
                if (data.indexOfFielditemlink < 0) {
                    // The field is unreachable !
                    logError(Messages.getString("RssOutput.Log.ErrorFindingField", meta.getItemLink()));
                    throw new KettleException(
                            Messages.getString("RssOutput.Log.ErrorFindingField", meta.getItemLink()));
                }

            }

            // Item pubdate field ...
            if (!Const.isEmpty(meta.getItemPubDate())) {
                data.indexOfFielditempubdate = data.inputRowMeta.indexOfValue(meta.getItemPubDate());
                if (data.indexOfFielditempubdate < 0) {
                    // The field is unreachable !
                    logError(Messages.getString("RssOutput.Log.ErrorFindingField", meta.getItemPubDate()));
                    throw new KettleException(
                            Messages.getString("RssOutput.Log.ErrorFindingField", meta.getItemPubDate()));
                }
            }

            // Item author field ...
            if (!Const.isEmpty(meta.getItemAuthor())) {
                data.indexOfFielditemauthor = data.inputRowMeta.indexOfValue(meta.getItemAuthor());
                if (data.indexOfFielditemauthor < 0) {
                    // The field is unreachable !
                    logError(Messages.getString("RssOutput.Log.ErrorFindingField", meta.getItemAuthor()));
                    throw new KettleException(
                            Messages.getString("RssOutput.Log.ErrorFindingField", meta.getItemAuthor()));
                }
            }
        } else {
            // Custom RSS
            // Check Custom channel fields
            data.customchannels = new int[meta.getChannelCustomFields().length];
            for (int i = 0; i < meta.getChannelCustomFields().length; i++) {
                data.customchannels[i] = data.inputRowMeta.indexOfValue(meta.getChannelCustomFields()[i]);
                if (data.customchannels[i] < 0) // couldn't find field!
                {
                    throw new KettleStepException(Messages.getString("RssOutput.Exception.FieldRequired", //$NON-NLS-1$
                            meta.getChannelCustomFields()[i])); //$NON-NLS-2$
                }
            }
            // Check Custom channel fields
            data.customitems = new int[meta.getItemCustomFields().length];
            for (int i = 0; i < meta.getItemCustomFields().length; i++) {
                data.customitems[i] = data.inputRowMeta.indexOfValue(meta.getItemCustomFields()[i]);
                if (data.customitems[i] < 0) // couldn't find field!
                {
                    throw new KettleStepException(Messages.getString("RssOutput.Exception.FieldRequired", //$NON-NLS-1$
                            meta.getItemCustomFields()[i])); //$NON-NLS-2$
                }
            }
            // Prepare Output RSS Custom document
            data.document = DocumentHelper.createDocument();
            data.rssElement = data.document.addElement("rss");
            data.rssElement.addAttribute("version", "2.0");
            // add namespaces here ...
            for (int i = 0; i < meta.getNameSpaces().length; i++) {
                data.rssElement.addAttribute(environmentSubstitute(meta.getNameSpacesTitle()[i]),
                        environmentSubstitute(meta.getNameSpaces()[i]));
            }

            // Add channel
            data.channel = data.rssElement.addElement("channel");

            // Set channel Only the first time ...
            for (int i = 0; i < data.customchannels.length; i++) {
                String channelname = environmentSubstitute(meta.getChannelCustomTags()[i]);
                String channelvalue = data.inputRowMeta.getString(r, data.customchannels[i]);

                if (log.isDetailed())
                    log.logDetailed(toString(), "outputting channel value <" + channelname + ">" + channelvalue
                            + "<" + channelname + "/>");

                // add Channel
                Element channeltag = data.channel.addElement(channelname);
                channeltag.setText(channelvalue);

            }
        }
    } // end test first time

    // Let's get value for each item...
    if (!meta.isCustomRss()) {
        String itemtitlevalue = null;
        String itemauthorvalue = null;
        String itemlinkvalue = null;
        Date itemdatevalue = null;
        String itemdescvalue = null;
        String itemgeopointx = null;
        String itemgeopointy = null;

        if (data.indexOfFielditemtitle > -1)
            itemtitlevalue = data.inputRowMeta.getString(r, data.indexOfFielditemtitle);
        if (data.indexOfFielditemauthor > -1)
            itemauthorvalue = data.inputRowMeta.getString(r, data.indexOfFielditemauthor);
        if (data.indexOfFielditemlink > -1)
            itemlinkvalue = data.inputRowMeta.getString(r, data.indexOfFielditemlink);
        if (data.indexOfFielditempubdate > -1)
            itemdatevalue = data.inputRowMeta.getDate(r, data.indexOfFielditempubdate);
        if (data.indexOfFielditemdescription > -1)
            itemdescvalue = data.inputRowMeta.getString(r, data.indexOfFielditemdescription);
        if (data.indexOfFielditempointx > -1)
            itemgeopointx = data.inputRowMeta.getString(r, data.indexOfFielditempointx);
        if (data.indexOfFielditempointy > -1)
            itemgeopointy = data.inputRowMeta.getString(r, data.indexOfFielditempointy);

        // Now add entry ..
        if (!createEntry(itemauthorvalue, itemtitlevalue, itemlinkvalue, itemdatevalue, itemdescvalue,
                itemgeopointx, itemgeopointy))
            throw new KettleException("Error adding item to feed");
    } else {

        // Set item tag at each row received 
        if (meta.isDisplayItem()) {
            data.itemtag = data.channel.addElement("item");
        }
        for (int i = 0; i < data.customitems.length; i++) {
            // get item value and name
            String itemname = environmentSubstitute(meta.getItemCustomTags()[i]);
            String itemvalue = data.inputRowMeta.getString(r, data.customitems[i]);

            if (log.isDetailed())
                log.logDetailed(toString(),
                        "outputting item value <" + itemname + ">" + itemvalue + "<" + itemname + "/>");

            // add Item
            if (meta.isDisplayItem()) {
                Element itemtagsub = data.itemtag.addElement(itemname);
                itemtagsub.setText(itemvalue);
            } else {
                // display item at channel level
                data.channel.addElement(itemname);
                data.channel.setText(itemvalue);
            }
        }
    }

    try {
        putRow(data.outputRowMeta, r); // in case we want it to go further...
        incrementLinesOutput();

        if (checkFeedback(getLinesOutput())) {
            if (log.isDebug())
                logDebug(Messages.getString("RssOutput.Log.Linenr", "" + getLinesOutput()));
        }

    } catch (KettleStepException e) {
        logError(Messages.getString("RssOutputMeta.Log.ErrorInStep") + e.getMessage()); //$NON-NLS-1$
        setErrors(1);
        stopAll();
        setOutputDone(); // signal end to receiver(s)
        return false;
    }

    return true;
}

From source file:com.partycommittee.service.PcLicenseService.java

public PcLicenseVo setLicenseInfo(String enstr, String tips) {
    if (enstr == null || enstr.equals(""))
        return null;
    if (tips == null || tips.equals(""))
        return null;
    PcLicenseVo licensevo = new PcLicenseVo();
    Des des = new Des();
    try {//from  w w  w  . j av a 2 s. c om

        String xml = des.DecryPwdStr(enstr, "hrpweb30");// String 
        System.out.println("xml ==============" + xml);
        if (xml == "" || xml.equals("")) {
            return null;
        }

        if (xml.indexOf("locknodw") <= 0) {
            return null;
        }

        if (xml.indexOf("locknozb") <= 0) {
            return null;
        }

        if (xml.indexOf("root") <= 0) {
            xml = "<root>" + xml + "</root>";
        }
        System.out.println("xml +++++++++++++++++" + xml);
        licensevo = setLicenseVo(xml, tips);

        SAXReader saxReader = new SAXReader();
        Document document = null;
        File file = new File(
                PcLicenseService.class.getClassLoader().getResource("/").getPath() + "license.xml");

        document = saxReader.read(file);
        Element root = document.getRootElement();

        Element license_element = root.element("license");

        license_element.setText(enstr);

        Element tips_element = root.element("tips");

        tips_element.setText(tips);

        String path = PcLicenseService.class.getClassLoader().getResource("/").getPath();
        OutputFormat format = OutputFormat.createPrettyPrint();
        // ??  
        format.setEncoding("UTF8");
        FileOutputStream output = new FileOutputStream(new File(path + "license.xml"));
        XMLWriter writer = new XMLWriter(output, format);
        writer.write(document);
        writer.flush();
        writer.close();

    } catch (Exception e) {
        //e.printStackTrace();
        return null;
    }

    return licensevo;
}

From source file:com.poka.util.XmlSax.java

public boolean writeToXml(Document doc, String file) {
    try {/*  w ww.  ja v a2s  .  c o m*/
        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setEncoding(encodingType);
        XMLWriter writer = new XMLWriter(new FileWriter(file), format);
        writer.write(doc);
        writer.flush();
        writer.close();

    } catch (IOException ex) {
        Logger.getLogger(XmlSax.class.getName()).log(Level.SEVERE, null, ex);
        return false;
    }
    return true;
}

From source file:com.poka.util.XmlSax.java

public int updateOrAddMachineInfo(MachinesCfg cfg) {
    Document doc = load(bankFile, false);
    Element rootElm = doc.getRootElement();
    Element root1Elm = rootElm.element("machines");
    if (root1Elm == null) {
        root1Elm = rootElm.addElement("machines");
    }/*from  ww w. jav a 2  s  .co  m*/
    List nodes = root1Elm.elements("machine");
    boolean flag = false;
    for (Iterator it = nodes.iterator(); it.hasNext();) {
        Element elm = (Element) it.next();
        if (elm.attributeValue("ip").trim().equals(cfg.getIp().trim())
                && elm.attributeValue("type").trim().equals("" + cfg.getType())) {
            elm.setAttributeValue("machineType", cfg.getMachineType().trim());
            elm.setAttributeValue("machineNum", "" + cfg.getMachineNum());
            elm.setAttributeValue("user1", cfg.getUser1().trim());
            elm.setAttributeValue("user2", cfg.getUser2().trim());
            elm.setAttributeValue("type", "" + cfg.getType());
            flag = true;
            break;
        }
    }
    if (!flag) {
        Element tem = root1Elm.addElement("machine");
        tem.addAttribute("machineType", cfg.getMachineType().trim());
        tem.setAttributeValue("machineNum", "" + cfg.getMachineNum());
        tem.addAttribute("ip", cfg.getIp().trim());
        tem.addAttribute("user1", cfg.getUser1().trim());
        tem.addAttribute("user2", cfg.getUser2().trim());
        tem.addAttribute("type", "" + cfg.getType());
    }
    try {
        OutputFormat format = OutputFormat.createPrettyPrint();
        // format.setEncoding("UTF-8");
        format.setEncoding(encodingType);
        XMLWriter writer = new XMLWriter(new FileWriter(bankFile), format);
        writer.write(doc);
        writer.flush();
        writer.close();
        if (flag) {//
            return 0;
        } else {//
            return 1;
        }
        //  return true;
    } catch (IOException ex) {
        Logger.getLogger(XmlSax.class.getName()).log(Level.SEVERE, null, ex);
    }
    return -1;
}

From source file:com.processpuzzle.application.domain.ApplicationRepository.java

License:Open Source License

private void writeXmlDocument() throws IOException {
    OutputFormat outputFormat = OutputFormat.createPrettyPrint();
    outputFormat.setEncoding(xmlDocument.getXMLEncoding());
    OutputStream output = new FileOutputStream(xmlResource.getFile());
    XMLWriter writer = new XMLWriter(output, outputFormat);

    writer.write(xmlDocument);//from ww  w  .jav a  2 s  . com
    writer.close();
}

From source file:com.rockagen.commons.util.XmlUtil.java

License:Apache License

/**
 * Format xml {@link OutputFormat#createPrettyPrint()}
 *
 * @param xmlStr                xml String
 * @param enc                   encoding
 * @param isSuppressDeclaration set supperss declaration
 * @return pretty String/*  w  w w . j av a2s .com*/
 */
public static String formatPretty(String xmlStr, String enc, boolean isSuppressDeclaration) {

    if (CommUtil.isBlank(xmlStr))
        return xmlStr;

    if (enc == null)
        enc = ENCODING;

    OutputFormat formater = OutputFormat.createPrettyPrint();
    formater.setEncoding(enc);
    formater.setSuppressDeclaration(isSuppressDeclaration);
    return format(xmlStr, formater);
}

From source file:com.rockagen.commons.util.XmlUtil.java

License:Apache License

/**
 * Format xml {@link OutputFormat#createCompactFormat()}
 *
 * @param xmlStr                xml String
 * @param enc                   encoding
 * @param isSuppressDeclaration set supperss declaration
 * @return Compact xml String//from   w  w w . j  av  a  2s .  c om
 */
public static String formatCompact(String xmlStr, String enc, boolean isSuppressDeclaration) {

    if (CommUtil.isBlank(xmlStr))
        return xmlStr;

    if (enc == null)
        enc = ENCODING;

    OutputFormat formater = OutputFormat.createCompactFormat();
    formater.setEncoding(enc);
    formater.setSuppressDeclaration(isSuppressDeclaration);
    return format(xmlStr, formater);
}

From source file:com.sammyun.util.SettingUtils.java

License:Open Source License

/**
 * /*from   w  ww.  j ava  2 s .c  o m*/
 * 
 * @param setting 
 */
public static void set(Setting setting) {
    try {
        File preschoolEduXmlFile = new ClassPathResource(CommonAttributes.PRESCHOOLEDU_XML_PATH).getFile();
        Document document = new SAXReader().read(preschoolEduXmlFile);
        List<Element> elements = document.selectNodes("/preschoolEdu/setting");
        for (Element element : elements) {
            try {
                String name = element.attributeValue("name");
                String value = beanUtils.getProperty(setting, name);
                Attribute attribute = element.attribute("value");
                attribute.setValue(value);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
                logger.error(e.getMessage());
            } catch (InvocationTargetException e) {
                e.printStackTrace();
                logger.error(e.getMessage());
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
                logger.error(e.getMessage());
            }
        }

        FileOutputStream fileOutputStream = null;
        XMLWriter xmlWriter = null;
        try {
            OutputFormat outputFormat = OutputFormat.createPrettyPrint();
            outputFormat.setEncoding("UTF-8");
            outputFormat.setIndent(true);
            outputFormat.setIndent("   ");
            outputFormat.setNewlines(true);
            fileOutputStream = new FileOutputStream(preschoolEduXmlFile);
            xmlWriter = new XMLWriter(fileOutputStream, outputFormat);
            xmlWriter.write(document);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (xmlWriter != null) {
                try {
                    xmlWriter.close();
                } catch (IOException e) {
                }
            }
            IOUtils.closeQuietly(fileOutputStream);
        }

        Ehcache cache = cacheManager.getEhcache(Setting.CACHE_NAME);
        cache.put(new net.sf.ehcache.Element(Setting.CACHE_KEY, setting));
    } catch (Exception e) {
        e.printStackTrace();
        logger.error(e.getMessage());
    }
}

From source file:com.sap.data.db.dao.StructureUtil.java

private void save(Document document, String filepath) throws IOException {
    document.addDocType(this.HIBERNATE_MAPPING, this.HIBERNATE_MAPPING_EN, this.HIBERNATE_MAPPING_DTD);
    OutputFormat format = OutputFormat.createPrettyPrint();
    format.setEncoding("UTF-8");

    XMLWriter writer = new XMLWriter(new FileWriter(new File(filepath)), format);
    writer.write(document);//w w  w . j  a v a  2 s  .  c o m
    writer.close();
}