]> git.argeo.org Git - lgpl/argeo-commons.git/blob - sandbox/runtime/org.argeo.sandbox.jackrabbit/src/main/java/jcr/CommandLineQuery.java
[maven-release-plugin] prepare release argeo-commons-0.3.2
[lgpl/argeo-commons.git] / sandbox / runtime / org.argeo.sandbox.jackrabbit / src / main / java / jcr / CommandLineQuery.java
1 /*
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17 package jcr;
18
19 import javax.jcr.*;
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;
27
28 public class CommandLineQuery extends Base {
29 public CommandLineQuery() {
30 }
31
32 public static void main(String[] args) throws IOException, RepositoryException {
33 CommandLineQuery clq=new CommandLineQuery();
34 clq.run();
35 }
36
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));
43 for(;;) {
44 System.out.print("JCRQL> ");
45 String queryString=reader.readLine();
46 if(queryString.equals("quit")) {
47 break;
48 }
49 if(queryString.length()==0 || queryString.startsWith("#")) {
50 continue;
51 }
52
53 int resultCounter=0;
54 try {
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();
60 dump(node);
61 resultCounter++;
62 }
63 } catch(Exception e) {
64 e.printStackTrace();
65 }
66
67 System.out.println("result count: "+resultCounter);
68 }
69 logout(session);
70 }
71
72 private void dump(Node node) throws RepositoryException {
73 StringBuilder sb=new StringBuilder();
74 String sep=",";
75 sb.append(node.getName());
76 sb.append("["+node.getPath());
77 PropertyIterator propIterator=node.getProperties();
78 while(propIterator.hasNext()) {
79 Property prop=propIterator.nextProperty();
80 sb.append(sep);
81 sb.append("@"+prop.getName()+"=\""+prop.getString()+"\"");
82 }
83 sb.append("]");
84 System.out.println(sb.toString());
85 }
86 }