Example usage for org.jdom2 Element getContentSize

List of usage examples for org.jdom2 Element getContentSize

Introduction

In this page you can find the example usage for org.jdom2 Element getContentSize.

Prototype

@Override
    public int getContentSize() 

Source Link

Usage

From source file:ca.nrc.cadc.caom2.xml.ObservationReader.java

License:Open Source License

/**
 * Build an CoordFunction2D from a JDOM representation of an element named
 * name.// w  w w . j av a  2  s . com
 * 
 * @param name
 *            the name of the Element.
 * @param parent
 *            the parent Element.
 * @param namespace
 *            of the document.
 * @param required
 *            is the element expected to be found.
 * @return an CoordFunction2D, or null if the document doesn't contain
 *         element named name.
 * @throws ObservationParsingException
 */
protected CoordFunction2D getCoordFunction2D(String name, Element parent, Namespace namespace, boolean required)
        throws ObservationParsingException {
    Element element = getChildElement(name, parent, namespace, false);
    if (element == null || element.getContentSize() == 0) {
        return null;
    }

    Dimension2D dimension = getDimension2D("dimension", element, namespace, true);
    Coord2D refCoord = getCoord2D("refCoord", element, namespace, true);
    double cd11 = getChildTextAsDouble("cd11", element, namespace, true);
    double cd12 = getChildTextAsDouble("cd12", element, namespace, true);
    double cd21 = getChildTextAsDouble("cd21", element, namespace, true);
    double cd22 = getChildTextAsDouble("cd22", element, namespace, true);
    return new CoordFunction2D(dimension, refCoord, cd11, cd12, cd21, cd22);
}

From source file:ca.nrc.cadc.caom2.xml.ObservationReader.java

License:Open Source License

/**
 * Build an CoordRange1D from a JDOM representation of an element named
 * name.//from  www.  j av a 2 s.  c o  m
 * 
 * @param name
 *            the name of the Element.
 * @param parent
 *            the parent Element.
 * @param namespace
 *            of the document.
 * @param required
 *            is the element expected to be found.
 * @return an CoordRange1D, or null if the document doesn't contain element
 *         named name.
 * @throws ObservationParsingException
 */
protected CoordRange1D getCoordRange1D(String name, Element parent, Namespace namespace, boolean required)
        throws ObservationParsingException {
    Element element = getChildElement(name, parent, namespace, required);
    if (element == null || element.getContentSize() == 0) {
        return null;
    }

    RefCoord start = getRefCoord("start", element, namespace, true);
    RefCoord end = getRefCoord("end", element, namespace, true);
    return new CoordRange1D(start, end);
}

From source file:ca.nrc.cadc.caom2.xml.ObservationReader.java

License:Open Source License

/**
 * Build an CoordRange2D from a JDOM representation of an element named
 * name./* w  ww  . j  a  v a2  s.c  o  m*/
 * 
 * @param name
 *            the name of the Element.
 * @param parent
 *            the parent Element.
 * @param namespace
 *            of the document.
 * @param required
 *            is the element expected to be found.
 * @return an CoordRange2D, or null if the document doesn't contain element
 *         named name.
 * @throws ObservationParsingException
 */
protected CoordRange2D getCoordRange2D(String name, Element parent, Namespace namespace, boolean required)
        throws ObservationParsingException {
    Element element = getChildElement(name, parent, namespace, required);
    if (element == null || element.getContentSize() == 0) {
        return null;
    }

    Coord2D start = getCoord2D("start", element, namespace, true);
    Coord2D end = getCoord2D("end", element, namespace, true);
    return new CoordRange2D(start, end);
}

From source file:ca.nrc.cadc.caom2.xml.ObservationReader.java

License:Open Source License

/**
 * Build an Dimension2D from a JDOM representation of an element named name.
 * //  ww w. j  a v a  2  s .  c  om
 * @param name
 *            the name of the Element.
 * @param parent
 *            the parent Element.
 * @param namespace
 *            of the document.
 * @param required
 *            is the element expected to be found.
 * @return an Dimension2D, or null if the document doesn't contain element
 *         named name.
 * @throws ObservationParsingException
 */
