Search in sources :

Example 1 with XMLOutputter

use of net.heartsome.xml.XMLOutputter in project translationstudio8 by heartsome.

the class AutoConfiguration method run.

/**
	 * Run.
	 * @param input
	 *            the input
	 * @param out
	 *            the out
	 * @param catalogue
	 *            the catalogue
	 * @throws SAXException
	 *             the SAX exception
	 * @throws IOException
	 *             Signals that an I/O exception has occurred.
	 * @throws ParserConfigurationException
	 *             the parser configuration exception
	 */
public void run(String input, String out, String catalogue) throws SAXException, IOException, ParserConfigurationException {
    SAXBuilder builder = new SAXBuilder();
    builder.setEntityResolver(new Catalogue(catalogue));
    Document d = builder.build(input);
    Element r = d.getRootElement();
    segment = new Hashtable<String, String>();
    recurse(r);
    //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    Document doc = new Document(null, "ini-file", "-//HEARTSOME//Converters 2.0.0//EN", "configuration.dtd");
    Element root = doc.getRootElement();
    Enumeration<String> keys = segment.keys();
    while (keys.hasMoreElements()) {
        //$NON-NLS-1$
        Element e = new Element("tag", doc);
        String key = keys.nextElement();
        e.setText(key);
        //$NON-NLS-1$ //$NON-NLS-2$
        e.setAttribute("hard-break", "segment");
        root.addContent(e);
        //$NON-NLS-1$
        root.addContent("\n");
    }
    XMLOutputter outputter = new XMLOutputter();
    FileOutputStream output = new FileOutputStream(out);
    outputter.output(doc, output);
    output.close();
}
Also used : Catalogue(net.heartsome.xml.Catalogue) XMLOutputter(net.heartsome.xml.XMLOutputter) SAXBuilder(net.heartsome.xml.SAXBuilder) Element(net.heartsome.xml.Element) FileOutputStream(java.io.FileOutputStream) Document(net.heartsome.xml.Document)

Example 2 with XMLOutputter

use of net.heartsome.xml.XMLOutputter in project translationstudio8 by heartsome.

the class ReverseConversionValidateWithLibrary3 method reBuildXlf.

/**
	 * 构建 xliff 文件副本
	 * @param tmpXLFFile
	 * @throws IOException
	 *             ;
	 */
