summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSteve Pitschke2012-07-02 13:04:54 (EDT)
committer Steve Pitschke2012-07-02 13:04:54 (EDT)
commit53f227d168ec3f0af1ca31f043f3e77933094e18 (patch)
treef6d4a6df1480ce94469dfc2ba1f12c1254b6c089
parent3ec522421e10d9c54d93b659fddca956e55bfabd (diff)
downloadorg.eclipse.lyo.core-53f227d168ec3f0af1ca31f043f3e77933094e18.zip
org.eclipse.lyo.core-53f227d168ec3f0af1ca31f043f3e77933094e18.tar.gz
org.eclipse.lyo.core-53f227d168ec3f0af1ca31f043f3e77933094e18.tar.bz2
Bug 383469: Add APIs for oslc.searchTerms and oslc.orderByrefs/changes/84/6584/1
Change-Id: I7bb1a71b6809338ca035ec6373079dad735245f9 Signed-off-by: Steve Pitschke <pitschke@us.ibm.com>
-rw-r--r--org.eclipse.lyo.core.query/src/main/antlr3/org/eclipse/lyo/core/query/OslcOrderBy.g181
-rw-r--r--org.eclipse.lyo.core.query/src/main/antlr3/org/eclipse/lyo/core/query/OslcSearchTerms.g85
-rw-r--r--org.eclipse.lyo.core.query/src/main/java/org/eclipse/lyo/core/query/OrderByClause.java23
-rw-r--r--org.eclipse.lyo.core.query/src/main/java/org/eclipse/lyo/core/query/PropertiesClause.java25
-rw-r--r--org.eclipse.lyo.core.query/src/main/java/org/eclipse/lyo/core/query/ScopedSortTerm.java24
-rw-r--r--org.eclipse.lyo.core.query/src/main/java/org/eclipse/lyo/core/query/SearchTermsClause.java25
-rw-r--r--org.eclipse.lyo.core.query/src/main/java/org/eclipse/lyo/core/query/SimpleSortTerm.java24
-rw-r--r--org.eclipse.lyo.core.query/src/main/java/org/eclipse/lyo/core/query/SortTerm.java32
-rw-r--r--org.eclipse.lyo.core.query/src/main/java/org/eclipse/lyo/core/query/SortTerms.java26
-rw-r--r--org.eclipse.lyo.core.query/src/main/java/org/eclipse/lyo/core/query/impl/ScopedSortTermInvocationHandler.java84
-rw-r--r--org.eclipse.lyo.core.query/src/main/java/org/eclipse/lyo/core/query/impl/SimpleSortTermInvocationHandler.java73
-rw-r--r--org.eclipse.lyo.core.query/src/main/java/org/eclipse/lyo/core/query/impl/SortTermInvocationHandler.java85
-rw-r--r--org.eclipse.lyo.core.query/src/main/java/org/eclipse/lyo/core/query/impl/SortTermsInvocationHandler.java118
-rw-r--r--org.eclipse.lyo.core.query/src/test/java/org/eclipse/lyo/core/query/test/BasicOrderByTest.java74
-rw-r--r--org.eclipse.lyo.core.query/src/test/java/org/eclipse/lyo/core/query/test/BasicSearchTermsTest.java110
15 files changed, 989 insertions, 0 deletions
diff --git a/org.eclipse.lyo.core.query/src/main/antlr3/org/eclipse/lyo/core/query/OslcOrderBy.g b/org.eclipse.lyo.core.query/src/main/antlr3/org/eclipse/lyo/core/query/OslcOrderBy.g
new file mode 100644
index 0000000..9c31214
--- /dev/null
+++ b/org.eclipse.lyo.core.query/src/main/antlr3/org/eclipse/lyo/core/query/OslcOrderBy.g
@@ -0,0 +1,181 @@
+/******************************************************************************
+ * Copyright (c) 2012 IBM Corporation.
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
+ *
+ * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
+ * and the Eclipse Distribution License is available at
+ * http://www.eclipse.org/org/documents/edl-v10.php.
+ *
+ * Contributors:
+ *
+ * Steve Pitchke - initial API and implementation
+ *******************************************************************************/
+grammar OslcOrderBy;
+
+options {
+ output=AST;
+ ASTLabelType=CommonTree;
+}
+
+tokens {
+ TERMS = 'terms';
+ SCOPED_TERM = 'scoped_term';
+ SIMPLE_TERM = 'simple_term';
+}
+
+@header {
+package org.eclipse.lyo.core.query;
+}
+
+@lexer::header
+{
+package org.eclipse.lyo.core.query;
+}
+
+@members {
+ private List<String> errors = new ArrayList<String>();
+ public void displayRecognitionError(String[] tokenNames,
+ RecognitionException e) {
+ String hdr = getErrorHeader(e);
+ String msg = getErrorMessage(e, tokenNames);
+ errors.add(hdr + " " + msg);
+ }
+ public List<String> getErrors() {
+ return errors;
+ }
+ public OslcOrderByParser(String where)
+ {
+ this(new CommonTokenStream(new OslcOrderByLexer(new ANTLRStringStream(where))));
+ }
+}
+
+
+oslc_order_by : sort_terms
+ ;
+
+sort_terms : sort_term ( ',' sort_term )* -> ^( 'terms' sort_term (sort_term)* )
+ ;
+
+sort_term : scoped_sort_term | DIRECTION identifier -> ^( 'simple_term' identifier DIRECTION )
+ ;
+
+scoped_sort_term : identifier '{' sort_terms '}' -> ^( 'scoped_term' identifier sort_terms )
+ ;
+
+identifier : prefixedName ;
+
+prefixedName
+ : PNAME_LN
+ | PNAME_NS
+ ;
+
+// $>
+
+// $<Lexer
+
+
+WS
+ : (' '| '\t'| EOL)+ { $channel=HIDDEN; }
+ ;
+
+PNAME_NS
+ : p=PN_PREFIX? ':'
+ ;
+
+PNAME_LN
+ : PNAME_NS PN_LOCAL
+ ;
+
+
+DIRECTION
+ : PLUS
+ | MINUS
+ ;
+
+fragment
+PN_CHARS_U
+ : PN_CHARS_BASE | '_'
+ ;
+
+fragment
+PN_CHARS
+ : PN_CHARS_U
+ | MINUS
+ | DIGIT
+ | '\u00B7'
+ | '\u0300'..'\u036F'
+ | '\u203F'..'\u2040'
+ ;
+
+fragment
+PN_PREFIX
+ : PN_CHARS_BASE ((PN_CHARS|DOT)* PN_CHARS)?
+ ;
+
+fragment
+PN_LOCAL
+ : ( PN_CHARS_U | DIGIT ) ((PN_CHARS|DOT)* PN_CHARS)?
+ ;
+
+fragment
+PN_CHARS_BASE
+ : 'A'..'Z'
+ | 'a'..'z'
+ | '\u00C0'..'\u00D6'
+ | '\u00D8'..'\u00F6'
+ | '\u00F8'..'\u02FF'
+ | '\u0370'..'\u037D'
+ | '\u037F'..'\u1FFF'
+ | '\u200C'..'\u200D'
+ | '\u2070'..'\u218F'
+ | '\u2C00'..'\u2FEF'
+ | '\u3001'..'\uD7FF'
+ | '\uF900'..'\uFDCF'
+ | '\uFDF0'..'\uFFFD'
+ ;
+
+fragment
+DIGIT
+ : '0'..'9'
+ ;
+
+fragment
+DOT
+ : '.'
+ ;
+
+fragment
+EOL
+ : '\n' | '\r'
+ ;
+
+OPEN_CURLY_BRACE
+ : '{'
+ ;
+
+CLOSE_CURLY_BRACE
+ : '}'
+ ;
+
+fragment
+PLUS
+ : '+'
+ ;
+
+fragment
+MINUS
+ : '-'
+ ;
+
+ASTERISK
+ : '*'
+ ;
+
+COMMA
+ : ','
+ ;
+
+// $>
diff --git a/org.eclipse.lyo.core.query/src/main/antlr3/org/eclipse/lyo/core/query/OslcSearchTerms.g b/org.eclipse.lyo.core.query/src/main/antlr3/org/eclipse/lyo/core/query/OslcSearchTerms.g
new file mode 100644
index 0000000..eb25d14
--- /dev/null
+++ b/org.eclipse.lyo.core.query/src/main/antlr3/org/eclipse/lyo/core/query/OslcSearchTerms.g
@@ -0,0 +1,85 @@
+/******************************************************************************
+ * Copyright (c) 2012 IBM Corporation.
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
+ *
+ * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
+ * and the Eclipse Distribution License is available at
+ * http://www.eclipse.org/org/documents/edl-v10.php.
+ *
+ * Contributors:
+ *
+ * Steve Pitchke - initial API and implementation
+ *******************************************************************************/
+grammar OslcSearchTerms;
+
+options {
+ output=AST;
+ ASTLabelType=CommonTree;
+}
+
+tokens {
+ STRING_LIST = 'string_list';
+}
+
+@header {
+package org.eclipse.lyo.core.query;
+}
+
+@lexer::header
+{
+package org.eclipse.lyo.core.query;
+}
+
+@members {
+ private List<String> errors = new ArrayList<String>();
+ public void displayRecognitionError(String[] tokenNames,
+ RecognitionException e) {
+ String hdr = getErrorHeader(e);
+ String msg = getErrorMessage(e, tokenNames);
+ errors.add(hdr + " " + msg);
+ }
+ public List<String> getErrors() {
+ return errors;
+ }
+ public OslcSearchTermsParser(String searchTerms)
+ {
+ this(new CommonTokenStream(new OslcSearchTermsLexer(new ANTLRStringStream(searchTerms))));
+ }
+}
+
+
+oslc_search_terms : string_esc ( ',' string_esc )* -> ^('string_list' string_esc (string_esc)* )
+ ;
+
+string_esc : STRING_LITERAL ;
+
+// $>
+
+// $<Lexer
+
+WS
+ : (' '| '\t'| EOL)+ { $channel=HIDDEN; }
+ ;
+
+fragment
+EOL
+ : '\n' | '\r'
+ ;
+
+STRING_LITERAL
+ : '"' ( options {greedy=false;} : ~('\u0022' | '\u005C' | '\u000A' | '\u000D') | ECHAR )* '"'
+ ;
+
+fragment
+ECHAR
+ : '\\' ('t' | 'b' | 'n' | 'r' | 'f' | '\\' | '"' | '\'')
+ ;
+
+COMMA
+ : ','
+ ;
+
+// $>
diff --git a/org.eclipse.lyo.core.query/src/main/java/org/eclipse/lyo/core/query/OrderByClause.java b/org.eclipse.lyo.core.query/src/main/java/org/eclipse/lyo/core/query/OrderByClause.java
new file mode 100644
index 0000000..e5b77b5
--- /dev/null
+++ b/org.eclipse.lyo.core.query/src/main/java/org/eclipse/lyo/core/query/OrderByClause.java
@@ -0,0 +1,23 @@
+/*******************************************************************************
+ * Copyright (c) 2012 IBM Corporation.
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
+ *
+ * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
+ * and the Eclipse Distribution License is available at
+ * http://www.eclipse.org/org/documents/edl-v10.php.
+ *
+ * Contributors:
+ *
+ * Steve Pitschke - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.lyo.core.query;
+
+/**
+ * Top-level {@link SortTerms} from oslc.orderBy clause
+ */
+public interface OrderByClause extends SortTerms
+{
+}
diff --git a/org.eclipse.lyo.core.query/src/main/java/org/eclipse/lyo/core/query/PropertiesClause.java b/org.eclipse.lyo.core.query/src/main/java/org/eclipse/lyo/core/query/PropertiesClause.java
new file mode 100644
index 0000000..19a11b8
--- /dev/null
+++ b/org.eclipse.lyo.core.query/src/main/java/org/eclipse/lyo/core/query/PropertiesClause.java
@@ -0,0 +1,25 @@
+/*******************************************************************************
+ * Copyright (c) 2012 IBM Corporation.
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
+ *
+ * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
+ * and the Eclipse Distribution License is available at
+ * http://www.eclipse.org/org/documents/edl-v10.php.
+ *
+ * Contributors:
+ *
+ * Steve Pitschke - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.lyo.core.query;
+
+import java.beans.PropertyEditor;
+
+/**
+ * Top-level properties from oslc.Properties clause
+ */
+public interface PropertiesClause extends Properties
+{
+}
diff --git a/org.eclipse.lyo.core.query/src/main/java/org/eclipse/lyo/core/query/ScopedSortTerm.java b/org.eclipse.lyo.core.query/src/main/java/org/eclipse/lyo/core/query/ScopedSortTerm.java
new file mode 100644
index 0000000..da6ab70
--- /dev/null
+++ b/org.eclipse.lyo.core.query/src/main/java/org/eclipse/lyo/core/query/ScopedSortTerm.java
@@ -0,0 +1,24 @@
+/*******************************************************************************
+ * Copyright (c) 2012 IBM Corporation.
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
+ *
+ * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
+ * and the Eclipse Distribution License is available at
+ * http://www.eclipse.org/org/documents/edl-v10.php.
+ *
+ * Contributors:
+ *
+ * Steve Pitschke - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.lyo.core.query;
+
+/**
+ * Scoped sort term from oslc.orderBy clause
+ */
+public interface ScopedSortTerm extends SortTerm
+{
+ SortTerms sortTerms();
+}
diff --git a/org.eclipse.lyo.core.query/src/main/java/org/eclipse/lyo/core/query/SearchTermsClause.java b/org.eclipse.lyo.core.query/src/main/java/org/eclipse/lyo/core/query/SearchTermsClause.java
new file mode 100644
index 0000000..e4c850e
--- /dev/null
+++ b/org.eclipse.lyo.core.query/src/main/java/org/eclipse/lyo/core/query/SearchTermsClause.java
@@ -0,0 +1,25 @@
+/*******************************************************************************
+ * Copyright (c) 2012 IBM Corporation.
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
+ *
+ * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
+ * and the Eclipse Distribution License is available at
+ * http://www.eclipse.org/org/documents/edl-v10.php.
+ *
+ * Contributors:
+ *
+ * Steve Pitschke - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.lyo.core.query;
+
+import java.util.List;
+
+/**
+ * String list from oslc.searchTerms clause
+ */
+public interface SearchTermsClause extends List<String>
+{
+}
diff --git a/org.eclipse.lyo.core.query/src/main/java/org/eclipse/lyo/core/query/SimpleSortTerm.java b/org.eclipse.lyo.core.query/src/main/java/org/eclipse/lyo/core/query/SimpleSortTerm.java
new file mode 100644
index 0000000..8429b02
--- /dev/null
+++ b/org.eclipse.lyo.core.query/src/main/java/org/eclipse/lyo/core/query/SimpleSortTerm.java
@@ -0,0 +1,24 @@
+/*******************************************************************************
+ * Copyright (c) 2012 IBM Corporation.
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
+ *
+ * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
+ * and the Eclipse Distribution License is available at
+ * http://www.eclipse.org/org/documents/edl-v10.php.
+ *
+ * Contributors:
+ *
+ * Steve Pitschke - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.lyo.core.query;
+
+/**
+ * Simple sort termf from oslc.orderBy clause
+ */
+public interface SimpleSortTerm extends SortTerm
+{
+ boolean ascending();
+}
diff --git a/org.eclipse.lyo.core.query/src/main/java/org/eclipse/lyo/core/query/SortTerm.java b/org.eclipse.lyo.core.query/src/main/java/org/eclipse/lyo/core/query/SortTerm.java
new file mode 100644
index 0000000..f699310
--- /dev/null
+++ b/org.eclipse.lyo.core.query/src/main/java/org/eclipse/lyo/core/query/SortTerm.java
@@ -0,0 +1,32 @@
+/*******************************************************************************
+ * Copyright (c) 2012 IBM Corporation.
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
+ *
+ * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
+ * and the Eclipse Distribution License is available at
+ * http://www.eclipse.org/org/documents/edl-v10.php.
+ *
+ * Contributors:
+ *
+ * Steve Pitschke - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.lyo.core.query;
+
+/**
+ * Sort term from oslc.orderBy clause
+ */
+public interface SortTerm
+{
+ enum Type
+ {
+ SIMPLE,
+ SCOPED
+ }
+
+ Type type();
+
+ PName identifier();
+}
diff --git a/org.eclipse.lyo.core.query/src/main/java/org/eclipse/lyo/core/query/SortTerms.java b/org.eclipse.lyo.core.query/src/main/java/org/eclipse/lyo/core/query/SortTerms.java
new file mode 100644
index 0000000..ebce5eb
--- /dev/null
+++ b/org.eclipse.lyo.core.query/src/main/java/org/eclipse/lyo/core/query/SortTerms.java
@@ -0,0 +1,26 @@
+/*******************************************************************************
+ * Copyright (c) 2012 IBM Corporation.
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
+ *
+ * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
+ * and the Eclipse Distribution License is available at
+ * http://www.eclipse.org/org/documents/edl-v10.php.
+ *
+ * Contributors:
+ *
+ * Steve Pitschke - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.lyo.core.query;
+
+import java.util.List;
+
+/**
+ * List of sort terms from oslc.orderBy clause
+ */
+public interface SortTerms
+{
+ List<SortTerm> children();
+}
diff --git a/org.eclipse.lyo.core.query/src/main/java/org/eclipse/lyo/core/query/impl/ScopedSortTermInvocationHandler.java b/org.eclipse.lyo.core.query/src/main/java/org/eclipse/lyo/core/query/impl/ScopedSortTermInvocationHandler.java
new file mode 100644
index 0000000..b8aa1d7
--- /dev/null
+++ b/org.eclipse.lyo.core.query/src/main/java/org/eclipse/lyo/core/query/impl/ScopedSortTermInvocationHandler.java
@@ -0,0 +1,84 @@
+/*******************************************************************************
+ * Copyright (c) 2012 IBM Corporation.
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
+ *
+ * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
+ * and the Eclipse Distribution License is available at
+ * http://www.eclipse.org/org/documents/edl-v10.php.
+ *
+ * Contributors:
+ *
+ * Steve Pitschke - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.lyo.core.query.impl;
+
+import java.lang.reflect.Method;
+import java.lang.reflect.Proxy;
+import java.util.Map;
+
+import org.antlr.runtime.tree.CommonTree;
+import org.eclipse.lyo.core.query.ScopedSortTerm;
+import org.eclipse.lyo.core.query.SortTerm.Type;
+import org.eclipse.lyo.core.query.SortTerms;
+
+/**
+ * Proxy implementation of {@link ScopedSortTerm}
+ */
+class ScopedSortTermInvocationHandler extends SortTermInvocationHandler
+{
+ public
+ ScopedSortTermInvocationHandler(
+ CommonTree tree,
+ Map<String, String> prefixMap
+ )
+ {
+ super(Type.SCOPED, tree, prefixMap);
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.lyo.core.query.impl.SortTerm#invoke(java.lang.Object, java.lang.reflect.Method, java.lang.Object[])
+ */
+ @Override
+ public Object invoke(
+ Object proxy,
+ Method method,
+ Object[] args
+ ) throws Throwable
+ {
+ String methodName = method.getName();
+ boolean isSortTerm = methodName.equals("sortTerms");
+
+ if (! isSortTerm &&
+ ! methodName.equals("toString")) {
+ return super.invoke(proxy, method, args);
+ }
+
+ if (sortTerms == null) {
+
+ sortTerms = (SortTerms)
+ Proxy.newProxyInstance(SortTerms.class.getClassLoader(),
+ new Class<?>[] { SortTerms.class },
+ new SortTermsInvocationHandler(
+ (CommonTree)tree.getChild(1), prefixMap));
+
+ }
+
+ if (isSortTerm) {
+ return sortTerms;
+ }
+
+ StringBuffer buffer = new StringBuffer();
+
+ buffer.append(((ScopedSortTerm)proxy).identifier().toString());
+ buffer.append('{');
+ buffer.append(sortTerms.toString());
+ buffer.append('}');
+
+ return buffer.toString();
+ }
+
+ private SortTerms sortTerms = null;
+}
diff --git a/org.eclipse.lyo.core.query/src/main/java/org/eclipse/lyo/core/query/impl/SimpleSortTermInvocationHandler.java b/org.eclipse.lyo.core.query/src/main/java/org/eclipse/lyo/core/query/impl/SimpleSortTermInvocationHandler.java
new file mode 100644
index 0000000..43a031b
--- /dev/null
+++ b/org.eclipse.lyo.core.query/src/main/java/org/eclipse/lyo/core/query/impl/SimpleSortTermInvocationHandler.java
@@ -0,0 +1,73 @@
+/*******************************************************************************
+ * Copyright (c) 2012 IBM Corporation.
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
+ *
+ * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
+ * and the Eclipse Distribution License is available at
+ * http://www.eclipse.org/org/documents/edl-v10.php.
+ *
+ * Contributors:
+ *
+ * Steve Pitschke - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.lyo.core.query.impl;
+
+import java.lang.reflect.Method;
+import java.util.Map;
+
+import org.antlr.runtime.tree.CommonTree;
+import org.eclipse.lyo.core.query.OslcOrderByLexer;
+import org.eclipse.lyo.core.query.SimpleSortTerm;
+import org.eclipse.lyo.core.query.SortTerm.Type;
+
+/**
+ * @author pitschke
+ *
+ */
+public class SimpleSortTermInvocationHandler extends SortTermInvocationHandler
+{
+ public
+ SimpleSortTermInvocationHandler(
+ CommonTree tree,
+ Map<String, String> prefixMap
+ )
+ {
+ super(Type.SIMPLE, tree, prefixMap);
+ }
+
+ /**
+ * @see java.lang.reflect.InvocationHandler#invoke(java.lang.Object, java.lang.reflect.Method, java.lang.Object[])
+ */
+ @Override
+ public Object
+ invoke(
+ Object proxy,
+ Method method,
+ Object[] args
+ ) throws Throwable
+ {
+ String methodName = method.getName();
+ boolean isAscending = methodName.equals("ascending");
+
+ if (! isAscending &&
+ ! methodName.equals("toString")) {
+ return super.invoke(proxy, method, args);
+ }
+
+ if (ascending == null) {
+ ascending = tree.getChild(1).getText().equals("+");
+ }
+
+ if (isAscending) {
+ return ascending;
+ }
+
+ return (ascending ? '+' : '-') +
+ ((SimpleSortTerm)proxy).identifier().toString();
+ }
+
+ private Boolean ascending = null;
+}
diff --git a/org.eclipse.lyo.core.query/src/main/java/org/eclipse/lyo/core/query/impl/SortTermInvocationHandler.java b/org.eclipse.lyo.core.query/src/main/java/org/eclipse/lyo/core/query/impl/SortTermInvocationHandler.java
new file mode 100644
index 0000000..f87f9bc
--- /dev/null
+++ b/org.eclipse.lyo.core.query/src/main/java/org/eclipse/lyo/core/query/impl/SortTermInvocationHandler.java
@@ -0,0 +1,85 @@
+/*******************************************************************************
+ * Copyright (c) 2012 IBM Corporation.
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
+ *
+ * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
+ * and the Eclipse Distribution License is available at
+ * http://www.eclipse.org/org/documents/edl-v10.php.
+ *
+ * Contributors:
+ *
+ * Steve Pitschke - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.lyo.core.query.impl;
+
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
+import java.util.Map;
+
+import org.antlr.runtime.tree.CommonTree;
+import org.eclipse.lyo.core.query.PName;
+import org.eclipse.lyo.core.query.SortTerm;
+import org.eclipse.lyo.core.query.SortTerm.Type;
+
+/**
+ * Proxy implementation of {@link SortTerm}
+ */
+class SortTermInvocationHandler implements InvocationHandler
+{
+ public
+ SortTermInvocationHandler(
+ Type type,
+ CommonTree tree,
+ Map<String, String> prefixMap
+ )
+ {
+ this.type = type;
+ this.tree = tree;
+ this.prefixMap = prefixMap;
+ }
+
+ /**
+ * @see java.lang.reflect.InvocationHandler#invoke(java.lang.Object, java.lang.reflect.Method, java.lang.Object[])
+ */
+ @Override
+ public Object
+ invoke(
+ Object proxy,
+ Method method,
+ Object[] args
+ ) throws Throwable
+ {
+ if (method.getName().equals("type")) {
+ return type;
+ }
+
+ if (identifier == null) {
+
+ String rawProperty = tree.getChild(0).getText();
+
+ identifier = new PName();
+
+ int colon = rawProperty.indexOf(':');
+
+ if (colon < 0) {
+ identifier.local = rawProperty;
+ } else {
+ if (colon > 0) {
+ identifier.prefix = rawProperty.substring(0, colon);
+ identifier.namespace = prefixMap.get(identifier.prefix);
+ }
+ identifier.local = rawProperty.substring(colon + 1);
+ }
+ }
+
+ return identifier;
+ }
+
+ private final Type type;
+ protected final CommonTree tree;
+ protected final Map<String, String> prefixMap;
+ private PName identifier = null;
+}
diff --git a/org.eclipse.lyo.core.query/src/main/java/org/eclipse/lyo/core/query/impl/SortTermsInvocationHandler.java b/org.eclipse.lyo.core.query/src/main/java/org/eclipse/lyo/core/query/impl/SortTermsInvocationHandler.java
new file mode 100644
index 0000000..655baf5
--- /dev/null
+++ b/org.eclipse.lyo.core.query/src/main/java/org/eclipse/lyo/core/query/impl/SortTermsInvocationHandler.java
@@ -0,0 +1,118 @@
+/*******************************************************************************
+ * Copyright (c) 2012 IBM Corporation.
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
+ *
+ * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
+ * and the Eclipse Distribution License is available at
+ * http://www.eclipse.org/org/documents/edl-v10.php.
+ *
+ * Contributors:
+ *
+ * Steve Pitschke - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.lyo.core.query.impl;
+
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
+import java.lang.reflect.Proxy;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+
+import org.antlr.runtime.tree.CommonTree;
+import org.eclipse.lyo.core.query.OslcOrderByParser;
+import org.eclipse.lyo.core.query.ScopedSortTerm;
+import org.eclipse.lyo.core.query.SimpleSortTerm;
+import org.eclipse.lyo.core.query.SortTerm;
+
+/**
+ * Proxy implementation of {@link SortTerms}
+ */
+public class SortTermsInvocationHandler implements InvocationHandler
+{
+ public
+ SortTermsInvocationHandler(
+ CommonTree tree,
+ Map<String, String> prefixMap
+ )
+ {
+ this.tree = tree;
+ this.prefixMap = prefixMap;
+ }
+
+ /**
+ * @see java.lang.reflect.InvocationHandler#invoke(java.lang.Object, java.lang.reflect.Method, java.lang.Object[])
+ */
+ @Override
+ public Object
+ invoke(
+ Object proxy,
+ Method method,
+ Object[] args
+ ) throws Throwable
+ {
+ if (children == null) {
+
+ @SuppressWarnings("unchecked")
+ List<CommonTree> rawChildren = tree.getChildren();
+
+ children = new ArrayList<SortTerm>(rawChildren.size());
+
+ for (CommonTree child : rawChildren) {
+
+ Object simpleTerm;
+
+ switch(child.getToken().getType()) {
+ case OslcOrderByParser.SIMPLE_TERM:
+ simpleTerm =
+ Proxy.newProxyInstance(SimpleSortTerm.class.getClassLoader(),
+ new Class<?>[] { SimpleSortTerm.class },
+ new SimpleSortTermInvocationHandler(
+ child, prefixMap));
+ break;
+ case OslcOrderByParser.SCOPED_TERM:
+ simpleTerm =
+ Proxy.newProxyInstance(ScopedSortTerm.class.getClassLoader(),
+ new Class<?>[] { ScopedSortTerm.class },
+ new ScopedSortTermInvocationHandler(
+ child, prefixMap));
+ break;
+ default:
+ throw new IllegalStateException("unimplemented type of sort term: " + child.getToken().getText());
+ }
+
+ children.add((SortTerm)simpleTerm);
+ }
+
+ children = Collections.unmodifiableList(children);
+ }
+
+ if (method.getName().equals("children")) {
+ return children;
+ }
+
+ StringBuffer buffer = new StringBuffer();
+ boolean first = true;
+
+ for (SortTerm term : children) {
+
+ if (first) {
+ first = false;
+ } else {
+ buffer.append(',');
+ }
+
+ buffer.append(term.toString());
+ }
+
+ return buffer.toString();
+ }
+
+ private final CommonTree tree;
+ private final Map<String, String> prefixMap;
+ private List<SortTerm> children = null;
+}
diff --git a/org.eclipse.lyo.core.query/src/test/java/org/eclipse/lyo/core/query/test/BasicOrderByTest.java b/org.eclipse.lyo.core.query/src/test/java/org/eclipse/lyo/core/query/test/BasicOrderByTest.java
new file mode 100644
index 0000000..a515bca
--- /dev/null
+++ b/org.eclipse.lyo.core.query/src/test/java/org/eclipse/lyo/core/query/test/BasicOrderByTest.java
@@ -0,0 +1,74 @@
+/*******************************************************************************
+ * Copyright (c) 2012 IBM Corporation.
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
+ *
+ * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
+ * and the Eclipse Distribution License is available at
+ * http://www.eclipse.org/org/documents/edl-v10.php.
+ *
+ * Contributors:
+ *
+ * Steve Pitschke - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.lyo.core.query.test;
+
+import java.lang.reflect.Proxy;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.antlr.runtime.RecognitionException;
+import org.antlr.runtime.tree.CommonTree;
+import org.eclipse.lyo.core.query.OrderByClause;
+import org.eclipse.lyo.core.query.OslcOrderByParser;
+import org.eclipse.lyo.core.query.SortTerms;
+import org.eclipse.lyo.core.query.impl.SortTermsInvocationHandler;
+
+/**
+ * Basic tests of oslc.orderBy clause parsing
+ */
+public class BasicOrderByTest
+{
+
+ /**
+ * @param args
+ */
+ public static void main(String[] args)
+ {
+ Map<String, String> prefixMap = new HashMap<String, String>();
+
+ prefixMap.put("qm", "http://qm.example.com/ns");
+ prefixMap.put("olsc", "http://open-services.net/ns/core#");
+
+ String[] expressions = {
+ "+gm:priority",
+ "+gm:priority,-oscl:name",
+ "gm:tested_by{+oslc:description}",
+ };
+
+ for (String expression : expressions) {
+
+ OslcOrderByParser parser = new OslcOrderByParser(expression);
+
+ try {
+ OslcOrderByParser.oslc_order_by_return resultTree =
+ parser.oslc_order_by();
+
+ OrderByClause orderByClause = (OrderByClause)
+ Proxy.newProxyInstance(OrderByClause.class.getClassLoader(),
+ new Class<?>[] { OrderByClause.class, SortTerms.class },
+ new SortTermsInvocationHandler(
+ (CommonTree)resultTree.getTree(),
+ prefixMap));
+
+ System.out.println(orderByClause);
+
+ } catch (RecognitionException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+ }
+}
diff --git a/org.eclipse.lyo.core.query/src/test/java/org/eclipse/lyo/core/query/test/BasicSearchTermsTest.java b/org.eclipse.lyo.core.query/src/test/java/org/eclipse/lyo/core/query/test/BasicSearchTermsTest.java
new file mode 100644
index 0000000..aaced0e
--- /dev/null
+++ b/org.eclipse.lyo.core.query/src/test/java/org/eclipse/lyo/core/query/test/BasicSearchTermsTest.java
@@ -0,0 +1,110 @@
+/*******************************************************************************
+ * Copyright (c) 2012 IBM Corporation.
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
+ *
+ * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
+ * and the Eclipse Distribution License is available at
+ * http://www.eclipse.org/org/documents/edl-v10.php.
+ *
+ * Contributors:
+ *
+ * Steve Pitschke - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.lyo.core.query.test;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.antlr.runtime.RecognitionException;
+import org.antlr.runtime.tree.CommonTree;
+import org.eclipse.lyo.core.query.OslcSearchTermsParser;
+import org.eclipse.lyo.core.query.SearchTermsClause;
+
+/**
+ * Basic tests of oslc.searchTerms clause parsing
+ */
+public class BasicSearchTermsTest
+{
+ static class StringList extends ArrayList<String>
+ implements SearchTermsClause
+ {
+ public
+ StringList(int size)
+ {
+ super(size);
+ }
+
+ public String
+ toString()
+ {
+ StringBuffer buffer = new StringBuffer();
+ boolean first = true;
+
+ for (String string : this) {
+
+ if (first) {
+ first = false;
+ } else {
+ buffer.append(',');
+ }
+
+ buffer.append('"');
+ buffer.append(string);
+ buffer.append('"');
+ }
+
+ return buffer.toString();
+ }
+
+ private static final long serialVersionUID = 1943909246265711359L;
+ }
+
+ /**
+ * @param args
+ */
+ public static void main(String[] args)
+ {
+ Map<String, String> prefixMap = new HashMap<String, String>();
+
+ prefixMap.put("qm", "http://qm.example.com/ns");
+ prefixMap.put("olsc", "http://open-services.net/ns/core#");
+
+ String[] expressions = {
+ "\"foobar\"",
+ "\"foobar\",\"whatsis\\\"yousa\"",
+ };
+
+ for (String expression : expressions) {
+
+ OslcSearchTermsParser parser = new OslcSearchTermsParser(expression);
+
+ try {
+ OslcSearchTermsParser.oslc_search_terms_return resultTree =
+ parser.oslc_search_terms();
+
+ CommonTree rawTree = (CommonTree)resultTree.getTree();
+ @SuppressWarnings("unchecked")
+ List<CommonTree> rawList = rawTree.getChildren();
+ List<String> stringList = new StringList(rawList.size());
+
+ for (CommonTree string : rawList) {
+
+ String rawString = string.getText();
+
+ stringList.add(rawString.substring(1, rawString.length()-1));
+ }
+
+ System.out.println(stringList);
+
+ } catch (RecognitionException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+ }
+}