protected Dimension2D getDimension2D(String name, Element parent, Namespace namespace, boolean required)
        throws ObservationParsingException {
    Element element = getChildElement(name, parent, namespace, false);
    if (element == null || element.getContentSize() == 0) {
        return null;
    }

    Long naxis1 = getChildTextAsLong("naxis1", element, namespace, true);
    Long naxis2 = getChildTextAsLong("naxis2", element, namespace, true);
    return new Dimension2D(naxis1, naxis2);
}

From source file:ca.nrc.cadc.caom2.xml.ObservationReader.java

License:Open Source License

/**
 * Build an RefCoord from a JDOM representation of an element named name.
 * /*from  www.j ava2 s  .  c  o m*/
 * @param name
 *            the name of the Element.
 * @param parent
 *            the parent Element.
 * @param namespace
 *            of the document.
 * @param required
 *            is the element expected to be found.
 * @return an RefCoord, or null if the document doesn't contain element
 *         named name.
 * @throws ObservationParsingException
 */
protected RefCoord getRefCoord(String name, Element parent, Namespace namespace, boolean required)
        throws ObservationParsingException {
    Element element = getChildElement(name, parent, namespace, false);
    if (element == null || element.getContentSize() == 0) {
        return null;
    }

    Double pix = getChildTextAsDouble("pix", element, namespace, true);
    Double val = getChildTextAsDouble("val", element, namespace, true);
    return new RefCoord(pix, val);
}

From source file:ca.nrc.cadc.caom2.xml.ObservationReader.java

License:Open Source License

/**
 * Build an Slice from a JDOM representation of an slice element.
 * //ww  w . j a  v  a  2 s  .c o m
 * @param name
 *            the name of the Element.
 * @param parent
 *            the parent Element.
 * @param namespace
 *            of the document.
 * @param required
 *            is the element expected to be found.
 * @return an Slice, or null if the document doesn't contain an slice
 *         element.
 * @throws ObservationParsingException
 */
protected Slice getSlice(String name, Element parent, Namespace namespace, boolean required)
        throws ObservationParsingException {
    Element element = getChildElement(name, parent, namespace, required);
    if (element == null || element.getContentSize() == 0) {
        return null;
    }

    Axis axis = getAxis("axis", element, namespace, true);
    Long bin = getChildTextAsLong("bin", element, namespace, true);
    return new Slice(axis, bin);
}

From source file:com.globalsight.dispatcher.bo.JobTask.java

License:Apache License

private void createTargetFile(JobBO p_job, String[] p_targetSegments) throws IOException {
    OutputStream writer = null;//from  w w w. ja v a  2  s . c  o m
    File fileStorage = CommonDAO.getFileStorage();
    File srcFile = p_job.getSrcFile();
    Account account = DispatcherDAOFactory.getAccountDAO().getAccount(p_job.getAccountId());
    File trgDir = CommonDAO.getFolder(fileStorage, account.getAccountName() + File.separator + p_job.getJobID()
            + File.separator + AppConstants.XLF_TARGET_FOLDER);
    File trgFile = new File(trgDir, srcFile.getName());
    FileUtils.copyFile(srcFile, trgFile);
    String encoding = FileUtil.getEncodingOfXml(trgFile);

    try {
        SAXBuilder builder = new SAXBuilder();
        Document doc = builder.build(p_job.getSrcFile());
        Element root = doc.getRootElement(); // Get root element
        Namespace namespace = root.getNamespace();
        Element fileElem = root.getChild("file", namespace);
        XPathFactory xFactory = XPathFactory.instance();
        XPathExpression<Element> expr = xFactory.compile("//trans-unit", Filters.element(), null, namespace);
        List<Element> tuList = expr.evaluate(fileElem.getChild("body", namespace));
        for (int tuIndex = 0, trgIndex = 0; tuIndex < tuList.size()
                && trgIndex < p_targetSegments.length; tuIndex++, trgIndex++) {
            if (p_targetSegments[trgIndex] == null) {
                continue;
            }

            Element elem = (Element) tuList.get(tuIndex);
            Element srcElem = elem.getChild("source", namespace);
            Element trgElem = elem.getChild("target", namespace);
            if (srcElem == null || srcElem.getContentSize() == 0) {
                trgIndex--;
                continue;
            }

            if (trgElem != null) {
                setTargetSegment(trgElem, p_targetSegments[trgIndex], encoding);
            } else {
                trgElem = new Element("target", namespace);
                setTargetSegment(trgElem, p_targetSegments[trgIndex], encoding);
                elem.addContent(trgElem);
            }

        }

        XMLOutputter xmlOutput = new XMLOutputter();
        Format format = Format.getRawFormat();
        format.setEncoding(encoding);
        writer = new FileOutputStream(trgFile);
        xmlOutput.setFormat(format);
        writeBOM(writer, format.getEncoding());
        xmlOutput.output(doc, writer);
        p_job.setTrgFile(trgFile);
        logger.info("Create Target File: " + trgFile);
    } catch (JDOMException e1) {
        logger.error("CreateTargetFile Error: ", e1);
    } catch (IOException e1) {
        logger.error("CreateTargetFile Error: ", e1);
    } finally {
        if (writer != null)
            writer.close();
    }
}

