Search in sources :

Example 1 with XPath

use of org.jdom.xpath.XPath in project intellij-community by JetBrains.

the class FogBugzRepository method getCases.

@SuppressWarnings("unchecked")
private Task[] getCases(String q) throws Exception {
    loginIfNeeded();
    PostMethod method = new PostMethod(getUrl() + "/api.asp");
    method.addParameter("token", myToken);
    method.addParameter("cmd", "search");
    method.addParameter("q", q);
    method.addParameter("cols", "sTitle,fOpen,dtOpened,dtLastUpdated,ixCategory");
    int status = getHttpClient().executeMethod(method);
    if (status != 200) {
        throw new Exception("Error listing cases: " + method.getStatusLine());
    }
    Document document = new SAXBuilder(false).build(method.getResponseBodyAsStream()).getDocument();
    List<Element> errorNodes = XPath.newInstance("/response/error").selectNodes(document);
    if (!errorNodes.isEmpty()) {
        throw new Exception("Error listing cases: " + errorNodes.get(0).getText());
    }
    final XPath commentPath = XPath.newInstance("events/event");
    final List<Element> nodes = (List<Element>) XPath.newInstance("/response/cases/case").selectNodes(document);
    final List<Task> tasks = ContainerUtil.mapNotNull(nodes, (NotNullFunction<Element, Task>) element -> createCase(element, commentPath));
    return tasks.toArray(new Task[tasks.size()]);
}
Also used : XPath(org.jdom.xpath.XPath) SAXBuilder(org.jdom.input.SAXBuilder) TasksIcons(icons.TasksIcons) DatatypeConfigurationException(javax.xml.datatype.DatatypeConfigurationException) Tag(com.intellij.util.xmlb.annotations.Tag) StringUtil(com.intellij.openapi.util.text.StringUtil) Date(java.util.Date) NotNullFunction(com.intellij.util.NotNullFunction) BaseRepository(com.intellij.tasks.impl.BaseRepository) PostMethod(org.apache.commons.httpclient.methods.PostMethod) DatatypeFactory(javax.xml.datatype.DatatypeFactory) ContainerUtil(com.intellij.util.containers.ContainerUtil) BaseRepositoryImpl(com.intellij.tasks.impl.BaseRepositoryImpl) XPath(org.jdom.xpath.XPath) Nullable(org.jetbrains.annotations.Nullable) Document(org.jdom.Document) List(java.util.List) Comparing(com.intellij.openapi.util.Comparing) com.intellij.tasks(com.intellij.tasks) PasswordUtil(com.intellij.openapi.util.PasswordUtil) Logger(com.intellij.openapi.diagnostic.Logger) NotNull(org.jetbrains.annotations.NotNull) Element(org.jdom.Element) javax.swing(javax.swing) SAXBuilder(org.jdom.input.SAXBuilder) PostMethod(org.apache.commons.httpclient.methods.PostMethod) Element(org.jdom.Element) List(java.util.List) Document(org.jdom.Document) DatatypeConfigurationException(javax.xml.datatype.DatatypeConfigurationException)

Example 2 with XPath

use of org.jdom.xpath.XPath in project intellij-community by JetBrains.

the class FogBugzRepository method login.

private void login(@NotNull PostMethod method) throws Exception {
    LOG.debug("Requesting new token");
    int status = getHttpClient().executeMethod(method);
    if (status != 200) {
        throw new Exception("Error logging in: " + method.getStatusLine());
    }
    Document document = new SAXBuilder(false).build(method.getResponseBodyAsStream()).getDocument();
    XPath path = XPath.newInstance("/response/token");
    Element result = (Element) path.selectSingleNode(document);
    if (result == null) {
        Element error = (Element) XPath.newInstance("/response/error").selectSingleNode(document);
        throw new Exception(error == null ? "Error logging in" : error.getText());
    }
    myToken = result.getTextTrim();
}
Also used : XPath(org.jdom.xpath.XPath) SAXBuilder(org.jdom.input.SAXBuilder) Element(org.jdom.Element) Document(org.jdom.Document) DatatypeConfigurationException(javax.xml.datatype.DatatypeConfigurationException)

Example 3 with XPath

use of org.jdom.xpath.XPath in project intellij-community by JetBrains.

the class XPathResponseHandler method lazyCompile.

