Search in sources :

Example 1 with Element

use of net.heartsome.xml.Element 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 Element

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

the class StringSegmenter method segment.

/**
	 * Segment.
	 * @param string
	 *            the string
	 * @return the string[]
	 */
public String[] segment(String string) {
    if (string.trim().equals("") || rules.size() == 0) {
        //$NON-NLS-1$
        String[] result = new String[1];
        result[0] = string;
        return result;
    }
    Vector<String> strings = new Vector<String>();
    tags = new Hashtable<String, String>();
    strings.add(prepareString(string));
    // now segment the strings
    int rulessize = rules.size();
    for (int i = 0; i < rulessize; i++) {
        Element rule = rules.get(i);
        //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        boolean breaks = rule.getAttributeValue("break", "yes").equalsIgnoreCase("yes");
        //$NON-NLS-1$
        Element before = rule.getChild("beforebreak");
        //$NON-NLS-1$
        Element after = rule.getChild("afterbreak");
        //$NON-NLS-1$
        String beforexp = "";
        if (before != null) {
            beforexp = before.getText();
        }
        //$NON-NLS-1$
        String afterxp = "";
        if (after != null) {
            afterxp = after.getText();
        }
        if (breaks) {
            // This rule tries to break segments
            Vector<String> temp = new Vector<String>();
            for (int j = 0; j < strings.size(); j++) {
                String[] parts = split(strings.get(j), beforexp, afterxp);
                for (int k = 0; k < parts.length; k++) {
                    temp.add(parts[k]);
                }
            }
            strings = null;
            strings = temp;
        } else {
            // strings = connect3(strings,beforexp,afterxp);
            // This rule marks exceptions, like abbreviations
            Vector<String> temp = new Vector<String>();
            String current = strings.get(0);
            for (int j = 1; j < strings.size(); j++) {
                String next = strings.get(j);
                if (endsWith(current, beforexp) && startsWith(next, afterxp)) {
                    current = current + next;
                } else {
                    temp.add(current);
                    current = next;
                }
            }
            temp.add(current);
            strings = null;
            strings = temp;
        }
    }
    String[] result = new String[strings.size()];
    for (int h = 0; h < strings.size(); h++) {
        result[h] = cleanup(strings.get(h));
    }
    return analysisBlank(result);
}
Also used : Element(net.heartsome.xml.Element) Vector(java.util.Vector)

Example 3 with Element

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

the class ReverseConversionValidateWithLibrary3 method getSkeleton.

/**
	 * 获取骨架文件
	 * @return 骨架文件路径
	 * @throws IOException
	 *             在读取骨架文件失败时抛出 IO 异常 ;
	 */
private String getSkeleton(String xlfPath) throws IOException {
    //$NON-NLS-1$
    String result = "";
    //$NON-NLS-1$
    Element file = root.getChild("file");
    Element header = null;
    String encoding = "";
    if (file != null) {
        //$NON-NLS-1$
        header = file.getChild("header");
        if (header != null) {
            // 添加源文件编码的读取
            //$NON-NLS-1$
            List<Element> propGroups = header.getChildren("hs:prop-group");
            for (int i = 0; i < propGroups.size(); i++) {
                Element prop = propGroups.get(i);
                if (prop.getAttributeValue("name").equals("encoding")) {
                    //$NON-NLS-1$ //$NON-NLS-2$
                    encoding = prop.getText().trim();
                    break;
                }
            }
            if (encoding.equals("utf-8")) {
                //$NON-NLS-1$
                //$NON-NLS-1$
                encoding = "UTF-8";
            }
            //$NON-NLS-1$
            Element mskl = header.getChild("skl");
            if (mskl != null) {
                //$NON-NLS-1$
                Element external = mskl.getChild("external-file");
                IFile xlfIfile = ConverterUtil.localPath2IFile(xlfPath);
                if (external != null) {
                    //$NON-NLS-1$
                    result = external.getAttributeValue("href");
                    //$NON-NLS-1$ //$NON-NLS-2$
                    result = result.replaceAll("&amp;", "&");
                    //$NON-NLS-1$ //$NON-NLS-2$
                    result = result.replaceAll("&lt;", "<");
                    //$NON-NLS-1$ //$NON-NLS-2$
                    result = result.replaceAll("&gt;", ">");
                    //$NON-NLS-1$ //$NON-NLS-2$
                    result = result.replaceAll("&apos;", "\'");
                    //$NON-NLS-1$ //$NON-NLS-2$
                    result = result.replaceAll("&quot;", "\"");
                    result = xlfIfile.getProject().getLocation().toOSString() + result;
                } else {
                    //$NON-NLS-1$
                    Element internal = mskl.getChild("internal-file");
                    if (internal != null) {
                        //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                        File tmp = File.createTempFile("internal", ".skl", new File(xlfIfile.getProject().getWorkspace().getRoot().getLocation().toOSString()));
                        tmp.deleteOnExit();
                        FileOutputStream out = new FileOutputStream(tmp);
                        List<Node> content = internal.getContent();
                        for (int i = 0; i < content.size(); i++) {
                            Node n = content.get(i);
                            if (n.getNodeType() == Node.TEXT_NODE) {
                                out.write(n.getNodeValue().getBytes(encoding));
                            } else if (n.getNodeType() == Node.CDATA_SECTION_NODE) {
                                // fixed bub 515 by john.
                                String cdataString = n.getNodeValue();
                                if (cdataString.endsWith("]]")) {
                                    //$NON-NLS-1$
                                    //$NON-NLS-1$
                                    cdataString += ">";
                                }
                                out.write(cdataString.getBytes());
                            }
                        }
                        out.close();
                        return tmp.getAbsolutePath();
                    }
                    return result;
                }
                external = null;
                mskl = null;
            } else {
                return result;
            }
        } else {
            return result;
        }
    } else {
        return result;
    }
    if (encoding != null) {
        if (encoding.equals("")) {
            //$NON-NLS-1$
            //$NON-NLS-1$
            List<Element> groups = header.getChildren("hs:prop-group");
            for (int i = 0; i < groups.size(); i++) {
                Element group = groups.get(i);
                //$NON-NLS-1$
                List<Element> props = group.getChildren("hs:prop");
                for (int k = 0; k < props.size(); k++) {
                    Element prop = props.get(k);
                    if (prop.getAttributeValue("prop-type", "").equals("encoding")) {
                        //$NON-NLS-1$
                        encoding = prop.getText();
                    }
                }
            }
        }
    }
    header = null;
    file = null;
    return result;
}
Also used : IFile(org.eclipse.core.resources.IFile) Element(net.heartsome.xml.Element) FileOutputStream(java.io.FileOutputStream) Node(org.w3c.dom.Node) IFile(org.eclipse.core.resources.IFile) File(java.io.File)