private void reBuildXlf(File tmpXLFFile) throws IOException {
    long startTime = 0;
    if (LOGGER.isInfoEnabled()) {
        startTime = System.currentTimeMillis();
        LOGGER.info(Messages.getString("model.ReverseConversionValidateWithLibrary3.logger14"), startTime);
    }
    for (int i = 0, size = segments.size() - 1; i < size; i++) {
        Element e = segments.get(i);
        //$NON-NLS-1$
        Element src = e.getChild("source");
        //$NON-NLS-1$
        Element tgt = e.getChild("target");
        //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        boolean isApproved = e.getAttributeValue("approved", "no").equalsIgnoreCase("yes");
        List<Node> srcList = src.getContent();
        Vector<Node> tmp = new Vector<Node>();
        for (int j = 0, jSize = srcList.size(); j < jSize; j++) {
            Node o = srcList.get(j);
            if (o.getNodeType() == Node.ELEMENT_NODE && o.getNodeName().equals("ph")) {
                //$NON-NLS-1$
                Element el = new Element(o);
                if (el.getAttributeValue("id", "").startsWith("hs-merge")) {
                    //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                    //$NON-NLS-1$ //$NON-NLS-2$
                    String tmpMergeId = el.getAttributeValue("id", "").substring(8);
                    //$NON-NLS-1$
                    String[] pairId = tmpMergeId.split("~");
                    srcList.remove(j);
                    j--;
                    jSize--;
                    //$NON-NLS-1$
                    int idIndex = pairId[0].indexOf("-");
                    if (idIndex != -1) {
                        pairId[0] = pairId[0].substring(0, idIndex);
                    }
                    //$NON-NLS-1$
                    idIndex = pairId[1].indexOf("-");
                    if (idIndex != -1) {
                        pairId[1] = pairId[1].substring(0, idIndex);
                    }
                    if (!pairId[0].equals(pairId[1])) {
                        pairId = null;
                        break;
                    }
                    pairId = null;
                } else {
                    srcList.remove(j);
                    j--;
                    jSize--;
                    tmp.add(o);
                }
            } else {
                srcList.remove(j);
                j--;
                jSize--;
                tmp.add(o);
            }
        }
        src.removeAllChildren();
        src.setContent(tmp);
        tmp = null;
        if (tgt == null) {
            //$NON-NLS-1$
            tgt = new Element("target", doc);
            //$NON-NLS-1$
            tgt.setAttribute(Messages.getString("model.ReverseConversionValidateWithLibrary3.msg5"), targetLanguage);
            //$NON-NLS-1$ //$NON-NLS-2$
            tgt.setAttribute("state", "new");
            List<Element> content = e.getChildren();
            Vector<Element> newContent = new Vector<Element>();
            for (int m = 0; m < content.size(); m++) {
                Element tmpEl = content.get(m);
                newContent.add(tmpEl);
                if (tmpEl.getName().equals("source")) {
                    //$NON-NLS-1$
                    newContent.add(tgt);
                }
                tmpEl = null;
            }
            e.setContent(newContent);
            newContent = null;
            content = null;
        }
        List<Node> tgtList = tgt.getContent();
        tmp = new Vector<Node>();
        for (int j = 0, jSize = tgtList.size(); j < jSize; j++) {
            Node o = tgtList.get(j);
            if (o.getNodeType() == Node.ELEMENT_NODE && o.getNodeName().equals("ph")) {
                //$NON-NLS-1$
                Element el = new Element(o);
                if (el.getAttributeValue("id", "").startsWith("hs-merge")) {
                    //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                    //$NON-NLS-1$ //$NON-NLS-2$
                    String tmpMergeId = el.getAttributeValue("id", "").substring(8);
                    //$NON-NLS-1$
                    String[] pairId = tmpMergeId.split("~");
                    tgtList.remove(j);
                    j--;
                    jSize--;
                    //$NON-NLS-1$
                    int idIndex = pairId[0].indexOf("-");
                    if (idIndex != -1) {
                        pairId[0] = pairId[0].substring(0, idIndex);
                    }
                    //$NON-NLS-1$
                    idIndex = pairId[1].indexOf("-");
                    if (idIndex != -1) {
                        pairId[1] = pairId[1].substring(0, idIndex);
                    }
                    if (!pairId[0].equals(pairId[1])) {
                        pairId = null;
                        break;
                    }
                    pairId = null;
                } else {
                    tgtList.remove(j);
                    j--;
                    jSize--;
                    tmp.add(o);
                }
                el = null;
            } else {
                tgtList.remove(j);
                j--;
                jSize--;
                tmp.add(o);
            }
        }
        tgt.removeAllChildren();
        tgt.setContent(tmp);
        tmp = null;
        Element nextEl = segments.get(i + 1);
        if (!isApproved && srcList.size() > 0) {
            //$NON-NLS-1$ //$NON-NLS-2$
            nextEl.setAttribute("approved", "no");
        }
        //$NON-NLS-1$
        Element nextSrc = nextEl.getChild("source");
        //$NON-NLS-1$
        Element nextTgt = nextEl.getChild("target");
        if (nextTgt == null) {
            //$NON-NLS-1$
            nextTgt = new Element("target", doc);
            //$NON-NLS-1$
            nextTgt.setAttribute("xml:lang", targetLanguage);
            //$NON-NLS-1$ //$NON-NLS-2$
            nextTgt.setAttribute("state", "new");
            List<Element> content = nextEl.getChildren();
            Vector<Element> newContent = new Vector<Element>();
            for (int m = 0; m < content.size(); m++) {
                Element tmpEl = content.get(m);
                newContent.add(tmpEl);
                if (tmpEl.getName().equals("source")) {
                    //$NON-NLS-1$
                    newContent.add(nextTgt);
                }
                tmpEl = null;
            }
            nextEl.setContent(newContent);
            newContent = null;
            content = null;
        }
        List<Node> nextSrcContent = nextSrc.getContent();
        List<Node> nextTgtContent = nextTgt.getContent();
        nextSrc.removeAllChildren();
        Vector<Node> newNextSrcContent = new Vector<Node>();
        newNextSrcContent.addAll(srcList);
        for (int j = 0, jSize = nextSrcContent.size(); j < jSize; j++) {
            newNextSrcContent.add(nextSrcContent.get(j));
        }
        nextSrc.setContent(newNextSrcContent);
        newNextSrcContent = null;
        nextTgt.removeAllChildren();
        Vector<Node> newNextTgtContent = new Vector<Node>();
        newNextTgtContent.addAll(tgtList);
        for (int j = 0, jSize = nextTgtContent.size(); j < jSize; j++) {
            newNextTgtContent.add(nextTgtContent.get(j));
        }
        nextTgt.setContent(newNextTgtContent);
        newNextTgtContent = null;
    }
    long endTime = 0;
    if (LOGGER.isInfoEnabled()) {
        endTime = System.currentTimeMillis();
        LOGGER.info(Messages.getString("model.ReverseConversionValidateWithLibrary3.logger15"), endTime);
        LOGGER.info(Messages.getString("model.ReverseConversionValidateWithLibrary3.logger16"), (endTime - startTime));
    }
    XMLOutputter outputter = new XMLOutputter();
    outputter.preserveSpace(true);
    FileOutputStream out;
    out = new FileOutputStream(tmpXLFFile);
    if (LOGGER.isInfoEnabled()) {
        startTime = System.currentTimeMillis();
        LOGGER.info(Messages.getString("model.ReverseConversionValidateWithLibrary3.logger17"), startTime);
    }
    outputter.output(doc, out);
    if (LOGGER.isInfoEnabled()) {
        endTime = System.currentTimeMillis();
        LOGGER.info(Messages.getString("model.ReverseConversionValidateWithLibrary3.logger18"), endTime);
        LOGGER.info(Messages.getString("model.ReverseConversionValidateWithLibrary3.logger19"), (endTime - startTime));
    }
    out.close();
    outputter = null;
}
Also used : XMLOutputter(net.heartsome.xml.XMLOutputter) Element(net.heartsome.xml.Element) Node(org.w3c.dom.Node) FileOutputStream(java.io.FileOutputStream) Vector(java.util.Vector)

Example 3 with XMLOutputter

use of net.heartsome.xml.XMLOutputter in project translationstudio8 by heartsome.

the class Xliff2Resx method saveXml.

// Save the xml to a file
/**
	 * Save xml.
	 * @param xmlDoc
	 *            the xml doc
	 * @param xmlFile
	 *            the xml file
	 * @throws Exception
	 *             the exception
	 */
private static void saveXml(Document xmlDoc, String xmlFile) throws Exception {
    XMLOutputter outputter = new XMLOutputter();
    outputter.preserveSpace(true);
    FileOutputStream soutput = new FileOutputStream(xmlFile);
    outputter.output(xmlDoc, soutput);
    soutput.close();
}
Also used : XMLOutputter(net.heartsome.xml.XMLOutputter) FileOutputStream(java.io.FileOutputStream)

Aggregations

FileOutputStream (java.io.FileOutputStream)3 XMLOutputter (net.heartsome.xml.XMLOutputter)3 Element (net.heartsome.xml.Element)2 Vector (java.util.Vector)1 Catalogue (net.heartsome.xml.Catalogue)1 Document (net.heartsome.xml.Document)1 SAXBuilder (net.heartsome.xml.SAXBuilder)1 Node (org.w3c.dom.Node)1