Example usage for org.dom4j Element elementText

List of usage examples for org.dom4j Element elementText

Introduction

In this page you can find the example usage for org.dom4j Element elementText.

Prototype

String elementText(QName qname);

Source Link

Usage

From source file:com.glaf.wechat.sdk.message.handler.LocationMessageHandler.java

License:Apache License

@Override
protected void parseSpecialMessage(Message message, Element root) {
    LocationMessage msg = (LocationMessage) message;
    msg.setLongitude(Double.parseDouble(root.elementText(TAG_LONGITUDE)));
    msg.setLatitude(Double.parseDouble(root.elementText(TAG_LATITUDE)));
    msg.setLabel(root.elementText(TAG_LABEL));
    msg.setScale(root.elementText(TAG_SCALE));
}

From source file:com.glaf.wechat.sdk.message.handler.TextMessageHandler.java

License:Apache License

@Override
protected void parseSpecialMessage(Message message, Element root) {
    TextMessage msg = (TextMessage) message;
    msg.setMsgType(message.getMsgType());
    msg.setContent(root.elementText(TAG_CONTENT));
}

From source file:com.glaf.wechat.sdk.message.handler.VideoMessageHandler.java

License:Apache License

@Override
protected void parseSpecialMessage(Message message, Element root) {
    VideoMessage msg = (VideoMessage) message;
    msg.setMsgType(message.getMsgType());
    msg.setThumbMediaId(root.elementText(TAG_THUMBMEDIAID));
    msg.setMediaId(root.elementText(TAG_MEDIAID));
}

From source file:com.glaf.wechat.sdk.message.handler.VoiceMessageHandler.java

License:Apache License

@Override
protected void parseSpecialMessage(Message message, Element root) {
    VoiceMessage msg = (VoiceMessage) message;
    msg.setMsgType(message.getMsgType());
    msg.setFormat(root.elementText(TAG_FORMAT));
    msg.setMediaId(root.elementText(TAG_MEDIAID));
}

From source file:com.glaf.wechat.sdk.WeixinExecutor.java

License:Apache License

private void parseInputStream() throws Exception {
    SAXReader xmlReader = new SAXReader();
    Document doc = null;//from  w w  w. j a  va  2s .  c o m
    try {
        doc = xmlReader.read(request.getInputStream());
    } catch (DocumentException ex) {
        ex.printStackTrace();
        doc = xmlReader.read(new ByteArrayInputStream(request.getParameter("xml").getBytes("UTF-8")));
    }

    String uri = request.getRequestURI();
    String id = uri.substring(uri.lastIndexOf("/") + 1);
    Long accountId = Long.parseLong(id);
    WxUser user = WxIdentityFactory.getUserByAccountId(accountId);

    Element root = doc.getRootElement();
    logger.debug(root.asXML());

    String type = root.elementText(IMessage.TAG_MSGTYPE);

    if (StringUtils.equalsIgnoreCase(type, MESSAGE_TEXT)) {
        message = new TextMessage();
        messageHandler = new TextMessageHandler();
    } else if (StringUtils.equalsIgnoreCase(type, MESSAGE_EVENT)) {
        // do subscribe event
        message = new EventMessage();
        messageHandler = new EventMessageHandler();
    } else if (StringUtils.equalsIgnoreCase(type, MESSAGE_IMAGE)) {
        message = new ImageMessage();
        messageHandler = new ImageMessageHandler();
    } else if (StringUtils.equalsIgnoreCase(type, MESSAGE_LINK)) {
        message = new LinkMessage();
        messageHandler = new LinkMessageHandler();
    } else if (StringUtils.equalsIgnoreCase(type, MESSAGE_LOCATION)) {
        message = new LocationMessage();
        messageHandler = new LocationMessageHandler();
    } else if (StringUtils.equalsIgnoreCase(type, MESSAGE_VOICE)) {
        message = new VoiceMessage();
        messageHandler = new VoiceMessageHandler();
    } else if (StringUtils.equalsIgnoreCase(type, MESSAGE_VIDEO)) {
        message = new VideoMessage();
        messageHandler = new VideoMessageHandler();
    }

    message.setRoot(root);
    message.setAccountId(accountId);
    message.setCustomer(user.getActorId());
    message.setContextPath(request.getContextPath());
    message.setRequestParameters(RequestUtils.getParameterMap(request));
    String serviceUrl = WechatUtils.getServiceUrl(request);
    message.setServiceUrl(serviceUrl);
    message.setRemoteIPAddr(RequestUtils.getIPAddress(request));

    // do the default/common parse!
    messageHandler.parseMessage(message, root);
}

From source file:com.globalsight.everest.aligner.AlignerPackageOptions.java

License:Apache License

/**
 * Initializes this object from an XML string.
 *///from w ww.  ja  v a  2  s  .c  o  m
