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

The following examples show how to use com.ximpleware.VTDGen#getNav() . 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 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 2
Source File: PluginConfigManageDialog.java    From translationstudio8 with GNU General Public License v2.0 6 votes vote down vote up
private void editPlugindata(PluginConfigBean bean) {
	VTDGen vg = new VTDGen();
	vg.parseFile(pluginXmlLocation, true);
	VTDNav vn = vg.getNav();
	AutoPilot ap = new AutoPilot(vn);
	try {
		ap.selectXPath(manage.buildXpath(curPluginBean));
		XMLModifier xm = new XMLModifier(vn);
		while (ap.evalXPath() != -1) {
			xm.remove();
			xm.insertAfterElement(manage.buildPluginData(bean));
			manage.updataPluginMenu(bean);
		}
		FileOutputStream fos = new FileOutputStream(pluginXmlLocation);
		BufferedOutputStream bos = new BufferedOutputStream(fos);
		xm.output(bos); // 写入文件
		bos.close();
		fos.close();
	} catch (Exception e) {
		LOGGER.error("", e);
	}
}
 
Example 3
Source File: Du2Xliff.java    From tmxeditor8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 解析骨架文件,此时的骨架文件的内容就是源文件的内容
 * @throws Exception 
 */
private void parseSkeletonFile() throws Exception{
	String errorInfo = "";
	VTDGen vg = new VTDGen();
	if (vg.parseFile(skeletonFile, true)) {
		sklVN = vg.getNav();
		sklXM = new XMLModifier(sklVN);
	}else {
		errorInfo = MessageFormat.format(Messages.getString("du.parse.msg1"), 
				new Object[]{new File(inputFile).getName()});
		throw new Exception(errorInfo);
	}
}
 
Example 4
Source File: PPTX2XLIFF.java    From translationstudio8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 解析 ppt/slideMasters/_rels 目录下的 rels 文件,确定 slideMasterN.xml 与 slideLayoutN.xml 的对应关系
 * @throws XPathParseException
 * @throws XPathEvalException
 * @throws NavException
 *             ;
 */
private void parseSlideMasterRels(IProgressMonitor monitor) throws XPathParseException, XPathEvalException,
		NavException {
	File relsFile = new File(strTmpFolderPath + File.separator + "ppt" + File.separator + "slideMasters"
			+ File.separator + "_rels");
	if (relsFile.isDirectory()) {
		File[] arrRelFile = relsFile.listFiles();
		monitor.beginTask("", arrRelFile.length);
		VTDGen vg = new VTDGen();
		AutoPilot ap = new AutoPilot();
		VTDUtils vu = new VTDUtils();
		for (File relFile : arrRelFile) {
			String name = relFile.getName();
			monitor.subTask(MessageFormat.format(Messages.getString("pptx.PPTX2XLIFF.task4"), name));
			if (relFile.isFile() && name.toLowerCase().endsWith(".rels")) {
				if (vg.parseFile(relFile.getAbsolutePath(), true)) {
					ArrayList<String> lstLayout = new ArrayList<String>();
					VTDNav vn = vg.getNav();
					ap.bind(vn);
					vu.bind(vn);
					ap.selectXPath("/Relationships/Relationship");
					while (ap.evalXPath() != -1) {
						String strTarget = vu.getCurrentElementAttribut("Target", "");
						if (strTarget.indexOf("slideLayout") != -1) {
							strTarget = strTarget.substring(strTarget.lastIndexOf("/") + 1);
							lstLayout.add(strTarget);
						}
					}
					mapLayout.put(name.substring(0, name.lastIndexOf(".")), lstLayout);
				}
			}
			monitor.worked(1);
		}
	}
	monitor.done();
}
 
Example 5
Source File: Xliff2Du.java    From translationstudio8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 解析结果文件(解析时这个结果文件还是一个骨架文件)
 * @param file
 * @throws Exception
 */