From source file:com.globalsight.dispatcher.bo.JobTask.java

License:Apache License

private void setTargetSegment(Element p_trgElement, String p_target, String p_encoding)
        throws UnsupportedEncodingException {
    if (p_target == null || p_target.trim().length() == 0)
        return;//  w w w. ja  v  a 2 s .  c  o  m

    String target = new String(p_target.getBytes("UTF-8"), p_encoding);
    try {
        StringReader stringReader = new StringReader("<target>" + p_target + "</target>");
        SAXBuilder builder = new SAXBuilder();
        Document doc = builder.build(stringReader);
        Element elem = doc.getRootElement().clone().detach();
        setNamespace(elem, p_trgElement.getNamespace());
        //Delete Original Target Segment.
        p_trgElement.removeContent();
        for (int i = 0; i < elem.getContentSize(); i++) {
            p_trgElement.addContent(elem.getContent(i).clone().detach());
        }
    } catch (Exception e) {
        p_trgElement.setText(target);
    }
}

From source file:com.globalsight.dispatcher.controller.TranslateXLFController.java

License:Apache License

private String parseXLF(JobBO p_job, File p_srcFile) {
    if (p_srcFile == null || !p_srcFile.exists())
        return "File not exits.";

    String srcLang, trgLang;//  w w w. ja  v a  2  s .  com
    List<String> srcSegments = new ArrayList<String>();

    try {
        SAXBuilder builder = new SAXBuilder();
        Document read_doc = builder.build(p_srcFile);
        // Get Root Element
        Element root = read_doc.getRootElement();
        Namespace namespace = root.getNamespace();
        Element fileElem = root.getChild("file", namespace);
        // Get Source/Target Language
        srcLang = fileElem.getAttributeValue(XLF_SOURCE_LANGUAGE);
        trgLang = fileElem.getAttributeValue(XLF_TARGET_LANGUAGE);
        XPathFactory xFactory = XPathFactory.instance();
        XPathExpression<Element> expr = xFactory.compile("//trans-unit", Filters.element(), null, namespace);
        List<Element> list = expr.evaluate(fileElem.getChild("body", namespace));
        for (int i = 0; i < list.size(); i++) {
            Element tuElem = (Element) list.get(i);
            Element srcElem = tuElem.getChild("source", namespace);
            // Get Source Segment 
            if (srcElem != null && srcElem.getContentSize() > 0) {
                String source = getInnerXMLString(srcElem);
                srcSegments.add(source);
            }
        }

        p_job.setSourceLanguage(srcLang);
        p_job.setTargetLanguage(trgLang);
        p_job.setSourceSegments(srcSegments);
    } catch (Exception e) {
        String msg = "Parse XLIFF file error.";
        logger.error(msg, e);
        return msg;
    }

    return null;
}

From source file:de.mud.flash.FlashTerminal.java

License:Open Source License

/**
 * Redraw terminal (send new/changed terminal lines to flash frontend).
 *///from  w w  w .  j  av  a  2  s .c  om
@Override
public void redraw() {
    log.debug("redraw()");

    if (terminalReady && writer != null) {
        try {
            // remove children from terminal
            terminal.removeContent();
            if (simpleMode) {
                Element result = redrawSimpleTerminal(terminal);
                if (result.getContentSize() > 0) {
                    xmlOutputter.output(result, writer);
                }
            } else {
                xmlOutputter.output(redrawFullTerminal(terminal), writer);
            }
            writer.write(0);
            writer.flush();
            log.debug("flushed data ...");
        } catch (IOException e) {
            log.error("error writing to client: " + e);
            writer = null;
        }
    }
}