public void init(String p_options) throws Exception {
    XmlParser parser = null;
    Document dom = null;
    Element elem;
    List nodes;

    clearFilePairs();
    clearExtensions();

    try {
        parser = XmlParser.hire();
        dom = parser.parseXml(p_options);

        Element root = dom.getRootElement();

        m_packageName = root.elementText("packageName");

        elem = (Element) root.selectSingleNode("//extractionOptions");
        m_extractionOptions.m_formatType = elem.elementText("formatType");
        m_extractionOptions.m_rules = elem.elementText("rules");
        m_extractionOptions.m_sourceEncoding = elem.elementText("sourceEncoding");
        m_extractionOptions.m_targetEncoding = elem.elementText("targetEncoding");
        m_extractionOptions.m_sourceLocale = elem.elementText("sourceLocale");
        m_extractionOptions.m_targetLocale = elem.elementText("targetLocale");

        nodes = root.selectNodes("//extractionOptions//extension");
        for (int i = 0, max = nodes.size(); i < max; i++) {
            elem = (Element) nodes.get(i);

            String ext = elem.getText();

            addExtension(ext);
        }

        nodes = root.selectNodes("//filePair");
        for (int i = 0, max = nodes.size(); i < max; i++) {
            elem = (Element) nodes.get(i);

            FilePair pair = new FilePair(elem.elementText("source"), elem.elementText("target"));

            addFilePair(pair);
        }

        elem = (Element) root.selectSingleNode("//alignerOptions");
        m_alignerOptions.m_ignoreFormatting = elem.elementText("ignoreFormatting");
    } finally {
        XmlParser.fire(parser);
    }
}

From source file:com.globalsight.everest.aligner.AlignerPackageUploadOptions.java

License:Apache License

/**
 * Initializes this object from an XML string.
 */// w  ww  . j a va  2  s.c o m
public void init(String p_options) throws Exception {
    XmlParser parser = null;
    Document dom = null;
    Element elem;

    try {
        parser = XmlParser.hire();
        dom = parser.parseXml(p_options);

        Element root = dom.getRootElement();

        elem = (Element) root.selectSingleNode("//fileOptions");
        m_fileOptions.m_fileName = elem.elementText("fileName");

        elem = (Element) root.selectSingleNode("//tmOptions");
        m_tmOptions.m_tmName = elem.elementText("tmName");
        m_tmOptions.m_syncMode = elem.elementText("syncMode");
    } finally {
        XmlParser.fire(parser);
    }
}

From source file:com.globalsight.everest.projecthandler.exporter.ExportOptions.java

License:Apache License

/**
 * Reads and validates a ExportOptions XML string.
 *//*w  w  w. ja  va 2 s .  c  o m*/
protected void initOther(Element p_root) throws ExporterException {
    try {
        Element elem = (Element) p_root.selectSingleNode("//selectOptions");
        m_selectOptions.m_selectMode = elem.elementText("selectMode");
        m_selectOptions.m_selectFilter = elem.elementText("selectFilter");

        elem = (Element) p_root.selectSingleNode("//outputOptions");
        m_outputOptions.m_header = elem.elementText("header");
        m_outputOptions.m_separator = elem.elementText("separator");
        m_outputOptions.m_subtype = elem.elementText("subtype");
    } catch (Exception e) {
        // cast exception and throw
        error(e.getMessage(), e);
    }
}

From source file:com.globalsight.everest.projecthandler.importer.ImportOptions.java

License:Apache License

/**
 * Reads and validates a ImportOptions XML string.
 *
 * Xml Format:/*from   w w  w.  ja v a2  s.c om*/
 */
protected void initOther(Element p_root) throws ImporterException {
    try {
        clearColumns();

        List columns = p_root.selectNodes("/importOptions/columnOptions/column");
        for (int i = 0; i < columns.size(); ++i) {
            Element col = (Element) columns.get(i);
            ColumnDescriptor desc = new ColumnDescriptor();

            desc.m_position = Integer.parseInt(col.attributeValue("id"));
            desc.m_name = col.elementText("name");
            desc.m_example = col.elementText("example");
            desc.m_type = col.elementText("type");
            desc.m_subtype = col.elementText("subtype");

            m_columns.add(desc);
        }

        Element elem = (Element) p_root.selectSingleNode("/importOptions/syncOptions");

        m_syncOptions.m_syncMode = elem.elementText("syncMode");
    } catch (Exception e) {
        // cast exception and throw
        error(e.getMessage(), e);
    }
}

From source file:com.globalsight.everest.segmentationhelper.XmlLoader.java

License:Apache License

