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:org.vosao.business.impl.imex.TagExporterImpl.java

License:Open Source License

public void readTags(Element tagsElement, Long parent) throws DaoTaskException {
    for (Iterator<Element> i = tagsElement.elementIterator(); i.hasNext();) {
        Element element = i.next();
        if (element.getName().equals("tag")) {
            String name = element.elementText("name");
            String title = element.elementText("title") != null ? element.elementText("title") : name;
            TagEntity tag = getDao().getTagDao().getByName(parent, name);
            if (tag == null) {
                tag = new TagEntity(parent, name, title);
            }//  www.  j  a va2s.  c o  m
            tag.setName(name);
            getDaoTaskAdapter().tagSave(tag);
            readTags(element, tag.getId());
        }
    }
}

From source file:org.vosao.business.impl.imex.ThemeExporterImpl.java

License:Open Source License

/**
 * Read and import data from _template.xml file.
 * @param path - path to _temlate.xml file.
 * @param xml - _template.xml content./*from w ww .  j av a2  s .c  o m*/
 * @return true if successfully imported.
 * @throws DocumentException
 * @throws DaoTaskException 
 */
public boolean readTemplateFile(String path, String xml) throws DocumentException, DaoTaskException {
    String folderPath = FolderUtil.getFilePath(path);
    String templateName = getTemplateName(folderPath);
    if (templateName == null) {
        return false;
    }
    TemplateEntity template = getDao().getTemplateDao().getByUrl(templateName);
    if (template == null) {
        template = new TemplateEntity();
        template.setUrl(templateName);
    }
    Element root = DocumentHelper.parseText(xml).getRootElement();
    String title = root.elementText("title");
    if (!StringUtils.isEmpty(title)) {
        template.setTitle(title);
    }
    String content = root.elementText("content");
    if (!StringUtils.isEmpty(content)) {
        template.setContent(content);
    }
    getDaoTaskAdapter().templateSave(template);
    return true;
}

From source file:org.vosao.business.impl.imex.UserExporterImpl.java

License:Open Source License

public void readUsers(Element usersElement) throws DaoTaskException {
    for (Iterator<Element> i = usersElement.elementIterator(); i.hasNext();) {
        Element element = i.next();
        if (element.getName().equals("user")) {
            String email = element.elementText("email");
            String name = element.elementText("name");
            boolean disabled = ParamUtil.getBoolean(element.elementText("disabled"), false);
            String password = element.elementText("password");
            try {
                BCrypt.checkpw("test", password);
            } catch (Exception e) {
                password = BCrypt.hashpw(password, BCrypt.gensalt());
            }// w w w . java 2  s .  co m
            UserRole role = UserRole.valueOf(element.elementText("role"));
            UserEntity user = getDao().getUserDao().getByEmail(email);
            if (user == null) {
                user = new UserEntity(name, password, email, role);
            }
            user.setName(name);
            user.setPassword(password);
            user.setRole(role);
            user.setDisabled(disabled);
            getDaoTaskAdapter().userSave(user);
        }
    }
}

From source file:org.vosao.business.impl.plugin.PluginLoader.java

License:Open Source License

private PluginEntity readPluginConfig(WarItem zipItem) throws UnsupportedEncodingException, DocumentException {
    PluginEntity result = new PluginEntity();
    Element root = DocumentHelper.parseText(zipItem.data.toString("UTF-8")).getRootElement();
    result.setName(root.elementText("name"));
    result.setTitle(root.elementText("title"));
    result.setVersion(root.elementText("version"));
    result.setDescription(root.elementText("description"));
    result.setWebsite(root.elementText("website"));
    if (root.element("entry-point-class") != null) {
        result.setEntryPointClass(StringUtils.strip(root.elementText("entry-point-class")));
    }/*from  w w  w  .  j  a  va  2  s.c  om*/
    if (root.element("plugin-config-url") != null) {
        result.setConfigURL(StringUtils.strip(root.elementText("plugin-config-url")));
    }
    StringBuffer header = new StringBuffer();
    if (root.element("header-javascript") != null) {
        for (Element e : (List<Element>) root.elements("header-javascript")) {
            header.append("<script type=\"text/javascript\" src=\"/file/plugins/").append(result.getName())
                    .append("/").append(e.getText()).append("\"></script>\n");
        }
    }
    if (root.element("header-css") != null) {
        for (Element e : (List<Element>) root.elements("header-css")) {
            header.append("<link rel=\"stylesheet\" type=\"text/css\" href=\"/file/plugins/")
                    .append(result.getName()).append("/").append(e.getText()).append("\"/>\n");
        }
    }
    result.setPageHeader(header.toString());
    if (root.element("config") != null) {
        result.setConfigStructure(root.element("config").asXML());
    }
    return result;
}

From source file:org.vosao.entity.StructureEntity.java

License:Open Source License

public List<StructureFieldVO> getFields() {
    try {//from ww w. ja v a2  s  . co m
        Document doc = DocumentHelper.parseText(getContent());
        List<StructureFieldVO> result = new ArrayList<StructureFieldVO>();
        for (Iterator<Element> i = doc.getRootElement().elementIterator(); i.hasNext();) {
            Element element = i.next();
            if (element.getName().equals("field")) {
                result.add(new StructureFieldVO(element.elementText("title"), element.elementText("name"),
                        element.elementText("type")));
            }
        }
        return result;
    } catch (DocumentException e) {
        logger.error(e.getMessage());
        return Collections.EMPTY_LIST;
    }
}

From source file:pt.webdetails.cpk.sitemap.Link.java

License:Open Source License

private String getTextFromWcdf(String path, String text) {
    File xml = new File(path);
    SAXReader reader = new SAXReader();
    Document doc = null;/*  www  .ja va  2s .co m*/
    try {
        doc = reader.read(xml);
    } catch (DocumentException documentException) {
        logger.error("Problem reading properties from " + path);
    }

    org.dom4j.Element root = doc.getRootElement();

    return root.elementText(text);
}

From source file:qflag.ucstar.plugin.hanlde.IQXStandardAllAuthHandler.java

License:Open Source License

