2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
20 import javax
.jcr
.nodetype
.PropertyDefinition
;
21 import javax
.jcr
.query
.QueryManager
;
22 import javax
.jcr
.query
.Query
;
23 import javax
.jcr
.query
.QueryResult
;
24 import java
.io
.IOException
;
25 import java
.io
.BufferedReader
;
26 import java
.io
.InputStreamReader
;
28 public class CommandLineQuery
extends Base
{
29 public CommandLineQuery() {
32 public static void main(String
[] args
) throws IOException
, RepositoryException
{
33 CommandLineQuery clq
=new CommandLineQuery();
37 private void run() throws IOException
, RepositoryException
{
38 Repository repository
=getRepository();
39 Session session
=getReadonlySession(repository
);
40 Workspace workspace
=session
.getWorkspace();
41 QueryManager qm
=workspace
.getQueryManager();
42 BufferedReader reader
=new BufferedReader(new InputStreamReader(System
.in
));
44 System
.out
.print("JCRQL> ");
45 String queryString
=reader
.readLine();
46 if(queryString
.equals("quit")) {
49 if(queryString
.length()==0 || queryString
.startsWith("#")) {
55 Query query
=qm
.createQuery(queryString
, Query
.XPATH
);
56 QueryResult queryResult
=query
.execute();
57 NodeIterator nodeIterator
=queryResult
.getNodes();
58 while(nodeIterator
.hasNext()) {
59 Node node
=nodeIterator
.nextNode();
63 } catch(Exception e
) {
67 System
.out
.println("result count: "+resultCounter
);
72 private void dump(Node node
) throws RepositoryException
{
73 StringBuilder sb
=new StringBuilder();
75 sb
.append(node
.getName());
76 sb
.append("["+node
.getPath());
77 PropertyIterator propIterator
=node
.getProperties();
78 while(propIterator
.hasNext()) {
79 Property prop
=propIterator
.nextProperty();
81 sb
.append("@"+prop
.getName()+"=\""+prop
.getString()+"\"");
84 System
.out
.println(sb
.toString());