Java Code Examples for com.ximpleware.VTDGen#setDoc()

The following examples show how to use com.ximpleware.VTDGen#setDoc() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Cell.java    From translationstudio8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Get rPh & phoneticPr Element fragment
 * @return ;
 */
private String getNonTextContent(){
	StringBuffer result = new StringBuffer();
	VTDGen vg = new VTDGen();
	vg.setDoc(content.getBytes());
	VTDUtils vu = null;
	try {
		vg.parse(true);
		vu = new VTDUtils(vg.getNav());

		AutoPilot ap = new AutoPilot(vu.getVTDNav());
		ap.selectXPath("/si/rPh | phoneticPr");
		while(ap.evalXPath() != -1){
			result.append(vu.getElementFragment());
		}			
	} catch (VTDException e) {
		e.printStackTrace();
	}
	return result.toString();
}
 
Example 2
Source File: Xliff2Ttx.java    From translationstudio8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 根据一个<g>标记的头部获取标记的 id 属性值
 * @param tagStr
 * @return
 */
private String getTagId(String tagStr) throws Exception {
	String tagId = "";
	VTDGen vg = new VTDGen();
	vg.setDoc(tagStr.getBytes());
	vg.parse(false);
	VTDNav vn = vg.getNav();
	tagAP = new AutoPilot(vn);
	tagAP.selectXPath("/g");
	int index = -1;
	if (tagAP.evalXPath() != -1) {
		if ((index = vn.getAttrVal("id")) != -1) {
			tagId = vn.toRawString(index);
		}
	}
	return tagId;
}
 
Example 3
Source File: Xliff2Ttx.java    From tmxeditor8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 根据一个<g>标记的头部获取标记的 id 属性值
 * @param tagStr
 * @return
 */
private String getTagId(String tagStr) throws Exception {
	String tagId = "";
	VTDGen vg = new VTDGen();
	vg.setDoc(tagStr.getBytes());
	vg.parse(false);
	VTDNav vn = vg.getNav();
	tagAP = new AutoPilot(vn);
	tagAP.selectXPath("/g");
	int index = -1;
	if (tagAP.evalXPath() != -1) {
		if ((index = vn.getAttrVal("id")) != -1) {
			tagId = vn.toRawString(index);
		}
	}
	return tagId;
}
 
Example 4
Source File: Cell.java    From tmxeditor8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Get rPh & phoneticPr Element fragment
 * @return ;
 */
private String getNonTextContent(){
	StringBuffer result = new StringBuffer();
	VTDGen vg = new VTDGen();
	vg.setDoc(content.getBytes());
	VTDUtils vu = null;
	try {
		vg.parse(true);
		vu = new VTDUtils(vg.getNav());

		AutoPilot ap = new AutoPilot(vu.getVTDNav());
		ap.selectXPath("/si/rPh | phoneticPr");
		while(ap.evalXPath() != -1){
			result.append(vu.getElementFragment());
		}			
	} catch (VTDException e) {
		e.printStackTrace();
	}
	return result.toString();
}
 
Example 5
Source File: TextUtil.java    From translationstudio8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * ,针对插件开发模块 robert 2012-03-15
 * @param langXMlLC
 * @param _languages
 * @throws Exception
 */
private static void getLanguages(String langXMlLC, Hashtable<String, String> _languages) throws Exception {
	VTDGen vg = new VTDGen();
	vg.setDoc(readBytesFromIS(CoreActivator.getConfigurationFileInputStream(langXMlLC)));
	vg.parse(true);

	VTDNav vn = vg.getNav();
	AutoPilot ap = new AutoPilot(vn);
	ap.selectXPath("/languages/lang");
	while (ap.evalXPath() != -1) {
		if (vn.getAttrVal("code") != -1 && vn.getText() != -1) {
			_languages.put(vn.toString(vn.getAttrVal("code")).toLowerCase(), vn.toString(vn.getText()).trim());
		}
	}
}
 
Example 6
Source File: Cell.java    From translationstudio8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 加载Cell的文本内容
 * @param content
 * @return ;
 */