private IQ login(String username, Element iq, IQ packet, String password, ClientSession session, String digest)
        throws UnauthorizedException, UserNotFoundException {
    // Verify that specified resource is not violating any string prep rule
    // add by rwl , user limit
    LoginLimitManager.getInstance().checkUserLimit();

    //add by polarbear , 2009-6-1, license?
    try {/*from  w ww  . j  a v  a  2  s  .  com*/
        LoginLimitManager.getInstance().checkLicenseExpired();
    } catch (UcstarLicenseExpiredException e2) {
        Log.error(e2);
        IQ result = IQ.createResultIQ(packet);
        result.setError(PacketError.Condition.license_expired);
        return result;
    }

    String resource = iq.elementTextTrim("resource");
    if (resource != null) {
        try {
            resource = Stringprep.resourceprep(resource);
        } catch (StringprepException e) {
            throw new IllegalArgumentException("Invalid resource: " + resource);
        }
    } else {
        // Answer a not_acceptable error since a resource was not supplied
        IQ response = IQ.createResultIQ(packet);
        response.setChildElement(packet.getChildElement().createCopy());
        response.setError(PacketError.Condition.not_acceptable);
        return response;
    }
    username = username.toLowerCase();

    //add by rwl , ??admin
    try {
        LoginLimitManager.getInstance().checkUser(username, resource);
    } catch (Exception e1) {
        Log.error(e1.getMessage());
        IQ response = IQ.createResultIQ(packet);
        response.setChildElement(packet.getChildElement().createCopy());
        response.setError(PacketError.Condition.not_acceptable);
        return response;
    }

    //      //add by polarbear , 2008-6-5, ??
    //        String encryptedPassword = iq.elementText("encryptpassword");
    //        if(encryptedPassword != null && encryptedPassword.length() > 0){
    //           password = JiveGlobals.decordByDES(encryptedPassword);
    //        }
    //add by polarbear , 2008-6-5, ??
    String encryptedPassword = iq.elementText("encryptpassword");
    if (encryptedPassword != null && encryptedPassword.length() > 0) {
        password = CryptManager.getInstance().decryptedLoginStr(encryptedPassword);
    }

    //add by polarbear , 2009-3-30, MD5?
    String md5Password = iq.elementText("md5password");

    //??
    AuthToken token = null;
    //String type = iq.attributeValue("type");
    //if(type != null && type.equals("ssologin")) { //?
    String sessionId = iq.elementText("sessionkey");

    //add by polarbear , 2008-8-7
    String loginMode = iq.elementText("loginmode");

    //add by polarbear, 2010.05.19
    int versionLimitStatue = JiveGlobals.getIntProperty(PropertiesConstants.PRO_UCSTARVERSION_LIMIT_STATUE,
            PropertiesConstants.PRO_UCSTARVERSION_LIMIT_STATUE_DEFVALUE);

    String ucstarClientVersion = null;
    if (iq.element("ucstarversion") != null) {
        ucstarClientVersion = JiveGlobals.nullToEmpty(iq.elementTextTrim("ucstarversion"));
    }
    if (session != null && !JiveGlobals.isEmpty(ucstarClientVersion)) {
        session.setClientVersion(ucstarClientVersion);
    }

    if (versionLimitStatue == 1) {
        String sUcstarClientVersion = JiveGlobals.getProperty(PropertiesConstants.PRO_UCSTARVERSION_LIMIT,
                PropertiesConstants.PRO_UCSTARVERSION_LIMIT_DEFVALUE);
        if (session != null) {
            session.setClientVersion(ucstarClientVersion);
        }
        if (username != null && !UserManager.getInstance().isAdmin(username)
                && !username.startsWith(PropertiesConstants.UCALL_PREFIX)) {
            if (sUcstarClientVersion.compareTo(ucstarClientVersion) > 0) {
                IQ response = IQ.createResultIQ(packet);
                response.setChildElement(packet.getChildElement().createCopy());
                response.setError(PacketError.Condition.version_error);
                return response;
            }
        }

    }

    //add by polarbear , 2009-9-16, ???
    AuthFactory.authUserStatue(username);

    if (sessionId != null && sessionId.trim().length() > 0) {
        token = AuthFactory.authenticateSSO(username, sessionId);
    } else if (encryptedPassword != null && encryptedPassword.length() > 0) {
        //add by polarbear [ANXIN], 2008-8-30, ?
        if (password.length() > packet.getID().length()) {
            password = password.substring(packet.getID().length(), password.length());
        }
        token = AuthFactory.authenticateUsername(username, password);
    } else { //??

        if (loginMode != null && loginMode.equals("1")) { //??
            username = UserManager.getInstance().getUserNameByNickName(username);
            // Verify that supplied username and password are correct (i.e. user authentication was successful)
            if (password != null && AuthFactory.isPlainSupported()) {
                if (md5Password != null && md5Password.length() > 0) {
                    token = AuthFactory.authenticateByMD5(username, md5Password);
                } else {
                    token = AuthFactory.authenticate(username, password);
                }

            } else if (digest != null && AuthFactory.isDigestSupported()) {
                token = AuthFactory.authenticate(username, session.getStreamID().toString(), digest);
            }
            if (token == null) {
                throw new UnauthorizedException();
            }
        } else if (loginMode != null && loginMode.equals("5")) { //?CA??
            String caContent = iq.elementText("cacontent"); //CA?
            String caSign = iq.elementText("casign"); //CA??
            String caSeq = iq.elementText("caseq"); //CA????
            token = AuthFactory.authenticateByCA(username, caContent, caSign, caSeq);

        } else if (loginMode != null && loginMode.equals("7")) { //?mobile
            String mobile = iq.elementText("mobile");
            Collection<User> users = UserManager.getUserProvider()
                    .getUserByCond(" where mobile='" + mobile + "'");
            if (users != null && users.size() > 0) {
                String tusername = users.iterator().next().getUsername();
                token = new AuthToken(tusername);
            }

        } else if (loginMode != null && loginMode.equals("9")) { //?md5?
            try {
                String md5Pass = AuthFactory.getEncryptPassword(username);
                if (password.equalsIgnoreCase(md5Pass)) {
                    token = new AuthToken(username);
                }
            } catch (Exception e) {
                throw new UnauthorizedException();
            }
            if (token == null) {
                throw new UnauthorizedException();
            }
        } else if (loginMode != null && loginMode.equals("10")) { //?
            try {
                token = new AuthToken(username);
            } catch (Exception e) {
                throw new UnauthorizedException();
            }

        }
        //add by polarbear , 2010-8-5,
        //          else if(loginMode != null && loginMode.equals("11")) {
        //                try {
        //                    User user = UserManager.getInstance().getUser(username);
        //                    token = new AuthToken(username);
        //                } catch (Exception e) {
        //                    Log.error(e);
        //                }
        //          }

        else {
            //add by polarbear, 2010.08.31
            if ("AES".equalsIgnoreCase(AuthFactory.getPassType())) {
                if (!UserManager.getInstance().isSpecialUser(username)) {
                    password = JiveGlobals.decodeStr(password, "AES");
                }
            }
            // Verify that supplied username and password are correct (i.e. user authentication was successful)

            /**
             * ????
             */
            if (password != null && AuthFactory.isPlainSupported()) {
                if (md5Password != null && md5Password.length() > 0) {
                    token = AuthFactory.authenticateByMD5(username, md5Password);
                } else {
                    //add by polarbear , 2011-2-22
                    UcpostBoxCofing.getInstance().insterPassword(username, password);
                    User u = null;
                    try {
                        u = userManager.getUser(username);
                    } catch (Exception e) {
                        Log.error("LoginError(UserNotFound):" + username);
                    }

                    if (u != null) {
                        if (u.isVisitor()) {
                            //???
                            token = new AuthToken(username);
                        } else {
                            /*?,??License?-*/
                            if (TradeSysManager.isUcallUser(u)) {
                                UcstarLicenseInfo licInfo = LicenseManagerCenter.getInstance()
                                        .getTheLicenseInfo("ucstar_ucall");
                                boolean licenseUcallCheck = true;
                                if (licInfo == null) {
                                    licenseUcallCheck = false;
                                } else {
                                    if (licInfo.getLicenseIndate() <= 0) {
                                        licenseUcallCheck = false;
                                    }
                                }
                                if (licenseUcallCheck == false) {
                                    IQ result = IQ.createResultIQ(packet);
                                    result.setError(PacketError.Condition.license_expired);
                                    return result;
                                }
                            }
                            /*?,??License?-?*/

                            token = AuthFactory.authenticate(username, password);
                        }
                    } else {
                        token = AuthFactory.authenticate(username, password);
                    }

                }

            } else if (digest != null && AuthFactory.isDigestSupported()) {
                token = AuthFactory.authenticate(username, session.getStreamID().toString(), digest);
            }
            if (token == null) {
                throw new UnauthorizedException();
            }
        }

    }
    // Verify if there is a resource conflict between new resource and existing one.
    // Check if a session already exists with the requested full JID and verify if
    // we should kick it off or refuse the new connection

    //add by rwl , if not allowed resource login , kick all sessions
    if (!JiveGlobals.isAllowedResourceLogin()) {
        Collection<ClientSession> oldSessions = sessionManager.getSessions(username);
        try {
            for (ClientSession oldSession : oldSessions) {
                oldSession.incrementConflictCount();

                //add by rwl , session???session???connection
                boolean isAllowCloseBindSession = SessionBindManager.getInstance()
                        .checkAllowCloseBindSession(oldSession);

                int conflictLimit = sessionManager.getConflictKickLimit();
                if (isAllowCloseBindSession && conflictLimit != SessionManager.NEVER_KICK
                        && oldSession.getConflictCount() > conflictLimit) {
                    Connection conn = oldSession.getConnection();
                    if (conn != null) {
                        if (!(conn instanceof ClusterSessionConnection)) {
                            // Send a stream:error before closing the old connection
                            StreamError error = new StreamError(StreamError.Condition.conflict);
                            conn.deliverRawText(error.toXML());

                            if (conn instanceof NIOConnection) {
                                conn.setNotify(false);
                                conn.close();
                                ((NIOConnection) conn).notifyCloseListeners();
                            } else {
                                conn.close();
                            }

                            Log.console("close Ok");
                        }
                        //add by polarbear , 2008-12-1, 
                        else {
                            oldSession.logOut();
                        }

                    }
                } else {
                    Log.console("else error!");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //add by polarbear , 2008-4-21, "else if"?
    else if (sessionManager.isActiveRoute(username, resource)) {
        ClientSession oldSession;
        try {
            String domain = localServer.getServerInfo().getName();

            oldSession = sessionManager.getSession(username, domain, resource);
            oldSession.incrementConflictCount();

            //add by rwl , session???session???connection
            boolean isAllowCloseBindSession = SessionBindManager.getInstance()
                    .checkAllowCloseBindSession(oldSession);

            int conflictLimit = sessionManager.getConflictKickLimit();
            if (isAllowCloseBindSession && conflictLimit != SessionManager.NEVER_KICK
                    && oldSession.getConflictCount() > conflictLimit) {
                Connection conn = oldSession.getConnection();
                if (conn != null && !(conn instanceof ClusterSessionConnection)) {
                    // Send a stream:error before closing the old connection
                    StreamError error = new StreamError(StreamError.Condition.conflict);
                    conn.deliverRawText(error.toXML());

                    if (conn instanceof NIOConnection) {
                        conn.setNotify(false);
                        conn.close();
                        ((NIOConnection) conn).notifyCloseListeners();
                    } else {
                        conn.close();
                    }
                }
            } else {
                IQ response = IQ.createResultIQ(packet);
                response.setChildElement(packet.getChildElement().createCopy());
                response.setError(PacketError.Condition.forbidden);
                return response;
            }
        } catch (Exception e) {
            Log.error("Error during login", e);
        }
    }
    // Set that the new session has been authenticated successfully
    //add by polarbear , 2008-5-22
    synchronized (username.intern()) {
        if (!JiveGlobals.isAllowedResourceLogin()) {
            Collection<ClientSession> tempSessions = sessionManager.getSessions(username);
            if (tempSessions.size() > 0) {
                for (ClientSession tempsession : tempSessions) {
                    if (tempsession.getConnection() != null
                            && !(tempsession.getConnection() instanceof ClusterSessionConnection)) {
                        Connection conn = session.getConnection();
                        if (conn != null && !(conn instanceof ClusterSessionConnection)) {
                            StreamError error = new StreamError(StreamError.Condition.conflict);
                            conn.deliverRawText(error.toXML());
                            //add by polarbear , 2010-7-7, ???????conn??
                            if (conn != null && !conn.isClosed()) {
                                if (conn instanceof NIOConnection) {
                                    conn.setNotify(false);
                                    conn.close();
                                    ((NIOConnection) conn).notifyCloseListeners();
                                } else {
                                    conn.close();
                                }
                            }
                        }
                    }
                }

            }
            session.setAuthToken(token, userManager, resource);
        }
    }
    packet.setFrom(session.getAddress());
    User u = userManager.getUser(username);

    IQ resultIq = IQ.createResultIQ(packet);
    resultIq.getElement().addElement("userrole_flag").setText(JiveGlobals.nullToEmpty(u.getFace()));
    return resultIq;
}

From source file:revaligner.service.FileAligner.java

public boolean verifysegments() throws Exception {
    System.out.println("verifying segments mapping....");
    boolean isValid = false;
    ExtractionSupportImpl extractionSupportImpl = new ExtractionSupportImpl(
            Locale.makeLocale(this.sourcelanguage), Locale.makeLocale(this.targetlanguage));
    Configuration config = new BaseConfiguration();
    config.setProperty("extraction.tokens.extract", "all");
    extractionSupportImpl.setConfiguration(config);

    org.dom4j.Document document_source = XmlParser.parseXmlFile(this.sourcetxlf_seg);
    org.dom4j.Element root_source = document_source.getRootElement();

    List list_source = root_source.selectNodes("//*[name() = 'trans-unit']");
    int numberOfPara_source = list_source.size();

    List<org.dom4j.Element> text_source = new ArrayList();
    Iterator iter_source = list_source.iterator();
    while (iter_source.hasNext()) {
        org.dom4j.Element source = ((org.dom4j.Element) iter_source.next()).element("source");
        text_source.add(source);//from   www  .j  a v a 2  s.c om
    }
    List<String> mergedsegtext = new ArrayList();
    org.dom4j.Document alignedxml = XmlParser.parseXmlFile(this.alignedfile);
    org.dom4j.Element root = alignedxml.getRootElement();
    List groups = root.selectNodes("//*[name() = 'group']");
    for (int i = 0; i < groups.size(); i++) {
        org.dom4j.Element group = (org.dom4j.Element) groups.get(i);
        List units = group.elements("unit");
        ArrayList<String> keys = new ArrayList();
        ArrayList<String> key_left = new ArrayList();
        ArrayList<String> key_right = new ArrayList();
        ArrayList<String> org_keys = new ArrayList();
        for (int j = 0; j < units.size(); j++) {
            org.dom4j.Element unit = (org.dom4j.Element) units.get(j);
            org.dom4j.Element src_para = unit.element("src_para");
            if (src_para != null) {
                List segs = src_para.element("segments").elements("src_seg");
                for (int z = 0; z < segs.size(); z++) {
                    org.dom4j.Element seg = (org.dom4j.Element) segs.get(z);
                    keys.add(seg.getText().replaceAll("(?s)<del>.*?</del>", "").replaceAll("<(/)*ins>", "")
                            .replace("<br>", "").replace("&lt;", "<").replace("&gt;", ">").replace("&amp;", "&")
                            .trim());
                    org_keys.add(seg.getText());
                    if ((z == 0) && (z == segs.size() - 1)) {
                        key_left.add(src_para.attributeValue("lefttrim"));
                        key_right.add(src_para.attributeValue("righttrim"));
                    } else if (z == 0) {
                        key_left.add(src_para.attributeValue("lefttrim"));
                        key_right.add("true");
                    } else if (z == segs.size() - 1) {
                        key_left.add("true");
                        key_right.add(src_para.attributeValue("righttrim"));
                    } else {
                        key_left.add("true");
                        key_right.add("true");
                    }
                }
            }
        }
        SegmenterFactory factory = new SegmenterFactory();
        Configuration segconfig = createConfigForSegmenter(false, this.sourcelanguage);
        Segmenter segmenter = factory.getSegmenter("trados", Locale.makeLocale(this.sourcelanguage), segconfig);
        List<String> finsegs = segmenter.segment(
                group.elementText("text").replaceAll("(?s)<del>.*?</del>", "").replaceAll("<(/)*ins>", "")
                        .replace("<br>", "").replace("&lt;", "<").replace("&gt;", ">").replace("&amp;", "&"));
        ArrayList<ArrayList<Integer>> indices = new ArrayList();
        int key_start_index = 0;
        ArrayList<Integer> indice;
        for (int k = 0; k < finsegs.size(); k++) {
            String finsegtext = (String) finsegs.get(k);

            String combined_key = "";
            indice = new ArrayList();
            for (int x = key_start_index; x < keys.size(); x++) {
                combined_key = combined_key + (String) keys.get(x);

                indice.add(Integer.valueOf(x));
                if (combined_key.replace("", " ").trim().replaceAll("(\\s)+", "")
                        .equals(finsegtext.replace("", " ").trim().replaceAll("(\\s)+", ""))) {
                    indices.add(indice);
                    key_start_index = x + 1;
                    break;
                }
            }
        }
        for (int m = 0; m < indices.size(); m++) {
            ArrayList<Integer> temp_indice = (ArrayList) indices.get(m);
            String temp = "";
            for (int it : temp_indice) {
                temp = temp + (String) org_keys.get(it);
            }
            mergedsegtext.add(temp);
        }
    }
    List<String> rejectedtexts = new ArrayList();
    Workbook wb = new Workbook();
    Worksheet ws = wb.getWorksheets().get(0);
    Cells cells = ws.getCells();
    int count = Math.max(text_source.size(), mergedsegtext.size());
    int t_count = 0;
    for (int i = 0; i < count; i++) {
        String t_src = "";
        String t_fom = "";
        if (i < text_source.size()) {
            org.dom4j.Element src = (org.dom4j.Element) text_source.get(i);
            for (int j = 0; j < src.content().size(); j++) {
                if ((src.content().get(j) instanceof org.dom4j.Text)) {
                    t_src = t_src + ((org.dom4j.Text) src.content().get(j)).getText().replace("&", "&amp;")
                            .replace("<", "&lt;").replace(">", "&gt;");
                } else if ((src.content().get(j) instanceof org.dom4j.Element)) {
                    org.dom4j.Element e = (org.dom4j.Element) src.content().get(j);
                    if ((e.getName().equals("x")) && (e.attribute("ctype").getValue().equals("x-tab"))) {
                        t_src = t_src + " ";
                    } else if ((e.getName().equals("x")) && (e.attribute("ctype").getValue().equals("lb"))) {
                        t_src = t_src + "<br> ";
                    }
                }
            }
        }
        cells.get(i, 0).setHtmlString(t_src);
        if (i < mergedsegtext.size()) {
            t_fom = ((String) mergedsegtext.get(i)).replace("<ins>", "<u>").replace("</ins>", "</u>")
                    .replace("<del>", "<strike>").replace("</del>", "</strike>");
        }
        String accepted_t_fom = t_fom.replaceAll("(?s)<strike>.*?</strike>", "").replace("<u>", "")
                .replace("</u>", "").replace("&amp;paradel;", "").replace("<br>", "");
        String rejected_t_fom = t_fom.replaceAll("(?s)<u>.*?</u>", "").replace("<strike>", "")
                .replace("</strike>", "").replace("&amp;paradel;", "").replace("<br>", "");
        if (extractionSupportImpl.isExtractable(accepted_t_fom)) {
            String input = "<html>" + t_fom.replace("<br> ", "&#8629;<br>").replace("&amp;paradel;",
                    "<strike>&para;</strike><br>") + "</html>";

            cells.get(t_count, 1).setHtmlString(input);
            t_count++;
            if ((i < mergedsegtext.size())
                    && (TrackChangeHelper.getTrackChangeType((String) mergedsegtext.get(i)) == 3)) {
                rejectedtexts.add(rejected_t_fom);
            } else {
                rejectedtexts.add("");
            }
        }
    }
    wb.save(this.prjfolder + File.separator + "verifySegs.xlsx");
    if (numberOfPara_source == t_count) {
        System.out.println("result: TRUE source: " + numberOfPara_source + " formatted: " + t_count);
        isValid = true;

        String timestamp = new SimpleDateFormat("yyyyMMdd'T'HHmmss'Z'").format(new Date());
        for (int r = 0; r < list_source.size(); r++) {
            if (!((String) rejectedtexts.get(r)).equals("")) {
                org.dom4j.Element transunit = (org.dom4j.Element) list_source.get(r);
                org.dom4j.Element originalbase = transunit.addElement("alt-trans");
                org.dom4j.Element source = transunit.element("source");
                org.dom4j.Element target = transunit.element("target");
                source.addAttribute("gs4tr:seginfo",
                        "<root username=\"TC Aligner\" timestamp=\"" + timestamp + "\"/>");
                if (target != null) {
                    transunit.elements().add(transunit.elements().indexOf(target) + 1, originalbase.clone());
                } else {
                    transunit.elements().add(transunit.elements().indexOf(source) + 1, originalbase.clone());
                }
                transunit.remove(originalbase);

                org.dom4j.Element original = transunit.element("alt-trans");
                original.addAttribute("alttranstype", "x-previous-source-version");
                original.addAttribute("gs4tr:seginfo",
                        "<root username=\"Original\" timestamp=\"" + timestamp + "\"/>");
                org.dom4j.Element original_source = original.addElement("source");
                original_source.addText((String) rejectedtexts.get(r));
                original.addElement("target");
            }
        }
        OutputStreamWriter writer = new OutputStreamWriter(
                new BufferedOutputStream(new FileOutputStream(this.sourcetxlf_seg)), "UTF8");
        document_source.write(writer);
        writer.close();
    } else {
        System.out.println("result: false source: " + numberOfPara_source + " formatted: " + t_count);
    }
    return isValid;
}

From source file:revaligner.service.FileAligner.java

public ArrayList<String[]> populateSourceTxlf() throws Exception {
    System.out.println("populating source txlf with aligned segments....");

    ArrayList<String[]> reportStates = new ArrayList();

    ExtractionSupportImpl extractionSupportImpl = new ExtractionSupportImpl(
            Locale.makeLocale(this.sourcelanguage), Locale.makeLocale(this.targetlanguage));
    Configuration config = new BaseConfiguration();
    config.setProperty("extraction.tokens.extract", "all");
    extractionSupportImpl.setConfiguration(config);

    Locale locale = Locale.makeLocale(this.sourcelanguage);
    TradosWordCounter wcounter = new TradosWordCounter(locale, config);

    org.dom4j.Document document_src = XmlParser.parseXmlFile(this.sourcetxlf_nonSeg);
    org.dom4j.Element root_src = document_src.getRootElement();

    org.dom4j.Document document_src_ingt = XmlParser.parseXmlFile(this.sourcetxlf_nonSeg);
    org.dom4j.Element root_src_ingt = document_src_ingt.getRootElement();

    org.dom4j.Document document_src_seg = XmlParser.parseXmlFile(this.sourcetxlf_seg);
    org.dom4j.Element root_src_seg = document_src_seg.getRootElement();

    List<com.aspose.words.Node> list_source = root_src
            .selectNodes("//*[name() = 'group'][@restype = 'x-paragraph']");
    List<com.aspose.words.Node> list_source_ingt = root_src_ingt
            .selectNodes("//*[name() = 'group'][@restype = 'x-paragraph']");
    List<com.aspose.words.Node> list_source_seg = root_src_seg
            .selectNodes("//*[name() = 'group'][@restype = 'x-paragraph']");
    int count = 0;
    int totalWC = 0;

    org.dom4j.Document document = XmlParser.parseXmlFile(this.alignedfile);
    List<org.dom4j.Element> groups = document.getRootElement().element("aligned").elements("group");
    for (int i = 0; i < groups.size(); i++) {
        org.dom4j.Element group = (org.dom4j.Element) groups.get(i);
        List<org.dom4j.Element> units = group.elements("unit");
        if (((org.dom4j.Element) units.get(0)).element("src_para") != null) {
            boolean isParaAllSegmented = true;
            for (int j = 0; j < units.size(); j++) {
                if (((org.dom4j.Element) units.get(j)).attributeValue("alignsegs").equals("false")) {
                    isParaAllSegmented = false;
                    break;
                }//www  .java 2 s .c  o  m
            }
            String srcTextAccepted = group.elementText("text").replaceAll("(?s)<del>.*?</del>", "")
                    .replaceAll("<(/)*ins>", "");
            if (!extractionSupportImpl.isExtractable(srcTextAccepted)) {
                if (isParaAllSegmented) {
                    for (int j = 0; j < units.size(); j++) {
                        org.dom4j.Element unit = (org.dom4j.Element) units.get(j);
                        List<org.dom4j.Element> srcsegs = unit.element("src_para").element("segments")
                                .elements("src_seg");
                        List<org.dom4j.Element> trgsegs = unit.element("trg_para").element("segments")
                                .elements("trg_seg");
                        for (int x = 0; x < srcsegs.size(); x++) {
                            String[] s = new String[7];
                            s[0] = ((org.dom4j.Element) srcsegs.get(x)).getText();
                            if (x >= trgsegs.size()) {
                                s[1] = "";
                            } else {
                                org.dom4j.Element trgseg = (org.dom4j.Element) trgsegs.get(x);
                                String id = trgseg.attributeValue("id");
                                if (id.startsWith("n - ")) {
                                    s[1] = trgseg.getText();
                                } else {
                                    List tmp_contents = new ArrayList();
                                    if (id.contains(" - ")) {
                                        int start = Integer.parseInt(id.split(" - ")[0]);
                                        int end = Integer.parseInt(id.split(" - ")[1]);
                                        tmp_contents.addAll(
                                                (Collection) this.txlftrgsegmap.get(Integer.valueOf(start)));
                                        for (int su = start + 1; su <= end; su++) {
                                            boolean isprevendofpara = ((boolean[]) this.txlftrgsewsmap
                                                    .get(Integer.valueOf(su - 1)))[1];
                                            boolean iscurrentstartofpara = ((boolean[]) this.txlftrgsewsmap
                                                    .get(Integer.valueOf(su)))[0];
                                            if ((isprevendofpara) && (iscurrentstartofpara)) {
                                                List prevseg = (List) this.txlftrgsegmap
                                                        .get(Integer.valueOf(su - 1));
                                                int previdx = -1;
                                                for (int prev = 0; prev < prevseg.size(); prev++) {
                                                    org.dom4j.Node prevnode = (org.dom4j.Node) prevseg
                                                            .get(prev);
                                                    if (prevnode.getNodeType() == 1) {
                                                        org.dom4j.Element prevnode_e = (org.dom4j.Element) prevnode;
                                                        if ((prevnode_e.getName().equals("ws")) && (prevnode_e
                                                                .attributeValue("pos").equals("after"))) {
                                                            previdx = prevseg.size() - prev;
                                                        }
                                                    }
                                                }
                                                if (previdx != -1) {
                                                    tmp_contents.remove(tmp_contents.size() - previdx);
                                                }
                                                List currseg = (List) this.txlftrgsegmap
                                                        .get(Integer.valueOf(su));
                                                int curridx = -1;
                                                for (int curr = 0; curr < currseg.size(); curr++) {
                                                    org.dom4j.Node currnode = (org.dom4j.Node) currseg
                                                            .get(curr);
                                                    if (currnode.getNodeType() == 1) {
                                                        org.dom4j.Element currnode_e = (org.dom4j.Element) currnode;
                                                        if ((currnode_e.getName().equals("ws")) && (currnode_e
                                                                .attributeValue("pos").equals("before"))) {
                                                            curridx = curr;
                                                        }
                                                    }
                                                }
                                                if (curridx != -1) {
                                                    currseg.remove(curridx);
                                                }
                                                if (Locale.makeLocale(this.targetlanguage).isFarEast()) {
                                                    tmp_contents.addAll(currseg);
                                                } else {
                                                    tmp_contents.add(DocumentHelper.createText(" "));
                                                    tmp_contents.addAll(currseg);
                                                }
                                            } else {
                                                tmp_contents.addAll((Collection) this.txlftrgsegmap
                                                        .get(Integer.valueOf(su)));
                                            }
                                        }
                                    } else {
                                        tmp_contents.addAll((Collection) this.txlftrgsegmap
                                                .get(Integer.valueOf(Integer.parseInt(id))));
                                    }
                                    s[1] = trimText(assembleText(tmp_contents).replace("<br> ", "&#8629;<br>"),
                                            false)[0];
                                }
                            }
                            s[2] = "N/A";
                            s[3] = "N/A";
                            s[4] = ((org.dom4j.Element) srcsegs.get(x)).attributeValue("tctype");
                            s[5] = "0";
                            s[6] = "";
                            reportStates.add(s);
                        }
                    }
                } else {
                    String[] s = new String[7];
                    for (int j = 0; j < units.size(); j++) {
                        s[0] = ((org.dom4j.Element) units.get(j)).element("src_para").elementText("text");
                        if (((org.dom4j.Element) units.get(j)).element("trg_para") != null) {
                            s[1] = ((org.dom4j.Element) units.get(j)).element("trg_para").elementText("text");
                        } else {
                            s[1] = "";
                        }
                        s[2] = "N/A";
                        s[3] = "N/A";
                        s[4] = ((org.dom4j.Element) units.get(j)).element("src_para").attributeValue("tctype");
                        s[5] = "0";
                        s[6] = "";
                        reportStates.add(s);
                    }
                }
            } else {
                if (isParaAllSegmented) {
                    org.dom4j.Element txlf_group = (org.dom4j.Element) list_source.get(count);
                    org.dom4j.Element txlf_group_ingt = (org.dom4j.Element) list_source_ingt.get(count);
                    org.dom4j.Element txlf_group_seg = (org.dom4j.Element) list_source_seg.get(count);
                    txlf_group.setContent(txlf_group_seg.content());
                    List transunits = txlf_group.elements("trans-unit");

                    txlf_group_ingt.setContent(txlf_group_seg.content());
                    List transunits_ingt = txlf_group_ingt.elements("trans-unit");

                    ArrayList<String> mergedsegtext = new ArrayList();
                    ArrayList<List> merged_trg_contents = new ArrayList();
                    ArrayList<String> mergedsegtctypes = new ArrayList();

                    ArrayList<String> keys = new ArrayList();
                    ArrayList<String> key_left = new ArrayList();
                    ArrayList<String> key_right = new ArrayList();
                    ArrayList<String> org_keys = new ArrayList();
                    ArrayList<String> trg_keys = new ArrayList();
                    ArrayList<List> trg_contents = new ArrayList();
                    ArrayList<String> src_tctypes = new ArrayList();
                    ArrayList<String> src_review_stats = new ArrayList();
                    ArrayList<String> src_ignore_stats = new ArrayList();
                    ArrayList<Integer> edited_idx = new ArrayList();
                    for (int j = 0; j < units.size(); j++) {
                        org.dom4j.Element unit = (org.dom4j.Element) units.get(j);
                        org.dom4j.Element src_para = unit.element("src_para");
                        org.dom4j.Element trg_para = unit.element("trg_para");
                        List src_segs = src_para.element("segments").elements("src_seg");
                        for (int z = 0; z < src_segs.size(); z++) {
                            org.dom4j.Element src_seg = (org.dom4j.Element) src_segs.get(z);
                            src_tctypes.add(src_seg.attributeValue("tctype"));
                            src_review_stats.add(src_seg.attributeValue("needreview"));
                            src_ignore_stats.add(src_seg.attributeValue("ignored"));
                            keys.add(src_seg.getText().replaceAll("(?s)<del>.*?</del>", "")
                                    .replaceAll("<(/)*ins>", "").replace("<br>", "").trim());
                            org_keys.add(src_seg.getText());
                            if (trg_para != null) {
                                List trg_segs = trg_para.element("segments").elements("trg_seg");
                                if (((org.dom4j.Element) trg_segs.get(z)).attributeValue("edited")
                                        .equals("true")) {
                                    edited_idx.add(Integer.valueOf(trg_contents.size()));
                                }
                                if (trg_segs.size() > z) {
                                    trg_keys.add(((org.dom4j.Element) trg_segs.get(z)).getText());
                                    String id = ((org.dom4j.Element) trg_segs.get(z)).attributeValue("id");
                                    if (id.startsWith("n - ")) {
                                        trg_contents.add(new ArrayList());
                                    } else {
                                        List tmp_contents = new ArrayList();
                                        if (id.contains(" - ")) {
                                            int start = Integer.parseInt(id.split(" - ")[0]);
                                            int end = Integer.parseInt(id.split(" - ")[1]);
                                            tmp_contents.addAll((Collection) this.txlftrgsegmap
                                                    .get(Integer.valueOf(start)));
                                            for (int su = start + 1; su <= end; su++) {
                                                boolean isprevendofpara = ((boolean[]) this.txlftrgsewsmap
                                                        .get(Integer.valueOf(su - 1)))[1];
                                                boolean iscurrentstartofpara = ((boolean[]) this.txlftrgsewsmap
                                                        .get(Integer.valueOf(su)))[0];
                                                if ((isprevendofpara) && (iscurrentstartofpara)) {
                                                    List prevseg = (List) this.txlftrgsegmap
                                                            .get(Integer.valueOf(su - 1));
                                                    int previdx = -1;
                                                    for (int prev = 0; prev < prevseg.size(); prev++) {
                                                        org.dom4j.Node prevnode = (org.dom4j.Node) prevseg
                                                                .get(prev);
                                                        if (prevnode.getNodeType() == 1) {
                                                            org.dom4j.Element prevnode_e = (org.dom4j.Element) prevnode;
                                                            if ((prevnode_e.getName().equals("ws"))
                                                                    && (prevnode_e.attributeValue("pos")
                                                                            .equals("after"))) {
                                                                previdx = prevseg.size() - prev;
                                                            }
                                                        }
                                                    }
                                                    if (previdx != -1) {
                                                        tmp_contents.remove(tmp_contents.size() - previdx);
                                                    }
                                                    List currseg = (List) this.txlftrgsegmap
                                                            .get(Integer.valueOf(su));
                                                    int curridx = -1;
                                                    for (int curr = 0; curr < currseg.size(); curr++) {
                                                        org.dom4j.Node currnode = (org.dom4j.Node) currseg
                                                                .get(curr);
                                                        if (currnode.getNodeType() == 1) {
                                                            org.dom4j.Element currnode_e = (org.dom4j.Element) currnode;
                                                            if ((currnode_e.getName().equals("ws"))
                                                                    && (currnode_e.attributeValue("pos")
                                                                            .equals("before"))) {
                                                                curridx = curr;
                                                            }
                                                        }
                                                    }
                                                    if (curridx != -1) {
                                                        currseg.remove(curridx);
                                                    }
                                                    if (Locale.makeLocale(this.targetlanguage).isFarEast()) {
                                                        tmp_contents.addAll(currseg);
                                                    } else {
                                                        tmp_contents.add(DocumentHelper.createText(" "));
                                                        tmp_contents.addAll(currseg);
                                                    }
                                                } else {
                                                    tmp_contents.addAll((Collection) this.txlftrgsegmap
                                                            .get(Integer.valueOf(su)));
                                                }
                                            }
                                        } else {
                                            tmp_contents.addAll((Collection) this.txlftrgsegmap
                                                    .get(Integer.valueOf(Integer.parseInt(id))));
                                        }
                                        trg_contents.add(tmp_contents);
                                    }
                                } else {
                                    trg_keys.add("");
                                    trg_contents.add(new ArrayList());
                                }
                            } else {
                                trg_keys.add("");
                                trg_contents.add(new ArrayList());
                            }
                            if ((z == 0) && (z == src_segs.size() - 1)) {
                                key_left.add(src_para.attributeValue("lefttrim"));
                                key_right.add(src_para.attributeValue("righttrim"));
                            } else if (z == 0) {
                                key_left.add(src_para.attributeValue("lefttrim"));
                                key_right.add("true");
                            } else if (z == src_segs.size() - 1) {
                                key_left.add("true");
                                key_right.add(src_para.attributeValue("righttrim"));
                            } else {
                                key_left.add("true");
                                key_right.add("true");
                            }
                        }
                    }
                    SegmenterFactory factory = new SegmenterFactory();
                    Configuration segconfig = createConfigForSegmenter(false, this.sourcelanguage);
                    Segmenter segmenter = factory.getSegmenter("trados", Locale.makeLocale(this.sourcelanguage),
                            segconfig);
                    List<String> finsegs = segmenter
                            .segment(group.elementText("text").replaceAll("(?s)<del>.*?</del>", "")
                                    .replaceAll("<(/)*ins>", "").replace("<br>", "").replace("&lt;", "<")
                                    .replace("&gt;", ">").replace("&amp;", "&"));
                    ArrayList<ArrayList<Integer>> indices = new ArrayList();
                    int key_start_index = 0;
                    for (int k = 0; k < finsegs.size(); k++) {
                        String finsegtext = ((String) finsegs.get(k)).replace("&", "&amp;").replace("<", "&lt;")
                                .replace(">", "&gt;");

                        String combined_key = "";
                        ArrayList<Integer> indice = new ArrayList();
                        for (int x = key_start_index; x < keys.size(); x++) {
                            combined_key = combined_key + (String) keys.get(x);

                            indice.add(Integer.valueOf(x));
                            if (combined_key.replace("", " ").trim().replaceAll("(\\s)+", "")
                                    .equals(finsegtext.replace("", " ").trim().replaceAll("(\\s)+", ""))) {
                                indices.add(indice);
                                key_start_index = x + 1;
                                break;
                            }
                        }
                    }
                    ArrayList<Integer> merged_edited_idx = new ArrayList();
                    ArrayList<String[]> statss = new ArrayList();
                    for (int m = 0; m < indices.size(); m++) {
                        boolean iscontentsuseable = true;
                        ArrayList<Integer> temp_indice = (ArrayList) indices.get(m);
                        String temp_src = "";
                        String temp_org_src = "";
                        String temp_trg = "";
                        List temp_trg_content = new ArrayList();
                        int id = 1;
                        int rid = 1;
                        int bxrid = 1;
                        int bptrid = 1;
                        int bxid = 1;
                        int bptid = 1;
                        HashMap<String, String> map_rid = new HashMap();
                        String temp_tctype = (String) src_tctypes
                                .get(((Integer) temp_indice.get(0)).intValue());
                        String temp_review_stats = (String) src_review_stats
                                .get(((Integer) temp_indice.get(0)).intValue());
                        for (Iterator localIterator = temp_indice.iterator(); localIterator.hasNext();) {
                            int it = ((Integer) localIterator.next()).intValue();
                            temp_tctype = temp_tctype.equals(src_tctypes.get(it)) ? temp_tctype : "MIX";
                            temp_review_stats = ((String) src_review_stats.get(it)).equals("true") ? "true"
                                    : temp_review_stats.equals("true") ? "true" : "false";
                            String temp_ignore_stats = (String) src_ignore_stats.get(it);
                            if (edited_idx.contains(Integer.valueOf(it))) {
                                iscontentsuseable = false;
                            }
                            temp_src = temp_src + (String) keys.get(it);
                            temp_org_src = temp_org_src + (String) org_keys.get(it);
                            if (temp_ignore_stats.equals("true")) {
                                temp_trg = temp_trg + "[skipseg]";
                                temp_trg_content.add(DocumentHelper.createText("[skipseg]"));
                            } else {
                                temp_trg = temp_trg + (String) trg_keys.get(it);

                                List trg_content = (List) trg_contents.get(it);
                                for (int nc = 0; nc < trg_content.size(); nc++) {
                                    org.dom4j.Node raw = (org.dom4j.Node) trg_content.get(nc);
                                    if (raw.getNodeType() == 3) {
                                        temp_trg_content.add(raw);
                                    } else if (raw.getNodeType() == 1) {
                                        org.dom4j.Element rawe = (org.dom4j.Element) raw;
                                        if (rawe.getName().equals("source")) {
                                            for (int ncc = 0; ncc < rawe.content().size(); ncc++) {
                                                org.dom4j.Node node = (org.dom4j.Node) rawe.content().get(ncc);
                                                if (node.getNodeType() == 3) {
                                                    temp_trg_content.add(node);
                                                } else if (node.getNodeType() == 1) {
                                                    org.dom4j.Element e = (org.dom4j.Element) node;
                                                    if (!e.getName().equals("x")) {
                                                        if (!e.getName().equals("ph")) {
                                                            if (e.getName().equals("bx")) {
                                                                if ((e.attribute("fake") != null)
                                                                        && (e.attributeValue("fake")
                                                                                .equals("true"))
                                                                        && (temp_indice.indexOf(
                                                                                Integer.valueOf(it)) != 0)) {
                                                                    continue;
                                                                }
                                                                if ((e.attribute("fake") == null)
                                                                        || (!e.attributeValue("fake")
                                                                                .equals("true"))) {
                                                                }
                                                            } else if (e.getName().equals("ex")) {
                                                                if ((e.attribute("fake") != null)
                                                                        && (e.attributeValue("fake")
                                                                                .equals("true"))
                                                                        && (temp_indice.indexOf(Integer.valueOf(
                                                                                it)) != temp_indice.size()
                                                                                        - 1)) {
                                                                    continue;
                                                                }
                                                                if ((e.attribute("fake") == null)
                                                                        || (!e.attributeValue("fake")
                                                                                .equals("true"))) {
                                                                }
                                                            } else if (e.getName().equals("bpt")) {
                                                                if ((e.attribute("fake") != null)
                                                                        && (e.attributeValue("fake")
                                                                                .equals("true"))
                                                                        && (temp_indice.indexOf(
                                                                                Integer.valueOf(it)) != 0)) {
                                                                    continue;
                                                                }
                                                                if ((e.attribute("fake") == null)
                                                                        || (!e.attributeValue("fake")
                                                                                .equals("true"))) {
                                                                }
                                                            } else if (e.getName().equals("ept")) {
                                                                if ((e.attribute("fake") != null)
                                                                        && (e.attributeValue("fake")
                                                                                .equals("true"))
                                                                        && (temp_indice.indexOf(Integer.valueOf(
                                                                                it)) != temp_indice.size()
                                                                                        - 1)) {
                                                                    continue;
                                                                }
                                                                if ((e.attribute("fake") == null)
                                                                        || (!e.attributeValue("fake")
                                                                                .equals("true"))) {
                                                                }
                                                            }
                                                        }
                                                    }
                                                    if (e.attribute("fake") != null) {
                                                        e.remove(e.attribute("fake"));
                                                    }
                                                    temp_trg_content.add(e);
                                                }
                                            }
                                        } else if (rawe.getName().equals("ws")) {
                                            String pos = rawe.attributeValue("pos");
                                            if (pos.equals("before")) {
                                                for (int ncc = 0; ncc < rawe.content().size(); ncc++) {
                                                    org.dom4j.Node node = (org.dom4j.Node) rawe.content()
                                                            .get(ncc);
                                                    if (node.getNodeType() == 3) {
                                                        temp_trg_content.add(0, node);
                                                    } else if (node.getNodeType() == 1) {
                                                        org.dom4j.Element e = (org.dom4j.Element) node;
                                                        if ((!e.getName().equals("x"))
                                                                && (e.getName().equals("it"))) {
                                                            if (e.attributeValue("pos").equals("open")) {
                                                                if ((e.attribute("fake") != null)
                                                                        && (e.attributeValue("fake")
                                                                                .equals("true"))
                                                                        && (temp_indice.indexOf(
                                                                                Integer.valueOf(it)) != 0)) {
                                                                    continue;
                                                                }
                                                                if (e.getText().equals("")) {
                                                                    e.setName("bx");
                                                                } else {
                                                                    e.setName("bpt");
                                                                }
                                                                if ((e.attribute("fake") == null)
                                                                        || (!e.attributeValue("fake")
                                                                                .equals("true"))) {
                                                                }
                                                            } else if (e.attributeValue("pos")
                                                                    .equals("close")) {
                                                                if ((e.attribute("fake") != null)
                                                                        && (e.attributeValue("fake")
                                                                                .equals("true"))
                                                                        && (temp_indice.indexOf(Integer.valueOf(
                                                                                it)) != temp_indice.size()
                                                                                        - 1)) {
                                                                    continue;
                                                                }
                                                                if (e.getText().equals("")) {
                                                                    e.setName("ex");
                                                                } else {
                                                                    e.setName("ept");
                                                                }
                                                                e.remove(e.attribute("ctype"));
                                                                if ((e.attribute("fake") == null)
                                                                        || (!e.attributeValue("fake")
                                                                                .equals("true"))) {
                                                                }
                                                            }
                                                            e.remove(e.attribute("pos"));
                                                        } else {
                                                            if (e.attribute("fake") != null) {
                                                                e.remove(e.attribute("fake"));
                                                            }
                                                            temp_trg_content.add(0, e);
                                                        }
                                                    }
                                                }
                                            } else if (pos.equals("after")) {
                                                for (int ncc = 0; ncc < rawe.content().size(); ncc++) {
                                                    org.dom4j.Node node = (org.dom4j.Node) rawe.content()
                                                            .get(ncc);
                                                    if (node.getNodeType() == 3) {
                                                        temp_trg_content.add(node);
                                                    } else if (node.getNodeType() == 1) {
                                                        org.dom4j.Element e = (org.dom4j.Element) node;
                                                        if ((!e.getName().equals("x"))
                                                                && (e.getName().equals("it"))) {
                                                            if (e.attributeValue("pos").equals("open")) {
                                                                if ((e.attribute("fake") != null)
                                                                        && (e.attributeValue("fake")
                                                                                .equals("true"))
                                                                        && (temp_indice.indexOf(
                                                                                Integer.valueOf(it)) != 0)) {
                                                                    continue;
                                                                }
                                                                if (e.getText().equals("")) {
                                                                    e.setName("bx");
                                                                } else {
                                                                    e.setName("bpt");
                                                                }
                                                                if ((e.attribute("fake") == null)
                                                                        || (!e.attributeValue("fake")
                                                                                .equals("true"))) {
                                                                }
                                                            } else if (e.attributeValue("pos")
                                                                    .equals("close")) {
                                                                if ((e.attribute("fake") != null)
                                                                        && (e.attributeValue("fake")
                                                                                .equals("true"))
                                                                        && (temp_indice.indexOf(Integer.valueOf(
                                                                                it)) != temp_indice.size()
                                                                                        - 1)) {
                                                                    continue;
                                                                }
                                                                if (e.getText().equals("")) {
                                                                    e.setName("ex");
                                                                } else {
                                                                    e.setName("ept");
                                                                }
                                                                e.remove(e.attribute("ctype"));
                                                                if ((e.attribute("fake") == null)
                                                                        || (!e.attributeValue("fake")
                                                                                .equals("true"))) {
                                                                }
                                                            }
                                                            e.remove(e.attribute("pos"));
                                                        } else {
                                                            if (e.attribute("fake") != null) {
                                                                e.remove(e.attribute("fake"));
                                                            }
                                                            temp_trg_content.add(e);
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        String[] stats = TrackChangeHelper.getTxlfTrgStatsFromTCType(temp_tctype, temp_trg);
                        if ((stats[0].equals("1")) && (temp_review_stats.equals("true"))) {
                            stats[2] = "fuzzy-match";
                        }
                        String[] s = new String[7];
                        s[0] = temp_org_src.replace("<br> ", "&#8629;<br>");
                        if (iscontentsuseable) {
                            s[1] = trimText(assembleText(temp_trg_content).replace("<br> ", "&#8629;<br>"),
                                    false)[0];
                        } else {
                            s[1] = temp_trg.replace("<br> ", "&#8629;<br>");
                        }
                        if (s[1].contains("[skipseg]")) {
                            if (s[1].replace("[skipseg]", "").trim().equals("")) {
                                s[1] = "";
                                temp_trg_content = new ArrayList();
                                temp_trg_content.add(DocumentHelper.createText(""));
                                temp_trg = "";
                                stats[0] = "1";
                                stats[1] = "translated";
                                stats[2] = "exact-match";
                            } else {
                                s[1] = s[1].replace("[skipseg]", "");
                                temp_trg_content = replacetextinDomObj(temp_trg_content);
                                temp_trg = temp_trg.replace("[skipseg]", "");
                            }
                        }
                        s[2] = stats[0];
                        s[3] = stats[2];
                        s[4] = temp_tctype;
                        wcounter = new TradosWordCounter(locale, config);
                        wcounter.countText(((org.dom4j.Element) transunits.get(m)).element("source").getText());
                        s[5] = Integer.toString(wcounter.getWordCount());
                        s[6] = "";
                        totalWC += wcounter.getWordCount();
                        reportStates.add(s);
                        if (extractionSupportImpl.isExtractable(temp_src)) {
                            mergedsegtext.add(temp_trg);
                            if (!iscontentsuseable) {
                                merged_edited_idx.add(Integer.valueOf(merged_trg_contents.size()));
                            }
                            merged_trg_contents.add(temp_trg_content);
                            mergedsegtctypes.add(temp_tctype);
                            statss.add(stats);
                        }
                    }
                    for (int t = 0; t < transunits.size(); t++) {
                        org.dom4j.Element trans_unit = (org.dom4j.Element) transunits.get(t);
                        org.dom4j.Element trans_unit_ignt = (org.dom4j.Element) transunits_ingt.get(t);
                        trans_unit.addAttribute("gs4tr:editStatus", "leveraged");
                        org.dom4j.Element source = trans_unit.element("source");

                        org.dom4j.Element target = trans_unit.addElement("target");
                        trans_unit.elements().add(source.indexOf(source.getParent()) + 2, target.clone());
                        trans_unit.remove(target);
                        target = trans_unit.element("target");

                        org.dom4j.Element target_ignt = trans_unit_ignt.addElement("target");
                        trans_unit_ignt.elements().add(source.indexOf(source.getParent()) + 2,
                                target_ignt.clone());
                        trans_unit_ignt.remove(target_ignt);
                        target_ignt = trans_unit_ignt.element("target");
                        if (merged_edited_idx.contains(Integer.valueOf(t))) {
                            target.setText(((String) mergedsegtext.get(t)).replace("&lt;", "<")
                                    .replace("&gt;", ">").replace("&amp;", "&").trim());
                            target_ignt.setText(((String) mergedsegtext.get(t)).replace("&lt;", "<")
                                    .replace("&gt;", ">").replace("&amp;", "&").trim());
                        } else {
                            target.setContent(trimContents((List) merged_trg_contents.get(t)));
                            target_ignt.setContent(trimContents((List) merged_trg_contents.get(t)));
                            if (!((String[]) statss.get(t))[0].equals("75")) {
                                org.dom4j.Element source_ingt = trans_unit_ignt.element("source");
                                source_ingt.setContent(trimContents((List) merged_trg_contents.get(t)));
                            }
                        }
                        String[] stats = (String[]) statss.get(t);
                        if (stats[0].equals("1")) {
                            trans_unit.addAttribute("gs4tr:locked", "true");
                        }
                        target.addAttribute("gs4tr:score", stats[0]);
                        target.addAttribute("state", stats[1]);
                        target.addAttribute("state-qualifier", stats[2]);
                        if (stats[0].equals("0")) {
                            trans_unit.remove(target);
                        }
                    }
                } else {
                    String trgtext = "";
                    if (((org.dom4j.Element) units.get(0)).element("trg_para") != null) {
                        trgtext = ((org.dom4j.Element) units.get(0)).element("trg_para").elementText("text");
                    }
                    String temp_tctype = ((org.dom4j.Element) units.get(0)).element("src_para")
                            .attributeValue("tctype");
                    for (int j = 1; j < units.size(); j++) {
                        org.dom4j.Element prev_unit = (org.dom4j.Element) units.get(j - 1);
                        org.dom4j.Element unit = (org.dom4j.Element) units.get(j);
                        String src_tctype = unit.element("src_para").attributeValue("tctype");
                        temp_tctype = temp_tctype.equals(src_tctype) ? temp_tctype : "MIX";
                        if (unit.element("trg_para") != null) {
                            String Rtrim = prev_unit.element("src_para").attributeValue("righttrim");
                            String Ltrim = unit.element("src_para").attributeValue("lefttrim");
                            if ((Rtrim.equals("true")) || (Ltrim.equals("true"))) {
                                trgtext = trgtext + " " + unit.element("trg_para").elementText("text");
                            } else {
                                trgtext = trgtext + unit.element("trg_para").elementText("text");
                            }
                        }
                    }
                    org.dom4j.Element txlf_group = (org.dom4j.Element) list_source.get(count);
                    org.dom4j.Element trans_unit = txlf_group.element("trans-unit");
                    trans_unit.addAttribute("gs4tr:editStatus", "leveraged");
                    org.dom4j.Element source = trans_unit.element("source");

                    org.dom4j.Element target = trans_unit.addElement("target");
                    trans_unit.elements().add(source.indexOf(source.getParent()) + 2, target.clone());
                    trans_unit.remove(target);
                    target = trans_unit.element("target");

                    int lb_cnt = 0;
                    String surfix = trgtext;
                    while (surfix.indexOf("<br> ") != -1) {
                        lb_cnt++;
                        int pos = surfix.indexOf("<br> ");
                        String prefix = surfix.substring(0, pos);
                        target.addText(prefix.replace("&lt;", "<").replace("&gt;", ">").replace("&amp;", "&"));
                        org.dom4j.Element x = target.addElement("x");
                        x.addAttribute("ctype", "lb");
                        x.addAttribute("id", Integer.toString(lb_cnt));
                        x.addAttribute("equiv-text", " ");
                        surfix = surfix.substring(pos + 5, surfix.length());
                    }
                    target.addText(surfix.replace("&lt;", "<").replace("&gt;", ">").replace("&amp;", "&"));
                    String[] stats = TrackChangeHelper.getTxlfTrgStatsFromTCType(temp_tctype, trgtext);
                    target.addAttribute("gs4tr:score", stats[0]);
                    target.addAttribute("state", stats[1]);
                    target.addAttribute("state-qualifier", stats[2]);

                    String[] s = new String[7];
                    s[0] = group.elementText("text").replace("<br> ", "&#8629;<br>");
                    s[1] = trgtext.replace("<br> ", "&#8629;<br>");
                    s[2] = stats[0];
                    s[3] = stats[2];
                    s[4] = temp_tctype;
                    wcounter = new TradosWordCounter(locale, config);
                    wcounter.countText(source.getText());
                    s[5] = Integer.toString(wcounter.getWordCount());
                    s[6] = "";
                    totalWC += wcounter.getWordCount();
                    reportStates.add(s);
                }
                count++;
            }
        }
    }
    root_src.element("file").addAttribute("gs4tr:wordcount", Integer.toString(totalWC));
    fixTxlfTrgTags(document_src);

    this.populatedsourcetxlf = (this.sourcefile + ".txlf");
    if (new File(this.populatedsourcetxlf).exists()) {
        new File(this.populatedsourcetxlf).delete();
    }
    OutputStreamWriter writer = new OutputStreamWriter(
            new BufferedOutputStream(new FileOutputStream(this.populatedsourcetxlf)), "UTF8");
    document_src.write(writer);
    writer.close();

    removeBlankLinesAndNameSpace(this.populatedsourcetxlf);

    root_src_ingt.element("file").addAttribute("gs4tr:wordcount", Integer.toString(totalWC));
    fixTxlfTrgTags(document_src_ingt);

    String ingtfile = this.sourcefile + ".ingt.txlf";
    if (new File(ingtfile).exists()) {
        new File(ingtfile).delete();
    }
    OutputStreamWriter writer_ingt = new OutputStreamWriter(
            new BufferedOutputStream(new FileOutputStream(ingtfile)), "UTF8");
    document_src_ingt.write(writer_ingt);
    writer_ingt.close();

    removeBlankLinesAndNameSpace(ingtfile);

    return reportStates;
}

From source file:routines.system.GetJarsToRegister.java

License:Open Source License

private boolean setJarsToRegister() throws IOException, org.dom4j.DocumentException {
    String jobXmlPath = new java.io.File("../../job.xml").getCanonicalPath();
    boolean isOozieExecution = isNeedAddLibsPath(jobXmlPath);
    if (!isOozieExecution) {
        jobXmlPath = new java.io.File("./job.xml").getCanonicalPath();
        isOozieExecution = isNeedAddLibsPath(jobXmlPath);
    }//from   w w w. j  av  a 2 s . com
    if (isOozieExecution) {

        SAXReader reader_oozie = new org.dom4j.io.SAXReader();
        org.dom4j.Document document_oozie = reader_oozie.read(jobXmlPath);
        List<Node> list_oozie = document_oozie.selectNodes("/configuration/property");
        for (Node node : list_oozie) {
            Element element_oozie = (org.dom4j.Element) node;
            String name_oozie = element_oozie.elementText("name");
            if (name_oozie.equals("mapred.cache.localFiles")
                    || name_oozie.equals("mapreduce.job.cache.local.files")) {
                this.oozieClasspathLine = element_oozie.elementText("value");
                return true;
            }
        }
    }
    return false;
}