private void parseOutputFile(String file, String xliffFile) throws Exception {
	VTDGen vg = new VTDGen();
	if (vg.parseFile(file, true)) {
		outputVN = vg.getNav();
		outputXM = new XMLModifier(outputVN);
		outputAP = new AutoPilot(outputVN);
		outputAP.declareXPathNameSpace("sdl", "http://sdl.com/FileTypes/SdlXliff/1.0");
	}else {
		String errorInfo = MessageFormat.format(Messages.getString("du.parse.msg2"), 
				new Object[]{new File(xliffFile).getName()});
		throw new Exception(errorInfo);
	}
}
 
Example 6
Source File: Wf2Xliff.java    From tmxeditor8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 解析骨架文件,此时的骨架文件的内容就是源文件的内容
 * @throws Exception 
 */
private void parseSkeletonFile() throws Exception{
	String errorInfo = "";
	VTDGen vg = new VTDGen();
	if (vg.parseFile(skeletonFile, true)) {
		sklVN = vg.getNav();
		sklXM = new XMLModifier(sklVN);
	}else {
		errorInfo = MessageFormat.format(Messages.getString("wf.parse.msg1"), 
				new Object[]{new File(inputFile).getName()});
		throw new Exception(errorInfo);
	}
}
 
Example 7
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 8
Source File: VTDUtilsTest.java    From translationstudio8 with GNU General Public License v2.0 5 votes vote down vote up
@BeforeClass
public static void setUp() throws Exception {
	VTDGen vg = new VTDGen();
	if (vg.parseFile(testFile, true)) {
		vn = vg.getNav();
		vu = new VTDUtils(vn);
	}
}
 
Example 9
Source File: DocumentRelation.java    From tmxeditor8 with GNU General Public License v2.0 5 votes vote down vote up
private void loadFile() throws Exception {
	VTDGen vg = new VTDGen();
	if (vg.parseFile(xmlPath, true)) {
		vn = vg.getNav();
		ap = new AutoPilot(vn);
	}else {
		throw new Exception(MessageFormat.format(Messages.getString("docxConvert.msg2"), xmlPath));
	}
}
 
Example 10
Source File: ExportConfig.java    From translationstudio8 with GNU General Public License v2.0 5 votes vote down vote up
private void writeDocumentxml(XliffBean bean) throws ExportExternalException {
	VTDGen vg = new VTDGen();
	if (!vg.parseZIPFile(templateDocxFile, "word/document.xml", true)) {
		canceled();
		throw new ExportExternalException(Messages.getString("ExportExternal.error.templateError"));
	}
	try {
		VTDNav vn = vg.getNav();
		AutoPilot ap = new AutoPilot(vn);
		ap.declareXPathNameSpace("w", ExportExternal.NAMESPACE_W);
		ap.selectXPath("/w:document");
		if (ap.evalXPath() == -1) {
			throw new ExportExternalException(Messages.getString("ExportExternal.error.templateError"));
		}

		long l = vn.getElementFragment();
		int dOffset = (int) l;
		int dLength = (int) (l >> 32);

		ap.selectXPath("/w:document/w:body/w:p");
		if (ap.evalXPath() == -1) {
			// we should write in body!!
			canceled();
			throw new ExportExternalException(Messages.getString("ExportExternal.error.templateError"));
		} else {
			l = vn.getElementFragment();
			int offset = (int) l;
			int length = (int) (l >> 32);
			String str = vn.toString(0, length + offset);
			docxmlWriter.write(str);
			doc_end_fragment = vn.toString(length + offset, dOffset + dLength - offset - length);
		}
		//we should export some info about this unclean
	} catch (Exception e) {
		LOGGER.error("", e);
		canceled();
		throw new ExportExternalException(e.getMessage(), e);
	}
}
 
Example 11
Source File: ExportConfig.java    From translationstudio8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * sdl 双语样式。
 * @throws ExportExternalException
 *             模板错误或者程序内部错误
 */