@NotNull
private XPath lazyCompile(@NotNull String path) throws Exception {
    XPath xPath = myCompiledCache.get(path);
    if (xPath == null) {
        try {
            xPath = XPath.newInstance(path);
            myCompiledCache.put(path, xPath);
        } catch (JDOMException e) {
            throw new Exception(String.format("Malformed XPath expression '%s'", path));
        }
    }
    return xPath;
}
Also used : XPath(org.jdom.xpath.XPath) JDOMException(org.jdom.JDOMException) JDOMException(org.jdom.JDOMException) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with XPath

use of org.jdom.xpath.XPath in project intellij-community by JetBrains.

the class XPathResponseHandler method selectTasksList.

@NotNull
@Override
protected List<Object> selectTasksList(@NotNull String response, int max) throws Exception {
    Document document = new SAXBuilder(false).build(new StringReader(response));
    Element root = document.getRootElement();
    XPath xPath = lazyCompile(getSelector(TASKS).getPath());
    @SuppressWarnings("unchecked") List<Object> rawTaskElements = xPath.selectNodes(root);
    if (!rawTaskElements.isEmpty() && !(rawTaskElements.get(0) instanceof Element)) {
        throw new Exception(String.format("Expression '%s' should match list of XML elements. Got '%s' instead.", xPath.getXPath(), rawTaskElements.toString()));
    }
    return rawTaskElements.subList(0, Math.min(rawTaskElements.size(), max));
}
Also used : XPath(org.jdom.xpath.XPath) SAXBuilder(org.jdom.input.SAXBuilder) Element(org.jdom.Element) StringReader(java.io.StringReader) Document(org.jdom.Document) JDOMException(org.jdom.JDOMException) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with XPath

use of org.jdom.xpath.XPath in project intellij-community by JetBrains.

the class XsltDocumentationProvider method getUrlFor.

@Nullable
public List<String> getUrlFor(PsiElement psiElement, PsiElement psiElement1) {
    if (psiElement instanceof XsltElement)
        return null;
    final String category;
    final String name;
    final String tagName = getTagName(psiElement);
    if (tagName != null) {
        name = tagName;
        category = "element";
    } else if (psiElement instanceof XPathFunction) {
        name = ((XPathFunction) psiElement).getName();
        category = "function";
    } else if (psiElement instanceof DocElement) {
        name = ((DocElement) psiElement).getName();
        category = ((DocElement) psiElement).getCategory();
    } else {
        return null;
    }
    try {
        final Document document = getDocumentationDocument();
        final XPath xPath = XPath.newInstance("//x:" + category + "[@name = '" + name + "']");
        xPath.addNamespace("x", document.getRootElement().getNamespaceURI());
        final Element e = (Element) xPath.selectSingleNode(document);
        if (e != null) {
            return Collections.singletonList(e.getParentElement().getAttributeValue("base") + e.getAttributeValue("href"));
        }
    } catch (Exception e) {
        Logger.getInstance(getClass().getName()).error(e);
    }
    return null;
}
Also used : XPath(org.jdom.xpath.XPath) XPathFunction(org.intellij.lang.xpath.psi.XPathFunction) XsltElement(org.intellij.lang.xpath.xslt.psi.XsltElement) XsltElement(org.intellij.lang.xpath.xslt.psi.XsltElement) LightElement(com.intellij.psi.impl.light.LightElement) Element(org.jdom.Element) Document(org.jdom.Document) JDOMException(org.jdom.JDOMException) IncorrectOperationException(com.intellij.util.IncorrectOperationException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) IOException(java.io.IOException) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

XPath (org.jdom.xpath.XPath)18 Document (org.jdom.Document)12 Element (org.jdom.Element)12 SAXBuilder (org.jdom.input.SAXBuilder)11 JDOMException (org.jdom.JDOMException)7 File (java.io.File)4 IOException (java.io.IOException)4 Xpp3Dom (org.codehaus.plexus.util.xml.Xpp3Dom)4 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 NotNull (org.jetbrains.annotations.NotNull)3 Nullable (org.jetbrains.annotations.Nullable)3 DatatypeConfigurationException (javax.xml.datatype.DatatypeConfigurationException)2 Logger (com.intellij.openapi.diagnostic.Logger)1 Comparing (com.intellij.openapi.util.Comparing)1 PasswordUtil (com.intellij.openapi.util.PasswordUtil)1 StringUtil (com.intellij.openapi.util.text.StringUtil)1 VfsUtil.findFileByIoFile (com.intellij.openapi.vfs.VfsUtil.findFileByIoFile)1 LightElement (com.intellij.psi.impl.light.LightElement)1 com.intellij.tasks (com.intellij.tasks)1 BaseRepository (com.intellij.tasks.impl.BaseRepository)1