public static SegmentationRule parseSegmentationRule(String p_xmlText) throws Exception {
    Document doc = parseWithSAX(p_xmlText);
    Element root = doc.getRootElement();
    SegmentationRule segmentationRule = new SegmentationRule();
    SrxHeader header = new SrxHeader();
    HashMap<String, String> formathandle = new HashMap<String, String>();
    HashMap<String, ArrayList<Rule>> langrules = new HashMap<String, ArrayList<Rule>>();
    ArrayList<LanguageMap> langruleMap = new ArrayList<LanguageMap>();

    segmentationRule.setRootName(root.getQualifiedName());
    // Now it only supports SRX2.0.
    segmentationRule.setVersion(root.attributeValue("version"));

    Element headerElement = root.element("header");
    Element bodyElement = root.element("body");
    Element maprulesElement = bodyElement.element("maprules");
    // to get SRX header informations
    if (headerElement != null) {
        // option segmentsubflows
        String segsub = headerElement.attributeValue("segmentsubflows");
        if (segsub == null) {
            // Take default value.
            header.isSegmentsubflows(true);
        } else {/*w ww .  j  av a  2  s .c o m*/
            header.isSegmentsubflows(!segsub.equalsIgnoreCase("no"));
        }

        // option cascade
        String cascade = headerElement.attributeValue("cascade");
        if (cascade == null) {
            header.isCascade(false);
        } else {
            header.isCascade(!cascade.equalsIgnoreCase("no"));
        }

        // elements : formathandle
        List formatList = headerElement.elements("formathandle");
        Iterator formatIter = formatList != null ? formatList.iterator() : (new ArrayList()).iterator();
        // If the header does not contain formathandle elements
        // we use the default values
        if (!formatIter.hasNext()) {
            formathandle.put("start", "no");
            formathandle.put("end", "yes");
            formathandle.put("isolated", "no");
        }
        // If the header contains formathandle elements
        // we use the values specified by formathandle elements
        else {
            while (formatIter.hasNext()) {
                Element formatElement = (Element) formatIter.next();
                String type = formatElement.attributeValue("type");
                String include = formatElement.attributeValue("include");
                formathandle.put(type, include);
            }
        }
        header.setFormatHandle(formathandle);

        // okpsrx extension
        Element okpOptions = headerElement.element("options");
        Element okpSample = headerElement.element("sample");
        Element okpRangeRule = headerElement.element("rangeRule");

        if (okpOptions != null) {
            String oneSegmentIncludesAll = okpOptions.attributeValue("oneSegmentIncludesAll");
            String trimLeadingWs = okpOptions.attributeValue("trimLeadingWhitespaces");
            String trimTrailingWs = okpOptions.attributeValue("trimTrailingWhitespaces");

            header.setOneSegmentIncludesAll("yes".equalsIgnoreCase(oneSegmentIncludesAll));
            header.setTrimLeadingWhitespaces("yes".equalsIgnoreCase(trimLeadingWs));
            header.setTrimTrailingWhitespaces("yes".equalsIgnoreCase(trimTrailingWs));
        }

        if (okpSample != null) {
            String language = okpSample.attributeValue("language");
            String useMappedRules = okpSample.attributeValue("useMappedRules");
            String sample = okpSample.getText();

            header.setSample(sample);
            header.setSampleLanguage(language);
            header.setUseMappedRulesForSample(!"no".equalsIgnoreCase(useMappedRules));
        }

        if (okpRangeRule != null) {
            String rangeRule = okpRangeRule.getText();
            header.setRangeRule(rangeRule);
        }
    }

    if (bodyElement != null) {
        Element languagerulesElement = bodyElement.element("languagerules");
        if (languagerulesElement != null) {
            Iterator languageruleIter = languagerulesElement.elementIterator();
            while (languageruleIter.hasNext()) {
                Element languageruleElement = (Element) languageruleIter.next();
                String languageName = languageruleElement.attributeValue("languagerulename");
                Iterator ruleIter = languageruleElement.elementIterator();
                ArrayList<Rule> rules = new ArrayList<Rule>();
                while (ruleIter.hasNext()) {
                    Element ruleSub = (Element) ruleIter.next();
                    String breakvalue = ruleSub.attributeValue("break");
                    String beforebreak = ruleSub.elementText("beforebreak");
                    String afterbreak = ruleSub.elementText("afterbreak");
                    Rule rule = new Rule();
                    if (breakvalue == null) {
                        // Take default value.
                        rule.isBreak(true);
                    } else {
                        if (breakvalue.equalsIgnoreCase("no")) {
                            rule.isBreak(false);
                        } else {
                            rule.isBreak(true);
                        }
                    }

                    // System.out.println(rule.getBreak());
                    rule.setAfterBreak(afterbreak);
                    rule.setBeforeBreak(beforebreak);
                    rules.add(rule);
                }
                langrules.put(languageName, rules);
            }
        } // end languageruleElement
    } // end bodyElement

    if (maprulesElement != null) {
        Iterator languagemapIter = maprulesElement.elementIterator();
        while (languagemapIter.hasNext()) {
            Element languagemapElement = (Element) languagemapIter.next();
            String languagepattern = languagemapElement.attributeValue("languagepattern");
            String languagerulename = languagemapElement.attributeValue("languagerulename");
            LanguageMap langMap = new LanguageMap();
            langMap.setLanguagePattern(languagepattern);
            langMap.setLanguageruleName(languagerulename);
            langruleMap.add(langMap);
        }
    }

    segmentationRule.setHeader(header);
    segmentationRule.setLanguageMap(langruleMap);
    segmentationRule.setRules(langrules);
    return segmentationRule;
}