private void addSDLStyle() throws ExportExternalException {
	VTDGen vg = new VTDGen();
	if (!vg.parseZIPFile(templateDocxFile, "word/styles.xml", true)) {
		throw new ExportExternalException(Messages.getString("ExportExternal.error.templateError"));
	}
	VTDNav vn = vg.getNav();
	AutoPilot ap = new AutoPilot(vn);
	ap.declareXPathNameSpace("w", ExportExternal.NAMESPACE_W);

	String styleCode = "<w:style w:type=\"character\" w:customStyle=\"1\" w:styleId=\"tw4winMark\"><w:name w:val=\"tw4winMark\"/>"
			+ "<w:rPr><w:rFonts w:ascii=\"Courier New\" w:hAnsi=\"Courier New\" w:cs=\"Courier New\"/><w:vanish/><w:color w:val=\"800080\"/>"
			+ "</w:rPr></w:style>\n";
	String styleCode_tag = "<w:style w:type=\"character\" w:styleId=\"Tag\" w:customStyle=\"true\">"
			+ "<w:name w:val=\"Tag\"/><w:basedOn w:val=\"DefaultParagraphFont\"/><w:uiPriority w:val=\"1\"/><w:qFormat/><w:rPr><w:i/><w:color w:val=\"FF0066\"/></w:rPr></w:style>";
	String rowidStyle = "<w:style w:type=\"character\" w:styleId=\"HSRow\" w:customStyle=\"true\">" +
			"<w:name w:val=\"HSRow\" />" +
			"<w:rPr><w:rFonts w:ascii=\"Consolas\"/><w:color w:val=\"0070C0\"/><w:b /></w:rPr></w:style>";
	
	try {
		ap.selectXPath("/w:styles/w:style[last()]");
		if (ap.evalXPath() != -1) {
			XMLModifier xm = new XMLModifier(vn);
			xm.insertAfterElement(styleCode + styleCode_tag + rowidStyle);
			xm.output(tmpUnzipFolder + "/word/styles.xml");
		}
	} catch (Exception e) {
		LOGGER.error("", e);
		throw new ExportExternalException(e.getMessage(), e);
	}
}
 
Example 12
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 13
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 14
Source File: Xliff2Du.java    From tmxeditor8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 解析要被逆转换的xliff文件
 * @param xliffFile
 * @throws Exception
 */
private void parseXlfFile(String xliffFile) throws Exception {
	VTDGen vg = new VTDGen();
	if (vg.parseFile(xliffFile, true)) {
		hsxlfVN = vg.getNav();
	}else {
		String errorInfo = MessageFormat.format(Messages.getString("du.parse.msg1"), 
				new Object[]{new File(xliffFile).getName()});
		throw new Exception(errorInfo);
	}
}
 
Example 15
Source File: Xliff2Wf.java    From tmxeditor8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 解析结果文件(解析时这个结果文件还是一个骨架文件)
 * @param file
 * @throws Exception
 */
private void parseOutputFile(String file, String xliffFile) throws Exception {
	VTDGen vg = new VTDGen();
	if (vg.parseFile(file, true)) {
		outputVN = vg.getNav();
		outputXM = new XMLModifier(outputVN);
		outputAP = new AutoPilot(outputVN);
	}else {
		String errorInfo = MessageFormat.format(Messages.getString("wf.parse.msg2"), 
				new Object[]{new File(xliffFile).getName()});
		throw new Exception(errorInfo);
	}
}
 
Example 16
Source File: TmxFileContainer.java    From tmxeditor8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Parse current TMX file with VTD
 * @return <code>VTDUtils<code>
 * @throws Exception
 *             A set of VTD parse exception;
 */
public void parseFileWithVTD() throws Exception {
	File f = new File(this.filePath);
	if (!f.exists() || f.isDirectory()) {
		throw new FileNotFoundException(Messages.getString("tmxdata.TmxFileContainer.parseTmxFileNotFound"));
	}
	VTDGen vg = parseFile(f);
	vu = new VTDUtils(vg.getNav());
	loadTmxHeader(vu);
	getTUIndexCache(true);
	vg.clear();
}
 
