private XPath createXPath(String xpathExpr) { HashMap<String, String> namespaceURIMap = new HashMap<String, String>(); namespaceURIMap.put("masc", "http://www.xces.org/ns/GrAF/1.0/"); XPath path = DocumentHelper.createXPath(xpathExpr); path.setNamespaceContext(new SimpleNamespaceContext(namespaceURIMap)); return path; }
/** * Returns a set of profiles for a user. If user has no profiles returns an empty set. * * @param username user's username. * @return set of profile ids. */ public static Set<String> getProfiles(String username) { Set<String> profiles = new HashSet<String>(); try { Document userProfiles = ProfilerUtil.getGtxProfile(username); XPath xpathProfiles = DocumentHelper.createXPath("//gtx:profiles/gtx:profile"); xpathProfiles.setNamespaceContext(new SimpleNamespaceContext(XmlUtil.getGtxNamespaceMap())); List<Node> nodes = xpathProfiles.selectNodes(userProfiles); for (Node node : nodes) { String profileId = ((Element) node).attributeValue("id"); // add profile to response profiles.add(profileId); } } catch (RegistrationNotFoundException e) { logger.error(GtmsLog.exceptionToString(e)); } return profiles; }
public static boolean isGtxProfile(String username, String profileName) { boolean result = false; try { Document userProfiles = ProfilerUtil.getGtxProfile(username); XPath xpathProfiles = DocumentHelper.createXPath("//gtx:profiles/gtx:profile"); xpathProfiles.setNamespaceContext(new SimpleNamespaceContext(XmlUtil.getGtxNamespaceMap())); @SuppressWarnings("unchecked") List<Node> nodes = xpathProfiles.selectNodes(userProfiles); for (Node node : nodes) { String profileId = ((Element) node).attributeValue("id"); if (!GenericValidator.isBlankOrNull(profileId) && profileId.equals(profileName)) { result = true; } } } catch (RegistrationNotFoundException e) { logger.error(GtmsLog.exceptionToString(e)); } return result; }
public boolean slurpOutput(FastPipedOutputStream stdout, FastPipedOutputStream stderr) throws IOException { log.log(Level.FINE, "--> SlurpOutput"); ImmutableMap<String, FastPipedOutputStream> streams = ImmutableMap.of("stdout", stdout, "stderr", stderr); Document request = factory.newGetOutputRequest(shellId, commandId).build(); Document response = sendRequest(request); XPath xpath = DocumentHelper.createXPath("//" + Namespaces.NS_WIN_SHELL.getPrefix() + ":Stream"); namespaceContext = new SimpleNamespaceContext(); namespaceContext.addNamespace(Namespaces.NS_WIN_SHELL.getPrefix(), Namespaces.NS_WIN_SHELL.getURI()); xpath.setNamespaceContext(namespaceContext); Base64 base64 = new Base64(); for (Element e : (List<Element>) xpath.selectNodes(response)) { FastPipedOutputStream stream = streams.get(e.attribute("Name").getText().toLowerCase()); final byte[] decode = base64.decode(e.getText()); log.log(Level.FINE, "piping " + decode.length + " bytes from " + e.attribute("Name").getText().toLowerCase()); stream.write(decode); } XPath done = DocumentHelper.createXPath("//*[@State='http://schemas.microsoft.com/wbem/wsman/1/windows/shell/CommandState/Done']"); done.setNamespaceContext(namespaceContext); if (Iterables.isEmpty(done.selectNodes(response))) { log.log(Level.FINE, "keep going baby!"); return true; } else { exitCode = Integer.parseInt(first(response, "//" + Namespaces.NS_WIN_SHELL.getPrefix() + ":ExitCode")); log.log(Level.FINE, "no more output - command is now done - exit code: " + exitCode); } return false; }
public boolean slurpOutput(PipedOutputStream stdout, PipedOutputStream stderr) throws IOException { log.log(Level.FINE, "--> SlurpOutput"); ImmutableMap<String, PipedOutputStream> streams = ImmutableMap.of("stdout", stdout, "stderr", stderr); Document request = factory.newGetOutputRequest(shellId, commandId).build(); Document response = sendRequest(request); XPath xpath = DocumentHelper.createXPath("//" + Namespaces.NS_WIN_SHELL.getPrefix() + ":Stream"); namespaceContext = new SimpleNamespaceContext(); namespaceContext.addNamespace(Namespaces.NS_WIN_SHELL.getPrefix(), Namespaces.NS_WIN_SHELL.getURI()); xpath.setNamespaceContext(namespaceContext); Base64 base64 = new Base64(); for (Element e : (List<Element>) xpath.selectNodes(response)) { PipedOutputStream stream = streams.get(e.attribute("Name").getText().toLowerCase()); final byte[] decode = base64.decode(e.getText()); log.log(Level.FINE, "piping " + decode.length + " bytes from " + e.attribute("Name").getText().toLowerCase()); stream.write(decode); } XPath done = DocumentHelper .createXPath("//*[@State='http://schemas.microsoft.com/wbem/wsman/1/windows/shell/CommandState/Done']"); done.setNamespaceContext(namespaceContext); if (Iterables.isEmpty(done.selectNodes(response))) { log.log(Level.FINE, "keep going baby!"); return true; } else { exitCode = Integer.parseInt(first(response, "//" + Namespaces.NS_WIN_SHELL.getPrefix() + ":ExitCode")); log.log(Level.FINE, "no more output - command is now done - exit code: " + exitCode); } return false; }
public static Element getDefaultRegistrationElement(String username, String profile) throws RegistrationNotFoundException { if ((username == null) || !StringUtil.validString(profile)) { throw new RegistrationNotFoundException("Username and profile must be non-null and non-empty for user " + username + " with profile " + profile); } String errMsg = "No profile found for user " + username + " with profile " + profile; Element elem = null; Element foundElement = getGtxProfile(username).getRootElement(); String findString = "//gtx:profiles/gtx:profile[@id='" + profile + "']/gtx:systems/gtx:system"; XPath xpath = DocumentHelper.createXPath(findString); xpath.setNamespaceContext(new SimpleNamespaceContext(XmlUtil.getGtxNamespaceMap())); Node node = xpath.selectSingleNode(foundElement); if (node != null) { elem = (Element) node; logger.debug("OFPrivateDataDAO: getDefaultRegistrationElement(): " + username + " element: " + node.asXML()); } else { logger.debug("OFPrivateDataDAO: getDefaultRegistrationElement(): " + username + " no element found"); } if (elem == null) { throw new RegistrationNotFoundException(errMsg); } return elem; }
HashMap<String, String> map = XmlUtil.getGtxNamespaceMap(); XPath xpath = DocumentHelper.createXPath("//gtx:system"); xpath.setNamespaceContext(new SimpleNamespaceContext(map)); Node systemNode = xpath.selectSingleNode(element); String system = null; xpath.setNamespaceContext(new SimpleNamespaceContext(map)); xpath.setNamespaceContext(new SimpleNamespaceContext(map)); Node id = xpath.selectSingleNode(call);
xpath.setNamespaceContext(new SimpleNamespaceContext(XmlUtil.getGtxNamespaceMap()));
xpath.setNamespaceContext(new SimpleNamespaceContext(XmlUtil.getGtxNamespaceMap()));
xpath.setNamespaceContext(new SimpleNamespaceContext(XmlUtil.getGtxNamespaceMap()));
protected void testXPath(String xpathText) { XPath xpath = DocumentHelper.createXPath(xpathText); SimpleNamespaceContext context = new SimpleNamespaceContext(); context.addNamespace("xplt", "www.xxxx.com"); context.addNamespace("xpl", "www.xxxx.com"); xpath.setNamespaceContext(context); List list = xpath.selectNodes(document); log("Searched path: " + xpathText + " found: " + list.size() + " result(s)"); assertTrue("Should have found at lest one result", list.size() > 0); }
/** * Given a GTMS system element <system id="asterisk">...</system> returns a collection of dial * prefixes. * * @param element GTMS system element. * @return collection of found dial prefixes or empty collection if nothing found. */ public static DialPrefixCollection getDialPrefixesFromElement(Element element) { DialPrefixCollection prefixes = new DialPrefixCollection(); Node node = (Node) element; if (node != null) { String findString = "//gtx:prefix"; XPath xpath = DocumentHelper.createXPath(findString); xpath.setNamespaceContext(new SimpleNamespaceContext(XmlUtil.getGtxNamespaceMap())); List<Node> prefixNodes = xpath.selectNodes(node); for (Node tmpNode : prefixNodes) { Element tmpElem = (Element) tmpNode; Attribute attrib = tmpElem.attribute("id"); String id = attrib.getStringValue(); String regex = tmpElem.getStringValue(); prefixes.add(new DialPrefixRule(id, regex)); } } return prefixes; }
/** * Given a GTMS system element <system id="asterisk">...</system> returns a collection of * trunks. * * @param element GTMS system element. * @return collection of found trunks or empty collection if nothing found. */ public static TrunkCollection getTrunksFromElement(Element element) { TrunkCollection trunks = new TrunkCollection(); Node node = (Node) element; if (node != null) { String findString = "//gtx:trunk"; XPath xpath = DocumentHelper.createXPath(findString); xpath.setNamespaceContext(new SimpleNamespaceContext(XmlUtil.getGtxNamespaceMap())); List<Node> trunkNodes = xpath.selectNodes(node); for (Node tmpNode : trunkNodes) { Element tmpElem = (Element) tmpNode; Attribute attrib = tmpElem.attribute("id"); String id = attrib.getStringValue(); String regex = tmpElem.getStringValue(); trunks.add(new Trunk(id, regex)); } } return trunks; }
/** * Given a gtx-profile element <gtx-profile>...</gtx-profile> returns a collection of trunks. * * @param element GTMS system element. * @param profile profile to look for. * @param systemType system to look for. * @return collection of found trunks or empty collection if nothing found. */ public static TrunkCollection getTrunksFromElement(Element element, String profile, GtmsSystemType systemType) { TrunkCollection trunks = new TrunkCollection(); Node node = (Node) element; if (node != null) { String findString = "//gtx:profiles//gtx:profile//gtx:systems//gtx:system[@id='" + systemType.name() + "']//gtx:trunk"; XPath xpath = DocumentHelper.createXPath(findString); xpath.setNamespaceContext(new SimpleNamespaceContext(XmlUtil.getGtxNamespaceMap())); List<Node> trunkNodes = xpath.selectNodes(node); for (Node tmpNode : trunkNodes) { Element tmpElem = (Element) tmpNode; Attribute attrib = tmpElem.attribute("id"); String id = attrib.getStringValue(); String regex = tmpElem.getStringValue(); trunks.add(new Trunk(id, regex)); } } return trunks; }
+ "']//gtx:prefix"; XPath xpath = DocumentHelper.createXPath(findString); xpath.setNamespaceContext(new SimpleNamespaceContext(XmlUtil.getGtxNamespaceMap()));
String findString = "//gtx:profiles//gtx:profile//gtx:systems//gtx:system[@id='" + systemType.name() + "']//gtx:prefix"; XPath xpath = DocumentHelper.createXPath(findString); xpath.setNamespaceContext(new SimpleNamespaceContext(XmlUtil.getGtxNamespaceMap()));
/** * Given username, profile and system returns a collection of dial prefixes. * * @param username Username * @param profile Profile * @param system System * @return collection of dial prefixes. * @throws RegistrationNotFoundException */ public static DialPrefixCollection getDialPrefixes(String username, String profile, GtmsSystemType systemType) throws RegistrationNotFoundException { DialPrefixCollection prefixes = new DialPrefixCollection(); Node node = getGtmsSystemRegistrationElement(username, profile, systemType); if (node != null /* && ( node.getNodeTypeName().equals("Element") ) */) { String findString = "//gtx:profiles//gtx:profile//gtx:systems//gtx:system[@id='" + systemType.name() + "']//gtx:prefix"; XPath xpath = DocumentHelper.createXPath(findString); xpath.setNamespaceContext(new SimpleNamespaceContext(XmlUtil.getGtxNamespaceMap())); List<Node> prefixNodes = xpath.selectNodes(node); for (Node tmpNode : prefixNodes) { Element tmpElem = (Element) tmpNode; Attribute attrib = tmpElem.attribute("id"); String id = attrib.getStringValue(); String regex = tmpElem.getStringValue(); prefixes.add(new DialPrefixRule(id, regex)); } } prefixes.printContents(); return prefixes; }