List of usage examples for org.jdom2.xpath XPathExpression evaluate
public List<T> evaluate(Object context);
From source file:AIR.Common.xml.XmlElement.java
License:Open Source License
public List<Element> selectNodes(String xpath) { XPathFactory factory = XPathFactory.instance(); XPathExpression<Element> expr = factory.compile(xpath, Filters.element()); return expr.evaluate(_element); }
From source file:AIR.Common.xml.XmlElement.java
License:Open Source License
public List<Element> selectNodes(String xpath, XmlNamespaceManager nsmgr) { XPathFactory factory = XPathFactory.instance(); XPathExpression<Element> expr = factory.compile(xpath, Filters.element(), null, nsmgr.getNamespaceList()); return expr.evaluate(_element); }
From source file:at.newmedialab.ldpath.model.functions.XPathFunction.java
License:Apache License
private LinkedList<String> doFilter(String in, Set<String> xpaths) throws IOException { LinkedList<String> result = new LinkedList<String>(); try {/* w w w . ja v a2 s . c o m*/ Document doc = new SAXBuilder(XMLReaders.NONVALIDATING).build(new StringReader(in)); XMLOutputter out = new XMLOutputter(); for (String xp : xpaths) { XPathExpression<Content> xpath = XPathFactory.instance().compile(xp, Filters.content()); for (Content node : xpath.evaluate(doc)) { if (node instanceof Element) result.add(out.outputString((Element) node)); else if (node instanceof Text) result.add(out.outputString((Text) node)); } } return result; } catch (JDOMException xpe) { throw new IllegalArgumentException("error while processing xpath expressions: '" + xpaths + "'", xpe); } }
From source file:ataraxis.passwordmanager.XMLHandler.java
License:Open Source License
/** * Return a List of all groups//from w ww. ja v a 2s . c o m * @return the GroupList * @throws JDOMException by problems with JDOM */ private List<Element> getGroupList() throws JDOMException { XPathExpression<Element> xpath = XPathFactory.instance().compile("//group", Filters.element()); return xpath.evaluate(s_doc); }
From source file:ataraxis.passwordmanager.XMLHandler.java
License:Open Source License
/** * Return a List of all entries// www . ja v a 2 s .co m * @return the GroupList * @throws JDOMException by problems with JDOM */ private List<Element> getAccountList() throws JDOMException { XPathExpression<Element> xpath = XPathFactory.instance().compile("//account", Filters.element()); return xpath.evaluate(s_doc); }
From source file:cager.parser.test.SimpleTest2.java
License:Open Source License
private void XMLtoJavaParser() { // Creamos el builder basado en SAX SAXBuilder builder = new SAXBuilder(); try {//from w w w. j a va2 s .c o m String nombreMetodo = null; List<String> atributos = new ArrayList<String>(); String tipoRetorno = null; String nombreArchivo = null; String exportValue = ""; String codigoFuente = ""; HashMap<String, String> tipos = new HashMap<String, String>(); // Construimos el arbol DOM a partir del fichero xml Document doc = builder.build(new FileInputStream(ruta + "VBParser\\ejemplosKDM\\archivo.kdm")); //Document doc = builder.build(new FileInputStream("E:\\WorkspaceParser\\VBParser\\ejemplosKDM\\archivo.kdm")); Namespace xmi = Namespace.getNamespace("xmi", "http://www.omg.org/XMI"); XPathExpression<Element> xpath = XPathFactory.instance().compile("//codeElement", Filters.element()); List<Element> elements = xpath.evaluate(doc); for (Element emt : elements) { if (emt.getAttribute("type", xmi).getValue().compareTo("code:CompilationUnit") == 0) { nombreArchivo = emt.getAttributeValue("name").substring(0, emt.getAttributeValue("name").indexOf('.')); } if (emt.getAttribute("type", xmi).getValue().compareTo("code:LanguageUnit") == 0) { List<Element> hijos = emt.getChildren(); for (Element hijo : hijos) { tipos.put(hijo.getAttributeValue("id", xmi), hijo.getAttributeValue("name")); } } } FileOutputStream fout; fout = new FileOutputStream(ruta + "VBParser\\src\\cager\\parser\\test\\" + nombreArchivo + ".java"); //fout = new FileOutputStream("E:\\WorkspaceParser\\VBParser\\src\\cager\\parser\\test\\"+nombreArchivo+".java"); // get the content in bytes byte[] contentInBytes = null; contentInBytes = ("package cager.parser.test;\n\n").getBytes(); fout.write(contentInBytes); fout.flush(); contentInBytes = ("public class " + nombreArchivo + "{\n\n").getBytes(); fout.write(contentInBytes); fout.flush(); for (Element emt : elements) { // System.out.println("XPath has result: " + emt.getName()+" "+emt.getAttribute("type",xmi)); if (emt.getAttribute("type", xmi).getValue().compareTo("code:MethodUnit") == 0) { nombreMetodo = emt.getAttribute("name").getValue(); if (emt.getAttribute("export") != null) exportValue = emt.getAttribute("export").getValue(); atributos = new ArrayList<String>(); List<Element> hijos = emt.getChildren(); for (Element hijo : hijos) { if (hijo.getAttribute("type", xmi) != null) { if (hijo.getAttribute("type", xmi).getValue().compareTo("code:Signature") == 0) { List<Element> parametros = hijo.getChildren(); for (Element parametro : parametros) { if (parametro.getAttribute("kind") == null || parametro.getAttribute("kind").getValue().compareTo("return") != 0) { atributos.add(tipos.get(parametro.getAttribute("type").getValue()) + " " + parametro.getAttributeValue("name")); } else { tipoRetorno = tipos.get(parametro.getAttribute("type").getValue()); } } } } else if (hijo.getAttribute("snippet") != null) { codigoFuente = hijo.getAttribute("snippet").getValue(); } } //System.out.println("MethodUnit!! " + emt.getName()+" "+emt.getAttribute("type",xmi)+" "+emt.getAttribute("name").getValue()); //System.out.println(emt.getAttribute("name").getValue()); if (tipoRetorno.compareTo("Void") == 0) { tipoRetorno = "void"; } contentInBytes = ("\t" + exportValue + " " + tipoRetorno + " " + nombreMetodo + " (") .getBytes(); fout.write(contentInBytes); fout.flush(); int n = 0; for (String parametro : atributos) { if (atributos.size() > 0 && n < atributos.size() - 1) { contentInBytes = (" " + parametro + ",").getBytes(); } else { contentInBytes = (" " + parametro).getBytes(); } fout.write(contentInBytes); fout.flush(); n++; } contentInBytes = (" ) {\n").getBytes(); fout.write(contentInBytes); fout.flush(); contentInBytes = ("\n/* \n " + codigoFuente + " \n */\n").getBytes(); fout.write(contentInBytes); fout.flush(); contentInBytes = ("\t}\n\n").getBytes(); fout.write(contentInBytes); fout.flush(); System.out.print("\t" + exportValue + " " + tipoRetorno + " " + nombreMetodo + " ("); n = 0; for (String parametro : atributos) { if (atributos.size() > 0 && n < atributos.size() - 1) { System.out.print(" " + parametro + ", "); } else { System.out.print(" " + parametro); } n++; } System.out.println(" ) {"); System.out.println("/* \n " + codigoFuente + " \n */"); System.out.println("\t}\n"); } } contentInBytes = ("}\n").getBytes(); fout.write(contentInBytes); fout.flush(); fout.close(); XPathExpression<Attribute> xp = XPathFactory.instance().compile("//@*", Filters.attribute(xmi)); for (Attribute a : xp.evaluate(doc)) { a.setName(a.getName().toLowerCase()); } xpath = XPathFactory.instance().compile("//codeElement/@name='testvb.cls'", Filters.element()); Element emt = xpath.evaluateFirst(doc); if (emt != null) { System.out.println("XPath has result: " + emt.getName()); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JDOMException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:com.c4om.autoconf.ulysses.extra.svrlinterpreter.SVRLInterpreterExtractor.java
License:Apache License
/** * Method that actually extracts the info *///from www. j a va 2 s. c o m public List<List<String>> extractInfo() { XPathExpression<Element> xpathExpression = xpathFactory.compile(XPATH_STRING, Filters.element(), null, NAMESPACE_SVRL); List<Element> xpathResults = xpathExpression.evaluate(document); List<List<String>> results = new ArrayList<>(xpathResults.size()); for (Element element : xpathResults) { List<String> result = new ArrayList<>(2); result.add(0, element.getChildTextNormalize("text", NAMESPACE_SVRL).replace(METAMODEL_DIR_REPLACE_TOKEN, metamodelPath)); result.add(1, element.getChildTextNormalize("diagnostic-reference", NAMESPACE_SVRL)); // result.add(2,element.getAttributeValue("location")); if (!results.contains(result)) { results.add(result); } } return results; }
From source file:com.c4om.utils.xmlutils.XMLLibsShortcuts.java
License:Apache License
/** * Simple method that performs a XPath query over elements * @param <T> The type of the XPath result. * @param path the path/*from w ww. j av a2 s. c om*/ * @param context a context node * @param filter an appropiate filter. Its type will be the one of the result * @param namespaces namespaces made available to XPath engine * @return the list of results */ public static <T> List<T> performJAXENXPath(String path, Object context, Filter<T> filter, Collection<Namespace> namespaces) { XPathBuilder<T> builder = new XPathBuilder<>(path, filter); if (namespaces != null) { builder.setNamespaces(namespaces); } XPathExpression<T> xpathExpression = builder.compileWith(XPATH_FACTORY); List<T> xpathResults = xpathExpression.evaluate(context); return xpathResults; }
From source file:com.globalsight.dispatcher.bo.JobTask.java
License:Apache License
private void createTargetFile(JobBO p_job, String[] p_targetSegments) throws IOException { OutputStream writer = null;//from w w w . j a v a 2 s . c o m File fileStorage = CommonDAO.getFileStorage(); File srcFile = p_job.getSrcFile(); Account account = DispatcherDAOFactory.getAccountDAO().getAccount(p_job.getAccountId()); File trgDir = CommonDAO.getFolder(fileStorage, account.getAccountName() + File.separator + p_job.getJobID() + File.separator + AppConstants.XLF_TARGET_FOLDER); File trgFile = new File(trgDir, srcFile.getName()); FileUtils.copyFile(srcFile, trgFile); String encoding = FileUtil.getEncodingOfXml(trgFile); try { SAXBuilder builder = new SAXBuilder(); Document doc = builder.build(p_job.getSrcFile()); Element root = doc.getRootElement(); // Get root element Namespace namespace = root.getNamespace(); Element fileElem = root.getChild("file", namespace); XPathFactory xFactory = XPathFactory.instance(); XPathExpression<Element> expr = xFactory.compile("//trans-unit", Filters.element(), null, namespace); List<Element> tuList = expr.evaluate(fileElem.getChild("body", namespace)); for (int tuIndex = 0, trgIndex = 0; tuIndex < tuList.size() && trgIndex < p_targetSegments.length; tuIndex++, trgIndex++) { if (p_targetSegments[trgIndex] == null) { continue; } Element elem = (Element) tuList.get(tuIndex); Element srcElem = elem.getChild("source", namespace); Element trgElem = elem.getChild("target", namespace); if (srcElem == null || srcElem.getContentSize() == 0) { trgIndex--; continue; } if (trgElem != null) { setTargetSegment(trgElem, p_targetSegments[trgIndex], encoding); } else { trgElem = new Element("target", namespace); setTargetSegment(trgElem, p_targetSegments[trgIndex], encoding); elem.addContent(trgElem); } } XMLOutputter xmlOutput = new XMLOutputter(); Format format = Format.getRawFormat(); format.setEncoding(encoding); writer = new FileOutputStream(trgFile); xmlOutput.setFormat(format); writeBOM(writer, format.getEncoding()); xmlOutput.output(doc, writer); p_job.setTrgFile(trgFile); logger.info("Create Target File: " + trgFile); } catch (JDOMException e1) { logger.error("CreateTargetFile Error: ", e1); } catch (IOException e1) { logger.error("CreateTargetFile Error: ", e1); } finally { if (writer != null) writer.close(); } }
From source file:com.globalsight.dispatcher.controller.TranslateXLFController.java
License:Apache License
private String parseXLF(JobBO p_job, File p_srcFile) { if (p_srcFile == null || !p_srcFile.exists()) return "File not exits."; String srcLang, trgLang;//from w w w . j a va 2s . c o m List<String> srcSegments = new ArrayList<String>(); try { SAXBuilder builder = new SAXBuilder(); Document read_doc = builder.build(p_srcFile); // Get Root Element Element root = read_doc.getRootElement(); Namespace namespace = root.getNamespace(); Element fileElem = root.getChild("file", namespace); // Get Source/Target Language srcLang = fileElem.getAttributeValue(XLF_SOURCE_LANGUAGE); trgLang = fileElem.getAttributeValue(XLF_TARGET_LANGUAGE); XPathFactory xFactory = XPathFactory.instance(); XPathExpression<Element> expr = xFactory.compile("//trans-unit", Filters.element(), null, namespace); List<Element> list = expr.evaluate(fileElem.getChild("body", namespace)); for (int i = 0; i < list.size(); i++) { Element tuElem = (Element) list.get(i); Element srcElem = tuElem.getChild("source", namespace); // Get Source Segment if (srcElem != null && srcElem.getContentSize() > 0) { String source = getInnerXMLString(srcElem); srcSegments.add(source); } } p_job.setSourceLanguage(srcLang); p_job.setTargetLanguage(trgLang); p_job.setSourceSegments(srcSegments); } catch (Exception e) { String msg = "Parse XLIFF file error."; logger.error(msg, e); return msg; } return null; }