Ejemplo n.º 1
0
    /**
     * 分析xliff文件的每一个 trans-unit 节点
     *
     * @throws Exception
     */
    private void ananysisXlfTU(IProgressMonitor monitor) throws Exception {
      if (monitor == null) {
        monitor = new NullProgressMonitor();
      }

      AutoPilot ap = new AutoPilot(xlfVN);
      AutoPilot childAP = new AutoPilot(xlfVN);
      VTDUtils vu = new VTDUtils(xlfVN);

      String xpath = "count(/xliff/file/body//trans-unit)";
      ap.selectXPath(xpath);
      int totalTuNum = (int) ap.evalXPathToNumber();
      if (totalTuNum > 500) {
        workInterval = totalTuNum / 500;
      }
      int matchWorkUnit =
          totalTuNum % workInterval == 0
              ? (totalTuNum / workInterval)
              : (totalTuNum / workInterval) + 1;
      monitor.beginTask("", matchWorkUnit);

      xpath = "/xliff/file/body//trans-unit";
      String srcXpath = "./source";
      String tgtXpath = "./target";
      ap.selectXPath(xpath);
      int attrIdx = -1;
      // trans-unit的id,对应sdl文件的占位符如%%%1%%% 。
      String segId = "";
      TuMrkBean srcBean = null;
      TuMrkBean tgtBean = null;
      int traversalTuIndex = 0;
      while (ap.evalXPath() != -1) {
        traversalTuIndex++;
        if ((attrIdx = xlfVN.getAttrVal("id")) == -1) {
          continue;
        }
        srcBean = new TuMrkBean();
        tgtBean = new TuMrkBean();
        segId = xlfVN.toString(attrIdx);

        // 处理source节点
        xlfVN.push();
        childAP.selectXPath(srcXpath);
        if (childAP.evalXPath() != -1) {
          String srcContent = vu.getElementContent();
          srcContent = srcContent == null ? "" : srcContent;
          srcBean.setContent(srcContent);
          srcBean.setSource(true);
        }
        xlfVN.pop();

        // 处理target节点
        String status = ""; // 状态,针对target节点,空字符串为未翻译
        xlfVN.push();
        tgtBean.setSource(false);
        String tgtContent = null;
        childAP.selectXPath(tgtXpath);
        if (childAP.evalXPath() != -1) {
          tgtContent = vu.getElementContent();

          if ((attrIdx = xlfVN.getAttrVal("state")) != -1) {
            status = xlfVN.toString(attrIdx);
          }
        }
        tgtContent = tgtContent == null ? "" : tgtContent;
        tgtBean.setContent(tgtContent);
        xlfVN.pop();

        // 处理批注
        getNotes(xlfVN, tgtBean);

        // 判断是否处于锁定状态
        if ((attrIdx = xlfVN.getAttrVal("translate")) != -1) {
          if ("no".equalsIgnoreCase(xlfVN.toString(attrIdx))) {
            tgtBean.setLocked(true);
          }
        }
        // 判断是否处于批准状态,若是签发,就没有必要判断了,因为签发了的一定就批准了的
        if (!"signed-off".equalsIgnoreCase(status)) {
          if ((attrIdx = xlfVN.getAttrVal("approved")) != -1) {
            if ("yes".equalsIgnoreCase(xlfVN.toString(attrIdx))) {
              status = "approved"; // 批准
            }
          }
        }
        tgtBean.setStatus(status);
        replaceSegment(segId, srcBean, tgtBean);

        monitorWork(monitor, traversalTuIndex, false);
      }
      monitorWork(monitor, traversalTuIndex, true);
    }