Example 4 with Element

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

the class PluginHelpDialog method loadToc.

private void loadToc(TreeItem itemParent, String tocFile) throws Exception {
    File file = new File(tocFile);
    String basePath = "";
    if (file.exists()) {
        basePath = file.getParent();
    }
    SAXBuilder builder = new SAXBuilder();
    builder.setEntityResolver(new Catalogue(PluginUtil.getCataloguePath()));
    Document document = builder.build(tocFile);
    Element root = document.getRootElement();
    recurseTree(root, itemParent, basePath);
    String url = root.getAttributeValue("url");
    if (!url.equals("")) {
        File f = new File(url);
        if (!f.exists()) {
            url = basePath + "/" + url;
        }
        hashTable.put(itemParent, new File(url).toURI().toURL().toString());
    }
}
Also used : Catalogue(net.heartsome.xml.Catalogue) SAXBuilder(net.heartsome.xml.SAXBuilder) Element(net.heartsome.xml.Element) Document(net.heartsome.xml.Document) File(java.io.File)

Example 5 with Element

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

the class PluginHelpDialog method searchFile.

private void searchFile(String file, String text, boolean sensitive) throws SAXException, IOException, ParserConfigurationException {
    SAXBuilder builder = new SAXBuilder();
    //$NON-NLS-1$
    builder.setEntityResolver(new Catalogue(PluginUtil.getCataloguePath()));
    if (!file.startsWith("file:")) {
        //$NON-NLS-1$
        if (!file.startsWith("http:")) {
            //$NON-NLS-1$
            File f = new File(file);
            file = f.toURI().toURL().toString();
        }
    }
    URL url = new URL(file);
    Document doc = builder.build(url);
    Element root = doc.getRootElement();
    searchElement(root, file, text, sensitive);
}
Also used : Catalogue(net.heartsome.xml.Catalogue) SAXBuilder(net.heartsome.xml.SAXBuilder) Element(net.heartsome.xml.Element) Document(net.heartsome.xml.Document) File(java.io.File) URL(java.net.URL)

Aggregations

Element (net.heartsome.xml.Element)18 File (java.io.File)8 Catalogue (net.heartsome.xml.Catalogue)7 SAXBuilder (net.heartsome.xml.SAXBuilder)7 Document (net.heartsome.xml.Document)5 Node (org.w3c.dom.Node)5 FileOutputStream (java.io.FileOutputStream)4 Vector (java.util.Vector)3 IOException (java.io.IOException)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 XMLOutputter (net.heartsome.xml.XMLOutputter)2 TreeItem (org.eclipse.swt.widgets.TreeItem)2 SAXException (org.xml.sax.SAXException)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 URL (java.net.URL)1 IFile (org.eclipse.core.resources.IFile)1