private String loadCellText(String content) {
	String result = "";
	VTDGen vg = new VTDGen();
	vg.setDoc(content.getBytes());
	VTDUtils vu = null;
	try {
		vg.parse(true);
		vu = new VTDUtils(vg.getNav());

		AutoPilot ap = new AutoPilot(vu.getVTDNav());
		ap.selectXPath("/si/r");
		if (ap.evalXPath() != -1) {
			StringBuffer bf = new StringBuffer();
			do {
				String tVal = vu.getChildContent("t");
				bf.append(tVal);
			} while (ap.evalXPath() != -1);
			result = bf.toString();
		} else {
			vu.pilot("/si/t");
			result = vu.getElementContent();
		}
	} catch (VTDException e) {
		e.printStackTrace();
	}
	return result;
}
 
Example 7
Source File: AbstractDrawing.java    From translationstudio8 with GNU General Public License v2.0 5 votes vote down vote up
private void loadXML() {
	VTDGen vg = new VTDGen();
	vg.setDoc(this.xmlContent.getBytes());
	try {
		vg.parse(true);
		vu = new VTDUtils(vg.getNav());
		xm = new XMLModifier(vu.getVTDNav());
	} catch (VTDException e) {
		logger.error("",e);
	}
}
 
Example 8
Source File: TextUtil.java    From tmxeditor8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * ,针对插件开发模块 robert 2012-03-15
 * @param langXMlLC
 * @param _languages
 * @throws Exception
 */
private static void getLanguages(String langXMlLC, Hashtable<String, String> _languages) throws Exception {
	VTDGen vg = new VTDGen();
	vg.setDoc(readBytesFromIS(CoreActivator.getConfigurationFileInputStream(langXMlLC)));
	vg.parse(true);

	VTDNav vn = vg.getNav();
	AutoPilot ap = new AutoPilot(vn);
	ap.selectXPath("/languages/lang");
	while (ap.evalXPath() != -1) {
		if (vn.getAttrVal("code") != -1 && vn.getText() != -1) {
			_languages.put(vn.toString(vn.getAttrVal("code")).toLowerCase(), vn.toString(vn.getText()).trim());
		}
	}
}
 
Example 9
Source File: Cell.java    From tmxeditor8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 加载Cell的文本内容
 * @param content
 * @return ;
 */
private String loadCellText(String content) {
	String result = "";
	VTDGen vg = new VTDGen();
	vg.setDoc(content.getBytes());
	VTDUtils vu = null;
	try {
		vg.parse(true);
		vu = new VTDUtils(vg.getNav());

		AutoPilot ap = new AutoPilot(vu.getVTDNav());
		ap.selectXPath("/si/r");
		if (ap.evalXPath() != -1) {
			StringBuffer bf = new StringBuffer();
			do {
				String tVal = vu.getChildContent("t");
				bf.append(tVal);
			} while (ap.evalXPath() != -1);
			result = bf.toString();
		} else {
			vu.pilot("/si/t");
			result = vu.getElementContent();
		}
	} catch (VTDException e) {
		e.printStackTrace();
	}
	return result;
}
 
Example 10
Source File: AbstractDrawing.java    From tmxeditor8 with GNU General Public License v2.0 5 votes vote down vote up
private void loadXML() {
	VTDGen vg = new VTDGen();
	vg.setDoc(this.xmlContent.getBytes());
	try {
		vg.parse(true);
		vu = new VTDUtils(vg.getNav());
		xm = new XMLModifier(vu.getVTDNav());
	} catch (VTDException e) {
		logger.error("",e);
	}
}
 
