1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.query.xwql.internal

File QueryAnalyzer.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

12
30
8
1
122
83
18
0.6
3.75
8
2.25

Classes

Class Line # Actions
QueryAnalyzer 35 30 0% 18 4
0.9292%
 

Contributing tests

This file is covered by 12 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.query.xwql.internal;
21   
22    import org.apache.commons.lang3.StringUtils;
23    import org.xwiki.query.jpql.analysis.DepthFirstAdapter;
24    import org.xwiki.query.jpql.node.AAbstractSchemaName;
25    import org.xwiki.query.jpql.node.APath;
26    import org.xwiki.query.jpql.node.ARangeVariableDeclaration;
27    import org.xwiki.query.jpql.node.ASelectStatement;
28    import org.xwiki.query.jpql.node.AXObjectDecl;
29    import org.xwiki.query.jpql.node.AXPath;
30   
31    /**
32    * @version $Id: 9a36ab404c01c3c177fbbac562996da7c165b429 $
33    * @since 2.4M2
34    */
 
35    public class QueryAnalyzer extends DepthFirstAdapter
36    {
 
37  1032 toggle public QueryAnalyzer(QueryContext context)
38    {
39  1032 this.context = context;
40    }
41   
42    QueryContext context;
43   
 
44  1032 toggle @Override
45    public void outASelectStatement(ASelectStatement node)
46    {
47    // needed for update information after FromClause
48  1032 node.getSelectClause().apply(this);
49  1032 super.outASelectStatement(node);
50    }
51   
52    String documentFromName = "Document";
53   
 
54  1077 toggle @Override
55    public void caseAAbstractSchemaName(AAbstractSchemaName node)
56    {
57  1077 String from = node.toString().trim();
58  1077 String alias = ((ARangeVariableDeclaration) node.parent()).getVariable().toString().trim();
59  1077 if (documentFromName.equals(from)) {
60  419 context.addDocument(alias);
61    }
62    }
63   
 
64  397 toggle @Override
65    public void caseAXObjectDecl(AXObjectDecl node)
66    {
67  397 String path[] = splitPath(node.getId().toString());
68  397 if (path.length != 2) {
69  0 throw new InvalidQueryException("docalias.object('classname') expected, but got:" + node.toString());
70    }
71  397 String docalias = path[0];
72  397 String method = path[1];
73  397 String className = unquote(node.getXClassName().toString().trim());
74   
75  397 String alias = node.parent().parent() instanceof ARangeVariableDeclaration
76    // used in from clause
77    ? ((ARangeVariableDeclaration) node.parent().parent()).getVariable().toString().trim()
78    // used in where clause. unnamed.
79    : null;
80   
81  397 if ("object".equals(method)) {
82  397 context.addObject(docalias, className, alias, node);
83    } else {
84  0 throw new InvalidQueryException("document's method + [" + method + "] is unsupported");
85    }
86    }
87   
 
88  5135 toggle @Override
89    public void caseAPath(APath node)
90    {
91  5135 String path[] = splitPath(node.toString());
92  5135 QueryContext.ObjectInfo obj = context.getObject(path[0]);
93  5135 if (path.length >= 2 && obj != null) {
94  169 obj.addProperty(path[1], node);
95    }
96    }
97   
 
98  3 toggle @Override
99    public void outAXPath(AXPath node)
100    {
101  3 QueryContext.ObjectInfo obj = context.getObject(node.getXObjectDecl());
102  3 String path[] = splitPath(node.getProperty().toString());
103  3 obj.addProperty(path[0], node);
104  3 super.outAXPath(node);
105    }
106   
 
107  5535 toggle public static String[] splitPath(String str)
108    {
109  5535 return StringUtils.split(str.trim(), ".");
110    }
111   
 
112  397 toggle public static String unquote(String str)
113    {
114  397 str = str.trim();
115  397 if (str.startsWith("'") && str.endsWith("'")
116    || str.startsWith("\"") && str.endsWith("\""))
117    {
118  9 str = str.substring(1, str.length() - 1);
119    }
120  397 return str;
121    }
122    }