Java Code Examples for org.jdom.xpath.XPath#selectNodes()

The following examples show how to use org.jdom.xpath.XPath#selectNodes() . 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: Parser.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
private void 
preProcessResources(Element the_manifest,
  DefaultHandler the_handler) {
 XPath path;
 List<Attribute> results;
 try {
  path = XPath.newInstance(FILE_QUERY);
  path.addNamespace(ns.getNs());
  results = path.selectNodes(the_manifest);
  for (Attribute result : results) {
	  the_handler.preProcessFile(result.getValue()); 
  }
 } catch (JDOMException | ClassCastException e) {
  log.info("Error processing xpath for files", e);
 }
}
 
Example 2
Source File: Parser.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
private void 
preProcessResources(Element the_manifest,
  DefaultHandler the_handler) {
 XPath path;
 List<Attribute> results;
 try {
  path = XPath.newInstance(FILE_QUERY);
  path.addNamespace(ns.getNs());
  results = path.selectNodes(the_manifest);
  for (Attribute result : results) {
	  the_handler.preProcessFile(result.getValue()); 
  }
 } catch (JDOMException | ClassCastException e) {
  log.info("Error processing xpath for files", e);
 }
}
 
Example 3
Source File: ProjectWrangler.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void cleanUpProjectForImport(@NotNull File projectPath) {
  File dotIdeaFolderPath = new File(projectPath, Project.DIRECTORY_STORE_FOLDER);
  if (dotIdeaFolderPath.isDirectory()) {
    File modulesXmlFilePath = new File(dotIdeaFolderPath, "modules.xml");
    if (modulesXmlFilePath.isFile()) {
      SAXBuilder saxBuilder = new SAXBuilder();
      try {
        Document document = saxBuilder.build(modulesXmlFilePath);
        XPath xpath = XPath.newInstance("//*[@fileurl]");
        //noinspection unchecked
        List<Element> modules = xpath.selectNodes(document);
        int urlPrefixSize = "file://$PROJECT_DIR$/".length();
        for (Element module : modules) {
          String fileUrl = module.getAttributeValue("fileurl");
          if (!StringUtil.isEmpty(fileUrl)) {
            String relativePath = toSystemDependentName(fileUrl.substring(urlPrefixSize));
            File imlFilePath = new File(projectPath, relativePath);
            if (imlFilePath.isFile()) {
              FileUtilRt.delete(imlFilePath);
            }
            // It is likely that each module has a "build" folder. Delete it as well.
            File buildFilePath = new File(imlFilePath.getParentFile(), "build");
            if (buildFilePath.isDirectory()) {
              FileUtilRt.delete(buildFilePath);
            }
          }
        }
      }
      catch (Throwable ignored) {
        // if something goes wrong, just ignore. Most likely it won't affect project import in any way.
      }
    }
    FileUtilRt.delete(dotIdeaFolderPath);
  }
}
 
Example 4
Source File: HasXPath.java    From yatspec with Apache License 2.0 5 votes vote down vote up
private static List evaluate(XPath xpath, Document document) {
    try {
        return xpath.selectNodes(document);
    } catch (JDOMException e) {
        return emptyList();
    }
}
 
Example 5
Source File: AnnotationRulesFactoryImpl.java    From owltools with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private void loadRegex(Document doc) {
		try{
			XPath regexRule = XPath.newInstance("//implementation/script[@language='regex']");			
			List<?> regexRules = regexRule.selectNodes(doc);

			for (Object obj : regexRules) {
				Element scriptElement = (Element) obj;

				String regex = scriptElement.getTextNormalize();
				boolean isCaseInsensitive = regex.endsWith("/i");

				//java doesn't like the /^ switch so it is replaced by ^
				regex = regex.replace("/^", "^");
				//java does not support the /i case in-sensitivity switch so it is removed
				regex = regex.replace("/i", "");

				final Element ruleElement = scriptElement.getParentElement().getParentElement().getParentElement();
				
				final String id = getElementValue(ruleElement.getChild("id"), "");
				final String title = getElementValue(ruleElement.getChild("title"), "");
				final String description = getElementValue(ruleElement.getChild("description"), null);
				
				String status = null;
				Date date = null;
				final Element statusElement = ruleElement.getChild("status");
				if (statusElement != null) {
					status = statusElement.getTextNormalize();
					String dateString = statusElement.getAttributeValue("date", (String) null);
					if (dateString != null) {
						try {
							date = status_df.get().parse(dateString);
						} catch (ParseException e) {
							LOG.warn("Could not parse String as status date: "+dateString, e);
						}
					}
				}
				
				// TODO add parsing for grand fathering date
//				Date grandFatheringDate = null;
				

				Pattern pattern;

				if (isCaseInsensitive) {
					pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
				} else {
					pattern = Pattern.compile(regex);
				}

				AnnotationRegularExpressionFromXMLRule rule = new AnnotationRegularExpressionFromXMLRule();
				rule.setRuleId(id);
				rule.setName(title);
				rule.setDescription(description);
				rule.setStatus(status);
				rule.setDate(date);
//				rule.setGrandFatheringDate(grandFatheringDate);
				rule.setErrorMessage(title);
				rule.setPattern(pattern);
				rule.setRegex(regex);

				annotationRules.add(rule);
			}
		}catch(Exception ex){
			LOG.error(ex.getMessage(), ex);
		}
	}
 
Example 6
Source File: XChangeImporter.java    From elexis-3-core with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Find the registered Data handler that matches best the given element
 * 
 * @param el
 *            Element o be imported
 * @return the best matching handler or null if no handler exists at all for the given data type
 */
@SuppressWarnings("unchecked")
public IExchangeContributor findImportHandler(XChangeElement el){
	int matchedRestrictions = 0;
	IConfigurationElement cand = null;
	for (IConfigurationElement ice : container.getXChangeContributors()) {
		String datatype = ice.getAttribute("ElementType");
		if (datatype.equalsIgnoreCase(el.getXMLName())) {
			if (cand == null) {
				cand = ice;
			}
			String restriction = ice.getAttribute("restrictions");
			if (restriction != null) {
				String[] restrictions = restriction.split(",");
				int matches = 0;
				for (String r : restrictions) {
					try {
						XPath xpath = XPath.newInstance(r);
						List<Object> nodes = xpath.selectNodes(el);
						if (nodes.size() > 0) {
							if (++matches > matchedRestrictions) {
								cand = ice;
								matchedRestrictions = matches;
							} else if (matches == matchedRestrictions) {
								if (compareValues(ice, cand) == -1) {
									cand = ice;
								}
							}
						}
					} catch (JDOMException e) {
						ElexisStatus status =
							new ElexisStatus(ElexisStatus.WARNING, Hub.PLUGIN_ID,
								ElexisStatus.CODE_NONE, "Parse error JDOM: " + e.getMessage(),
								e, ElexisStatus.LOG_WARNINGS);
						throw new ExchangeException(status);
					}
				}
				
			} else {
				if (compareValues(ice, cand) == -1)
					cand = ice;
			}
		}
		
	}
	if (cand != null) {
		try {
			return (IExchangeContributor) cand.createExecutableExtension("Actor");
		} catch (CoreException ce) {
			ExHandler.handle(ce);
		}
	}
	return null;
}