Example 17
Source File: ConvertToXmlConfigMojo.java    From revapi with Apache License 2.0 4 votes vote down vote up
private void updateAllConfigurationFiles(MavenProject project, Map<String, ModelNode> extensionSchemas,
                                         int indentationSize) throws Exception {
    VTDGen gen = new VTDGen();
    gen.enableIgnoredWhiteSpace(true);
    gen.parseFile(project.getFile().getAbsolutePath(), true);

    VTDNav nav = gen.getNav();
    XMLModifier mod = new XMLModifier(nav);

    AutoPilot ap = new AutoPilot(nav);

    ThrowingConsumer<String> update = xpath -> {
        ap.resetXPath();
        ap.selectXPath(xpath);

        while (ap.evalXPath() != -1) {
            int textPos = nav.getText();

            String configFile = nav.toString(textPos);

            File newFile = updateConfigurationFile(new File(configFile), extensionSchemas, indentationSize);
            if (newFile == null) {
                continue;
            }

            mod.updateToken(textPos, newFile.getPath());
        }
    };

    update.accept("//plugin[groupId = 'org.revapi' and artifactId = 'revapi-maven-plugin']" +
            "/configuration/analysisConfigurationFiles/*[not(self::configurationFile)]");
    update.accept("//plugin[groupId = 'org.revapi' and artifactId = 'revapi-maven-plugin']" +
            "/configuration/analysisConfigurationFiles/configurationFile[not(roots)]/path");

    update.accept("//plugin[groupId = 'org.revapi' and artifactId = 'revapi-maven-plugin']" +
            "/executions/execution/configuration/analysisConfigurationFiles/*[not(self::configurationFile)]");
    update.accept("//plugin[groupId = 'org.revapi' and artifactId = 'revapi-maven-plugin']" +
            "/executions/execution/configuration/analysisConfigurationFiles/configurationFile[not(roots)]/path");

    try (OutputStream out = new FileOutputStream(project.getFile())) {
        mod.output(out);
    }
}
 
Example 18
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 19
Source File: CommonFunction.java    From translationstudio8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 关闭指定文件的编辑器		-- robert 2013-04-01
 * 备注:这里面的方法,是不能获取 nattable 的实例,故,在处理 合并打开的情况时,是通过 vtd 进行解析 合并临时文件从而获取相关文件的
 * @param iFileList
 */
public static void closePointEditor(List<IFile> iFileList){
	Map<IFile, IEditorPart> openedIfileMap = new HashMap<IFile, IEditorPart>();
	IEditorReference[] referenceArray = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getEditorReferences();
	for(IEditorReference reference : referenceArray){
		IEditorPart editor = reference.getEditor(true);
		IFile iFile = ((FileEditorInput)editor.getEditorInput()).getFile();
		// 如果这是一个 nattable 编辑器
		if (XLIFF_EDITOR_ID.equals(editor.getSite().getId())) {
			String iFilePath = iFile.getLocation().toOSString();
			String extension = iFile.getFileExtension();
			if ("hsxliff".equals(extension)) {
				openedIfileMap.put(iFile, editor);
			}else if ("xlp".equals(extension)) {
				// 这是合并打开的情况
				// 开始解析这个合并打开临时文件,获取合并打开的文件。
				VTDGen vg = new VTDGen();
				if (vg.parseFile(iFilePath, true)) {
					VTDNav vn = vg.getNav();
					AutoPilot ap = new AutoPilot(vn);
					try {
						ap.selectXPath("/mergerFiles/mergerFile/@filePath");
						int index = -1;
						while ((index = ap.evalXPath()) != -1) {
							String fileLC = vn.toString(index + 1);
							if (fileLC != null && !"".equals(fileLC)) {
								openedIfileMap.put(ResourceUtils.fileToIFile(fileLC), editor);
							}
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		}else {
			// 其他情况,直接将文件丢进去就行了
			openedIfileMap.put(iFile, editor);
		}
		
		IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
		
		for(IFile curIfile : iFileList){
			if (openedIfileMap.containsKey(curIfile)) {
				page.closeEditor(openedIfileMap.get(curIfile), false);
			}
		}
	}
}
 
Example 20
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;
}