Search in sources :

Example 1 with ParseException

use of net.sourceforge.pmd.lang.ast.ParseException in project pmd by pmd.

the class ViewerModel method evaluateXPathExpression.

/**
 * Evaluates the given XPath expression against the current tree.
 *
 * @param xPath
 *            XPath expression to be evaluated
 * @param evaluator
 *            object which requests the evaluation
 */
public void evaluateXPathExpression(String xPath, Object evaluator) throws ParseException, JaxenException {
    try {
        if (LOGGER.isLoggable(Level.FINEST)) {
            LOGGER.finest("xPath=" + xPath);
            LOGGER.finest("evaluator=" + evaluator);
        }
        XPath xpath = new BaseXPath(xPath, new DocumentNavigator());
        if (LOGGER.isLoggable(Level.FINEST)) {
            LOGGER.finest("xpath=" + xpath);
            LOGGER.finest("rootNode=" + rootNode);
        }
        try {
            evaluationResults = xpath.selectNodes(rootNode);
        } catch (Exception e) {
            LOGGER.finest("selectNodes problem:");
            e.printStackTrace(System.err);
        }
        if (LOGGER.isLoggable(Level.FINEST)) {
            LOGGER.finest("evaluationResults=" + evaluationResults);
        }
        fireViewerModelEvent(new ViewerModelEvent(evaluator, ViewerModelEvent.PATH_EXPRESSION_EVALUATED));
    } catch (JaxenException je) {
        je.printStackTrace(System.err);
        throw je;
    }
}
Also used : BaseXPath(org.jaxen.BaseXPath) XPath(org.jaxen.XPath) BaseXPath(org.jaxen.BaseXPath) JaxenException(org.jaxen.JaxenException) DocumentNavigator(net.sourceforge.pmd.lang.ast.xpath.DocumentNavigator) JaxenException(org.jaxen.JaxenException) ParseException(net.sourceforge.pmd.lang.ast.ParseException)

Example 2 with ParseException

use of net.sourceforge.pmd.lang.ast.ParseException in project pmd by pmd.

the class EcmascriptParser method parse.

public EcmascriptNode<AstRoot> parse(final Reader reader) {
    try {
        final List<ParseProblem> parseProblems = new ArrayList<>();
        final String sourceCode = IOUtils.toString(reader);
        final AstRoot astRoot = parseEcmascript(sourceCode, parseProblems);
        final EcmascriptTreeBuilder treeBuilder = new EcmascriptTreeBuilder(sourceCode, parseProblems);
        EcmascriptNode<AstRoot> tree = treeBuilder.build(astRoot);
        suppressMap = new HashMap<>();
        if (astRoot.getComments() != null) {
            for (Comment comment : astRoot.getComments()) {
                int nopmd = comment.getValue().indexOf(suppressMarker);
                if (nopmd > -1) {
                    String suppression = comment.getValue().substring(nopmd + suppressMarker.length());
                    EcmascriptNode<Comment> node = treeBuilder.build(comment);
                    suppressMap.put(node.getBeginLine(), suppression);
                }
            }
        }
        return tree;
    } catch (IOException e) {
        throw new ParseException(e);
    }
}
Also used : Comment(org.mozilla.javascript.ast.Comment) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ParseProblem(org.mozilla.javascript.ast.ParseProblem) ParseException(net.sourceforge.pmd.lang.ast.ParseException) AstRoot(org.mozilla.javascript.ast.AstRoot)

Example 3 with ParseException

use of net.sourceforge.pmd.lang.ast.ParseException in project pmd by pmd.

the class ApexParser method parse.

public ApexNode<Compilation> parse(final Reader reader) {
    try {
        final String sourceCode = IOUtils.toString(reader);
        final Compilation astRoot = parseApex(sourceCode);
        final ApexTreeBuilder treeBuilder = new ApexTreeBuilder(sourceCode);
        suppressMap = new HashMap<>();
        if (astRoot == null) {
            throw new ParseException("Couldn't parse the source - there is not root node - Syntax Error??");
        }
        return treeBuilder.build(astRoot);
    } catch (IOException e) {
        throw new ParseException(e);
    }
}
Also used : Compilation(apex.jorje.semantic.ast.compilation.Compilation) ParseException(net.sourceforge.pmd.lang.ast.ParseException) IOException(java.io.IOException)

Example 4 with ParseException

use of net.sourceforge.pmd.lang.ast.ParseException in project pmd by pmd.

the class XmlParser method parseDocument.

protected Document parseDocument(Reader reader) throws ParseException {
    nodeCache.clear();
    try {
        String xmlData = IOUtils.toString(reader);
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(parserOptions.isNamespaceAware());
        dbf.setValidating(parserOptions.isValidating());
        dbf.setIgnoringComments(parserOptions.isIgnoringComments());
        dbf.setIgnoringElementContentWhitespace(parserOptions.isIgnoringElementContentWhitespace());
        dbf.setExpandEntityReferences(parserOptions.isExpandEntityReferences());
        dbf.setCoalescing(parserOptions.isCoalescing());
        dbf.setXIncludeAware(parserOptions.isXincludeAware());
        dbf.setFeature("http://xml.org/sax/features/external-general-entities", false);
        dbf.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
        DocumentBuilder documentBuilder = dbf.newDocumentBuilder();
        documentBuilder.setEntityResolver(parserOptions.getEntityResolver());
        Document document = documentBuilder.parse(new InputSource(new StringReader(xmlData)));
        DOMLineNumbers lineNumbers = new DOMLineNumbers(document, xmlData);
        lineNumbers.determine();
        return document;
    } catch (ParserConfigurationException | SAXException | IOException e) {
        throw new ParseException(e);
    }
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) StringReader(java.io.StringReader) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IOException(java.io.IOException) ParseException(net.sourceforge.pmd.lang.ast.ParseException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException)

Aggregations

ParseException (net.sourceforge.pmd.lang.ast.ParseException)4 IOException (java.io.IOException)3 Compilation (apex.jorje.semantic.ast.compilation.Compilation)1 StringReader (java.io.StringReader)1 ArrayList (java.util.ArrayList)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 DocumentNavigator (net.sourceforge.pmd.lang.ast.xpath.DocumentNavigator)1 BaseXPath (org.jaxen.BaseXPath)1 JaxenException (org.jaxen.JaxenException)1 XPath (org.jaxen.XPath)1 AstRoot (org.mozilla.javascript.ast.AstRoot)1 Comment (org.mozilla.javascript.ast.Comment)1 ParseProblem (org.mozilla.javascript.ast.ParseProblem)1 Document (org.w3c.dom.Document)1 InputSource (org.xml.sax.InputSource)1 SAXException (org.xml.sax.SAXException)1