Example 11
Source File: Xliff2Ttx.java    From tmxeditor8 with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) {
//		String content = "<g id='2'>this is a test</g>asdf as sad ";
//		content = content.substring(0, content.lastIndexOf("</g>")) + content.substring(content.lastIndexOf("</g>") + 4);
//		System.out.println(content);
		
//		String content = "<g id='2'>this is a test</g>asdf as sad</g> ";
//		int index = content.indexOf("</g>");
//		content = "<cf" + content.substring(content.indexOf("<g") + 2, content.indexOf(">")) + ">";
//		System.out.println(content);
		
		int index = -1;
//		String content = "<g id='2' size=\"11\" complexscriptssize=\"11\" complexscriptsbold=\"on\" bold=\"on\" superscript=\"on\"><ph>&lt;symbol font=&quot;Symbol&quot; character=&quot;F0E2&quot;/&gt;</ph></g>";
//		String content = "<g id='2' size=\"11\" complexscriptssize=\"11\" complexscriptsbold=\"on\" bold=\"on\" superscript=\"on\"><ph>&lt;field/&gt;</ph></g>";
//		String content = "<g id='2' size=\"11\" complexscriptssize=\"11\" complexscriptsbold=\"on\" bold=\"on\" superscript=\"on\"><ph>&lt;/cf&gt;</ph></g><ph>&lt;field/&gt;</ph>";
		String content = "<g id='2' size=\"11\" complexscriptssize=\"11\" complexscriptsbold=\"on\" bold=\"on\" superscript=\"on\"><ph>&lt;/cf&gt;</ph></g>这后面是个cf标记了哦。<ph type='cf'>&lt;/cf&gt;</ph>";
		index = content.indexOf("<ph");
		while(index != -1){
			String phFrag = content.substring(index, content.indexOf("</ph>", index) + 5);
			
			System.out.println(phFrag);
			try {
				VTDGen vg = new VTDGen();
				vg.setDoc(phFrag.getBytes());
				vg.parse(true);
				VTDNav vn = vg.getNav();
				AutoPilot ap = new AutoPilot(vn);
				VTDUtils vu = new VTDUtils(vn);
				ap.selectXPath("/ph");
				String replaceText = "";
				String phContent = "";
				int attrIdx = -1;
				if (ap.evalXPath() != -1) {
					phContent = vu.getElementContent();
//					System.out.println("phContent = " + phContent);
					// 有type属性的,一般是cf标记
					if ((attrIdx = vn.getAttrVal("type")) != -1) {
						if ("cf".equals(vn.toString(attrIdx))) {
							String utType = "";
							// 这个cf是开始还是结束&lt;cf size=&quot;11&quot; complexscriptssize=&quot;11&quot;&gt;
							if (phContent.indexOf("&lt;cf") != -1) {
								utType = "start";
							}else {
								utType = "end";
							}
							replaceText = "<ut Type=\"" + utType + "\" RightEdge=\"angle\" DisplayText=\"cf\">" + phContent + "</ut>";
						}
					}else {
						//没有type的,是其他标记,如&lt;symbol font=&quot;Symbol&quot; character=&quot;F0E2&quot;/&gt
						String tagName = "";
						int startIdx = -1;
						int endIdx = -1;
						// 针对起始标记如&lt;symbol font=&quot;Symbol&quot; character=&quot;F0E2&quot;/&gt
						if ((startIdx = phContent.trim().indexOf("&lt;")) != -1) {
							//针对结束标记如&lt;/null?&gt;
							if ("/".equals(phContent.trim().substring(startIdx + 4, startIdx + 5))) {
								tagName = phContent.trim().substring(startIdx + 5, phContent.trim().indexOf("&gt;"));
							}else {
								if ((endIdx = phContent.trim().indexOf(" ")) != -1) {
									tagName = phContent.trim().substring(startIdx + 4, endIdx);
								}else {
									//针对没有空格的如<ph>&lt;field/&gt;</ph>
									tagName = phContent.trim().substring(startIdx + 4, phContent.trim().indexOf("/"));
								}	
							}
//							System.out.println("tagName = '" + tagName + "'");
						}
//						System.out.println( "tagName = " + tagName);
						replaceText = "<ut DisplayText=\"" + tagName + "\">" + phContent + "</ut>";
						
					}
					content = content.replace(phFrag, replaceText);
//					System.out.println(content);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			index = content.indexOf("<ph", index + 1);
		}
//		System.out.println(content);
		
		
		String tagStr = "<g id='1' size='12'/>";
		String newTagStr = "<cf" + tagStr.substring(tagStr.indexOf("<g") + 2, tagStr.indexOf("/>")) + ">";
		newTagStr = "<ut Type=\"start\" RightEdge=\"angle\" DisplayText=\"cf\">" + cleanString(newTagStr) + "</ut>";
//		System.out.println("newTagStr = " + newTagStr);
		
	}
 
Example 12
Source File: TextUtil.java    From translationstudio8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 加载国家名称,针对插件开发模块 robert 2012-03-15
 * @param langXMlLC
 * @param _languages
 * @throws Exception
 */
public static Hashtable<String, String> plugin_loadCoutries() throws Exception {
	// 国家文件位置
	String countryXmlLC = CoreActivator.ISO3166_1_PAHT;
	Hashtable<String, String> _countries = new Hashtable<String, String>();

	VTDGen vg = new VTDGen();
	vg.setDoc(readBytesFromIS(CoreActivator.getConfigurationFileInputStream(countryXmlLC)));
	vg.parse(true);

	VTDNav vn = vg.getNav();

	AutoPilot ap = new AutoPilot(vn);
	AutoPilot childAP = new AutoPilot(vn);
	ap.selectXPath("/ISO_3166-1_List_en/ISO_3166-1_Entry");
	while (ap.evalXPath() != -1) {
		String code = "";
		String name = "";

		int index;
		vn.push();
		childAP.selectXPath("./ISO_3166-1_Alpha-2_Code_element");
		if (childAP.evalXPath() != -1) {
			if ((index = vn.getText()) != -1) {
				code = vn.toString(index).trim().toUpperCase();
			}
		}
		vn.pop();

		vn.push();
		childAP.selectXPath("./ISO_3166-1_Country_name");
		if (childAP.evalXPath() != -1) {
			if ((index = vn.getText()) != -1) {
				name = vn.toString(index).trim();
			}
		}
		vn.pop();

		if (!"".equals(code) && !"".equals(name)) {
			_countries.put(code, name);
		}
	}
	return _countries;
}
 
Example 13
Source File: Xliff2Ttx.java    From tmxeditor8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 处理 ph 节点
 * @return
 * @throws Exception
 */
private String replacePH(String phFrag) throws Exception{
	String replaceText = "";
	VTDGen vg = new VTDGen();
	vg.setDoc(phFrag.getBytes());
	vg.parse(true);
	VTDNav vn = vg.getNav();
	AutoPilot ap = new AutoPilot(vn);
	VTDUtils vu = new VTDUtils(vn);
	ap.selectXPath("/ph");
	String phContent = "";
	int attrIdx = -1;
	String utType = "";
	String edgeStr = "";
	if (ap.evalXPath() != -1) {
		phContent = vu.getElementContent();
		// 有type属性的,一般是cf标记
		if ((attrIdx = vn.getAttrVal("type")) != -1) {
			if ("cf".equals(vn.toString(attrIdx))) {
				// 这个cf是开始还是结束&lt;cf size=&quot;11&quot; complexscriptssize=&quot;11&quot;&gt;
				if (phContent.indexOf("&lt;cf") != -1) {
					utType = "start";
					edgeStr = "RightEdge=\"angle\"";
				}else {
					utType = "end";
					edgeStr = "LeftEdge=\"angle\"";
				}
				replaceText += "<ut Type=\"" + utType + "\" "+ edgeStr +" DisplayText=\"cf\">" + cleanString(phContent) + "</ut>";
			}
		}else {
			//没有type的,是其他标记,如&lt;symbol font=&quot;Symbol&quot; character=&quot;F0E2&quot;/&gt
			String tagName = "";
			int startIdx = -1;
			int endIdx = -1;
			// 针对起始标记如&lt;symbol font=&quot;Symbol&quot; character=&quot;F0E2&quot;/&gt
			if ((startIdx = phContent.trim().indexOf("&lt;")) != -1) {
				//针对结束标记如&lt;/null?&gt;
				if ("/".equals(phContent.trim().substring(startIdx + 4, startIdx + 5))) {
					tagName = phContent.trim().substring(startIdx + 5, phContent.trim().indexOf("&gt;"));
					utType = "end";
					edgeStr = " LeftEdge=\"angle\"";
				}else {
					if ((endIdx = phContent.trim().indexOf(" ")) != -1) {
						tagName = phContent.trim().substring(startIdx + 4, endIdx);
						if (phContent.indexOf("/&gt") != -1) {
							utType = "";
						}else {
							utType = "start";
							edgeStr = " RightEdge=\"angle\"";
						}
					}else if((endIdx = phContent.trim().indexOf("/")) != -1) {
						//针对没有空格的如<ph>&lt;field/&gt;</ph>
						tagName = phContent.trim().substring(startIdx + 4, phContent.trim().indexOf("/"));
						utType = "";
					}else{
						//针对 &lt;field&gt;
						tagName = phContent.trim().substring(startIdx + 4, phContent.trim().indexOf("&gt"));
						utType = "start";
						edgeStr = " RightEdge=\"angle\"";
					}	
				}
			}
			String utTypeAttrStr = utType.length() > 0 ? " Type=\""+utType+"\"" : "";
			replaceText = "<ut"+ utTypeAttrStr + edgeStr +" DisplayText=\"" + tagName + "\">" + cleanString(phContent) + "</ut>";
		}
	}
	return replaceText;
}
 
Example 14
Source File: IDML2XLIFF.java    From tmxeditor8 with GNU General Public License v2.0 4 votes vote down vote up
public String handlePara(String para, HashMap<String, String> mapDelete) throws Exception {
	StringBuffer sb = new StringBuffer();
	List<Integer> lstDepth = new ArrayList<Integer>();
	VTDGen vg = new VTDGen();
	vg.setDoc(para.getBytes());
	vg.parse(true);
	VTDNav vn = vg.getNav();
	XMLModifier xm = new XMLModifier(vn);
	AutoPilot ap = new AutoPilot(vn);
	VTDUtils vu = new VTDUtils(vn);
	boolean isFind = false;
	int curDepth = 0;
	ArrayList<String> lstSegment = new ArrayList<String>();
	ArrayList<Integer> lstIndex = new ArrayList<Integer>();
	ap.selectXPath("/ParagraphStyleRange/descendant::node()");
	StringBuffer sbSkl = new StringBuffer();
	StringBuffer sbXLF = new StringBuffer();
	StringBuffer sbContent = new StringBuffer();
	AutoPilot ap2 = new AutoPilot(vn);
	AutoPilot childAp = new AutoPilot(vn);
	while (ap.evalXPath() != -1) {
		String nodeName = vu.getCurrentElementName();
		curDepth = vn.getCurrentDepth();
		// 跳过 ParagraphStyleRange 节点
		if (nodeName.equalsIgnoreCase("ParagraphStyleRange")) {
			lstDepth.add(curDepth);
			// vn.pop();
			// 一个文本段结束,要进行分段
			if (sb.length() > 0) {
				vn.push();
				updateParaAndXLF(sb, vn, lstSegment, sbSkl, sbContent, sbXLF, xm, ap2, childAp, lstIndex);
				vn.pop();
			}
			continue;
		} else {
			isFind = false;
			Iterator<Integer> it = lstDepth.iterator();
			while (it.hasNext()) {
				Integer depth = (Integer) it.next();
				if (curDepth >= depth) {
					isFind = true;
					it.remove();
				}
			}
			if (isFind || nodeName.equalsIgnoreCase("Br") || nodeName.equalsIgnoreCase("Table")) {
				// 一个文本段结束,要进行分段
				if (sb.length() > 0) {
					vn.push();
					updateParaAndXLF(sb, vn, lstSegment, sbSkl, sbContent, sbXLF, xm, ap2, childAp, lstIndex);
					vn.pop();
				}
			}
			if (nodeName.equalsIgnoreCase("Content")) {
				String seg = vu.getElementContent();
				sb.append(seg);
				lstSegment.add(seg);
				lstIndex.add(vn.getCurrentIndex());
			}
		}
	}
	if (sb.length() > 0) {
		vn.push();
		updateParaAndXLF(sb, vn, lstSegment, sbSkl, sbContent, sbXLF, xm, ap2, childAp, lstIndex);
		vn.pop();
	}
	
	if (mapDelete.size() > 0) {
		childAp.bind(vn);
		ap.selectXPath("/ParagraphStyleRange/descendant::node()[text()!='' and starts-with(normalize-space(text()),'%%%%') and ends-with(normalize-space(text()),'%%%%')]");
		while (ap.evalXPath() != -1) {
			String strId = vu.getElementPureText().trim();
			String strPrimary = mapDelete.get(strId);
			vn.push();
			childAp.selectXPath("./text()");
			if (childAp.evalXPath() != -1) {
				xm.updateToken(vn.getCurrentIndex(), strPrimary);
			}
			vn.pop();
		}
	}

	vn = xm.outputAndReparse();
	vu.bind(vn);
	return vu.getElementFragment();
}
 
Example 15
Source File: TextUtil.java    From tmxeditor8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 加载国家名称,针对插件开发模块 robert 2012-03-15
 * @param langXMlLC
 * @param _languages
 * @throws Exception
 */
public static Hashtable<String, String> plugin_loadCoutries() throws Exception {
	// 国家文件位置
	String countryXmlLC = CoreActivator.ISO3166_1_PAHT;
	Hashtable<String, String> _countries = new Hashtable<String, String>();

	VTDGen vg = new VTDGen();
	vg.setDoc(readBytesFromIS(CoreActivator.getConfigurationFileInputStream(countryXmlLC)));
	vg.parse(true);

	VTDNav vn = vg.getNav();

	AutoPilot ap = new AutoPilot(vn);
	AutoPilot childAP = new AutoPilot(vn);
	ap.selectXPath("/ISO_3166-1_List_en/ISO_3166-1_Entry");
	while (ap.evalXPath() != -1) {
		String code = "";
		String name = "";

		int index;
		vn.push();
		childAP.selectXPath("./ISO_3166-1_Alpha-2_Code_element");
		if (childAP.evalXPath() != -1) {
			if ((index = vn.getText()) != -1) {
				code = vn.toString(index).trim().toUpperCase();
			}
		}
		vn.pop();

		vn.push();
		childAP.selectXPath("./ISO_3166-1_Country_name");
		if (childAP.evalXPath() != -1) {
			if ((index = vn.getText()) != -1) {
				name = vn.toString(index).trim();
			}
		}
		vn.pop();

		if (!"".equals(code) && !"".equals(name)) {
			_countries.put(code, name);
		}
	}
	return _countries;
}
 
Example 16
Source File: Xliff2Ttx.java    From translationstudio8 with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) {
//		String content = "<g id='2'>this is a test</g>asdf as sad ";
//		content = content.substring(0, content.lastIndexOf("</g>")) + content.substring(content.lastIndexOf("</g>") + 4);
//		System.out.println(content);
		
//		String content = "<g id='2'>this is a test</g>asdf as sad</g> ";
//		int index = content.indexOf("</g>");
//		content = "<cf" + content.substring(content.indexOf("<g") + 2, content.indexOf(">")) + ">";
//		System.out.println(content);
		
		int index = -1;
//		String content = "<g id='2' size=\"11\" complexscriptssize=\"11\" complexscriptsbold=\"on\" bold=\"on\" superscript=\"on\"><ph>&lt;symbol font=&quot;Symbol&quot; character=&quot;F0E2&quot;/&gt;</ph></g>";
//		String content = "<g id='2' size=\"11\" complexscriptssize=\"11\" complexscriptsbold=\"on\" bold=\"on\" superscript=\"on\"><ph>&lt;field/&gt;</ph></g>";
//		String content = "<g id='2' size=\"11\" complexscriptssize=\"11\" complexscriptsbold=\"on\" bold=\"on\" superscript=\"on\"><ph>&lt;/cf&gt;</ph></g><ph>&lt;field/&gt;</ph>";
		String content = "<g id='2' size=\"11\" complexscriptssize=\"11\" complexscriptsbold=\"on\" bold=\"on\" superscript=\"on\"><ph>&lt;/cf&gt;</ph></g>这后面是个cf标记了哦。<ph type='cf'>&lt;/cf&gt;</ph>";
		index = content.indexOf("<ph");
		while(index != -1){
			String phFrag = content.substring(index, content.indexOf("</ph>", index) + 5);
			
			System.out.println(phFrag);
			try {
				VTDGen vg = new VTDGen();
				vg.setDoc(phFrag.getBytes());
				vg.parse(true);
				VTDNav vn = vg.getNav();
				AutoPilot ap = new AutoPilot(vn);
				VTDUtils vu = new VTDUtils(vn);
				ap.selectXPath("/ph");
				String replaceText = "";
				String phContent = "";
				int attrIdx = -1;
				if (ap.evalXPath() != -1) {
					phContent = vu.getElementContent();
//					System.out.println("phContent = " + phContent);
					// 有type属性的,一般是cf标记
					if ((attrIdx = vn.getAttrVal("type")) != -1) {
						if ("cf".equals(vn.toString(attrIdx))) {
							String utType = "";
							// 这个cf是开始还是结束&lt;cf size=&quot;11&quot; complexscriptssize=&quot;11&quot;&gt;
							if (phContent.indexOf("&lt;cf") != -1) {
								utType = "start";
							}else {
								utType = "end";
							}
							replaceText = "<ut Type=\"" + utType + "\" RightEdge=\"angle\" DisplayText=\"cf\">" + phContent + "</ut>";
						}
					}else {
						//没有type的,是其他标记,如&lt;symbol font=&quot;Symbol&quot; character=&quot;F0E2&quot;/&gt
						String tagName = "";
						int startIdx = -1;
						int endIdx = -1;
						// 针对起始标记如&lt;symbol font=&quot;Symbol&quot; character=&quot;F0E2&quot;/&gt
						if ((startIdx = phContent.trim().indexOf("&lt;")) != -1) {
							//针对结束标记如&lt;/null?&gt;
							if ("/".equals(phContent.trim().substring(startIdx + 4, startIdx + 5))) {
								tagName = phContent.trim().substring(startIdx + 5, phContent.trim().indexOf("&gt;"));
							}else {
								if ((endIdx = phContent.trim().indexOf(" ")) != -1) {
									tagName = phContent.trim().substring(startIdx + 4, endIdx);
								}else {
									//针对没有空格的如<ph>&lt;field/&gt;</ph>
									tagName = phContent.trim().substring(startIdx + 4, phContent.trim().indexOf("/"));
								}	
							}
//							System.out.println("tagName = '" + tagName + "'");
						}
//						System.out.println( "tagName = " + tagName);
						replaceText = "<ut DisplayText=\"" + tagName + "\">" + phContent + "</ut>";
						
					}
					content = content.replace(phFrag, replaceText);
//					System.out.println(content);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			index = content.indexOf("<ph", index + 1);
		}
//		System.out.println(content);
		
		
		String tagStr = "<g id='1' size='12'/>";
		String newTagStr = "<cf" + tagStr.substring(tagStr.indexOf("<g") + 2, tagStr.indexOf("/>")) + ">";
		newTagStr = "<ut Type=\"start\" RightEdge=\"angle\" DisplayText=\"cf\">" + TextUtil.cleanSpecialString(newTagStr) + "</ut>";
//		System.out.println("newTagStr = " + newTagStr);
		
	}
 
Example 17
Source File: Xliff2Ttx.java    From translationstudio8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 处理 ph 节点
 * @return
 * @throws Exception
 */
private String replacePH(String phFrag) throws Exception{
	String replaceText = "";
	VTDGen vg = new VTDGen();
	vg.setDoc(phFrag.getBytes());
	vg.parse(true);
	VTDNav vn = vg.getNav();
	AutoPilot ap = new AutoPilot(vn);
	VTDUtils vu = new VTDUtils(vn);
	ap.selectXPath("/ph");
	String phContent = "";
	int attrIdx = -1;
	String utType = "";
	String edgeStr = "";
	if (ap.evalXPath() != -1) {
		phContent = vu.getElementContent();
		// 有type属性的,一般是cf标记
		if ((attrIdx = vn.getAttrVal("type")) != -1) {
			if ("cf".equals(vn.toString(attrIdx))) {
				// 这个cf是开始还是结束&lt;cf size=&quot;11&quot; complexscriptssize=&quot;11&quot;&gt;
				if (phContent.indexOf("&lt;cf") != -1) {
					utType = "start";
					edgeStr = "RightEdge=\"angle\"";
				}else {
					utType = "end";
					edgeStr = "LeftEdge=\"angle\"";
				}
				replaceText += "<ut Type=\"" + utType + "\" "+ edgeStr +" DisplayText=\"cf\">" + phContent + "</ut>";
			}
		}else {
			//没有type的,是其他标记,如&lt;symbol font=&quot;Symbol&quot; character=&quot;F0E2&quot;/&gt
			String tagName = "";
			int startIdx = -1;
			int endIdx = -1;
			// 针对起始标记如&lt;symbol font=&quot;Symbol&quot; character=&quot;F0E2&quot;/&gt
			if ((startIdx = phContent.trim().indexOf("&lt;")) != -1) {
				//针对结束标记如&lt;/null?&gt;
				if ("/".equals(phContent.trim().substring(startIdx + 4, startIdx + 5))) {
					tagName = phContent.trim().substring(startIdx + 5, phContent.trim().indexOf("&gt;"));
					utType = "end";
					edgeStr = " LeftEdge=\"angle\"";
				}else {
					if ((endIdx = phContent.trim().indexOf(" ")) != -1) {
						tagName = phContent.trim().substring(startIdx + 4, endIdx);
						if (phContent.indexOf("/&gt") != -1) {
							utType = "";
						}else {
							utType = "start";
							edgeStr = " RightEdge=\"angle\"";
						}
					}else if((endIdx = phContent.trim().indexOf("/")) != -1) {
						//针对没有空格的如<ph>&lt;field/&gt;</ph>
						tagName = phContent.trim().substring(startIdx + 4, phContent.trim().indexOf("/"));
						utType = "";
					}else{
						//针对 &lt;field&gt;
						tagName = phContent.trim().substring(startIdx + 4, phContent.trim().indexOf("&gt"));
						utType = "start";
						edgeStr = " RightEdge=\"angle\"";
					}	
				}
			}
			String utTypeAttrStr = utType.length() > 0 ? " Type=\""+utType+"\"" : "";
			replaceText = "<ut"+ utTypeAttrStr + edgeStr +" DisplayText=\"" + tagName + "\">" + phContent + "</ut>";
		}
	}
	return replaceText;
}
 
Example 18
Source File: IDML2XLIFF.java    From translationstudio8 with GNU General Public License v2.0 4 votes vote down vote up
public String handlePara(String para, HashMap<String, String> mapDelete) throws Exception {
	StringBuffer sb = new StringBuffer();
	List<Integer> lstDepth = new ArrayList<Integer>();
	VTDGen vg = new VTDGen();
	vg.setDoc(para.getBytes());
	vg.parse(true);
	VTDNav vn = vg.getNav();
	XMLModifier xm = new XMLModifier(vn);
	AutoPilot ap = new AutoPilot(vn);
	VTDUtils vu = new VTDUtils(vn);
	boolean isFind = false;
	int curDepth = 0;
	ArrayList<String> lstSegment = new ArrayList<String>();
	ArrayList<Integer> lstIndex = new ArrayList<Integer>();
	ap.selectXPath("/ParagraphStyleRange/descendant::node()");
	StringBuffer sbSkl = new StringBuffer();
	StringBuffer sbXLF = new StringBuffer();
	StringBuffer sbContent = new StringBuffer();
	AutoPilot ap2 = new AutoPilot(vn);
	AutoPilot childAp = new AutoPilot(vn);
	while (ap.evalXPath() != -1) {
		String nodeName = vu.getCurrentElementName();
		curDepth = vn.getCurrentDepth();
		// 跳过 ParagraphStyleRange 节点
		if (nodeName.equalsIgnoreCase("ParagraphStyleRange")) {
			lstDepth.add(curDepth);
			// vn.pop();
			// 一个文本段结束,要进行分段
			if (sb.length() > 0) {
				vn.push();
				updateParaAndXLF(sb, vn, lstSegment, sbSkl, sbContent, sbXLF, xm, ap2, childAp, lstIndex);
				vn.pop();
			}
			continue;
		} else {
			isFind = false;
			Iterator<Integer> it = lstDepth.iterator();
			while (it.hasNext()) {
				Integer depth = (Integer) it.next();
				if (curDepth >= depth) {
					isFind = true;
					it.remove();
				}
			}
			if (isFind || nodeName.equalsIgnoreCase("Br") || nodeName.equalsIgnoreCase("Table")) {
				// 一个文本段结束,要进行分段
				if (sb.length() > 0) {
					vn.push();
					updateParaAndXLF(sb, vn, lstSegment, sbSkl, sbContent, sbXLF, xm, ap2, childAp, lstIndex);
					vn.pop();
				}
			}
			if (nodeName.equalsIgnoreCase("Content")) {
				String seg = vu.getElementContent();
				sb.append(seg);
				lstSegment.add(seg);
				lstIndex.add(vn.getCurrentIndex());
			}
		}
	}
	if (sb.length() > 0) {
		vn.push();
		updateParaAndXLF(sb, vn, lstSegment, sbSkl, sbContent, sbXLF, xm, ap2, childAp, lstIndex);
		vn.pop();
	}

	if (mapDelete.size() > 0) {
		childAp.bind(vn);
		ap.selectXPath("/ParagraphStyleRange/descendant::node()[text()!='' and starts-with(normalize-space(text()),'%%%%') and ends-with(normalize-space(text()),'%%%%')]");
		while (ap.evalXPath() != -1) {
			String strId = vu.getElementPureText().trim();
			String strPrimary = mapDelete.get(strId);
			vn.push();
			childAp.selectXPath("./text()");
			if (childAp.evalXPath() != -1) {
				xm.updateToken(vn.getCurrentIndex(), strPrimary);
			}
			vn.pop();
		}
	}

	vn = xm.outputAndReparse();
	vu.bind(vn);
	return vu.getElementFragment();
}