summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSteve Pitschke2012-07-09 15:23:23 (EDT)
committer Steve Pitschke2012-07-09 15:23:23 (EDT)
commita9a35f91a676e2e755ada7201e3b1c325f22ca73 (patch)
tree812553140f8c3bc42052d63ab817fe2cc9fdbeab
parent53f227d168ec3f0af1ca31f043f3e77933094e18 (diff)
downloadorg.eclipse.lyo.core-a9a35f91a676e2e755ada7201e3b1c325f22ca73.zip
org.eclipse.lyo.core-a9a35f91a676e2e755ada7201e3b1c325f22ca73.tar.gz
org.eclipse.lyo.core-a9a35f91a676e2e755ada7201e3b1c325f22ca73.tar.bz2
Bug 383469: Add QueryUtils class and parsing of oslc.prefix clauserefs/changes/83/6683/3
Change-Id: Id63c9a865aeba7c4fb9766a303c2bc9cb1600371 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/OslcPrefix.g147
-rw-r--r--org.eclipse.lyo.core.query/src/main/java/org/eclipse/lyo/core/query/ParseException.java35
-rw-r--r--org.eclipse.lyo.core.query/src/main/java/org/eclipse/lyo/core/query/QueryUtils.java398
-rw-r--r--org.eclipse.lyo.core.query/src/test/java/org/eclipse/lyo/core/query/test/BasicOrderByTest.java35
-rw-r--r--org.eclipse.lyo.core.query/src/test/java/org/eclipse/lyo/core/query/test/BasicPrefixesTest.java55
-rw-r--r--org.eclipse.lyo.core.query/src/test/java/org/eclipse/lyo/core/query/test/BasicSearchTermsTest.java72
-rw-r--r--org.eclipse.lyo.core.query/src/test/java/org/eclipse/lyo/core/query/test/BasicSelectTest.java53
-rw-r--r--org.eclipse.lyo.core.query/src/test/java/org/eclipse/lyo/core/query/test/BasicWhereTest.java51
8 files changed, 679 insertions, 167 deletions
diff --git a/org.eclipse.lyo.core.query/src/main/antlr3/org/eclipse/lyo/core/query/OslcPrefix.g b/org.eclipse.lyo.core.query/src/main/antlr3/org/eclipse/lyo/core/query/OslcPrefix.g
new file mode 100644
index 0000000..96fad2f
--- /dev/null
+++ b/org.eclipse.lyo.core.query/src/main/antlr3/org/eclipse/lyo/core/query/OslcPrefix.g
@@ -0,0 +1,147 @@
+/******************************************************************************
+ * 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 OslcPrefix;
+
+options {
+ output=AST;
+ ASTLabelType=CommonTree;
+}
+
+tokens {
+ PREFIX_LIST = 'prefix_list';
+ PREFIX = 'prefix';
+}
+
+@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 OslcPrefixParser(String prefixes)
+ {
+ this(new CommonTokenStream(new OslcPrefixLexer(new ANTLRStringStream(prefixes))));
+ }
+}
+
+
+oslc_prefixes : prefix_binding ( ',' prefix_binding )* -> ^( 'prefix_list' prefix_binding (prefix_binding)* )
+ ;
+
+prefix_binding : PN_PREFIX '=' IRI_REF -> ^( 'prefix' PN_PREFIX IRI_REF )
+ ;
+
+// $>
+
+// $<Lexer
+
+WS
+ : (' '| '\t'| EOL)+ { $channel=HIDDEN; }
+ ;
+
+fragment
+EOL
+ : '\n' | '\r'
+ ;
+
+PN_PREFIX
+ : PN_CHARS_BASE ((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
+PN_CHARS
+ : PN_CHARS_U
+ | MINUS
+ | DIGIT
+ | '\u00B7'
+ | '\u0300'..'\u036F'
+ | '\u203F'..'\u2040'
+ ;
+
+fragment
+PN_CHARS_U
+ : PN_CHARS_BASE | '_'
+ ;
+
+fragment
+DIGIT
+ : '0'..'9'
+ ;
+
+IRI_REF
+ : LESS ( options {greedy=false;} : ~(LESS | GREATER | '"' | OPEN_CURLY_BRACE | CLOSE_CURLY_BRACE | '|' | '^' | '\\' | '`' | ('\u0000'..'\u0020')) )* GREATER
+ ;
+
+COMMA
+ : ','
+ ;
+
+DOT
+ : '.'
+ ;
+
+MINUS
+ : '-'
+ ;
+
+OPEN_CURLY_BRACE
+ : '{'
+ ;
+
+CLOSE_CURLY_BRACE
+ : '}'
+ ;
+
+LESS
+ : '<'
+ ;
+
+GREATER
+ : '>'
+ ;
+
+// $>
diff --git a/org.eclipse.lyo.core.query/src/main/java/org/eclipse/lyo/core/query/ParseException.java b/org.eclipse.lyo.core.query/src/main/java/org/eclipse/lyo/core/query/ParseException.java
new file mode 100644
index 0000000..78c0941
--- /dev/null
+++ b/org.eclipse.lyo.core.query/src/main/java/org/eclipse/lyo/core/query/ParseException.java
@@ -0,0 +1,35 @@
+/*******************************************************************************
+ * 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 org.antlr.runtime.RecognitionException;
+
+/**
+ * Wrapper for {@link RecognitionException} so clients of OLSC query
+ * functionality don't need to import ANTLR classes.
+ */
+public class ParseException extends Exception
+{
+ /**
+ * @param cause
+ */
+ ParseException(RecognitionException cause)
+ {
+ super(cause);
+ }
+
+ private static final long serialVersionUID = 2373494371127406191L;
+}
diff --git a/org.eclipse.lyo.core.query/src/main/java/org/eclipse/lyo/core/query/QueryUtils.java b/org.eclipse.lyo.core.query/src/main/java/org/eclipse/lyo/core/query/QueryUtils.java
new file mode 100644
index 0000000..888c456
--- /dev/null
+++ b/org.eclipse.lyo.core.query/src/main/java/org/eclipse/lyo/core/query/QueryUtils.java
@@ -0,0 +1,398 @@
+/*******************************************************************************
+ * 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.lang.reflect.Proxy;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+import org.antlr.runtime.RecognitionException;
+import org.antlr.runtime.Token;
+import org.antlr.runtime.tree.CommonErrorNode;
+import org.antlr.runtime.tree.CommonTree;
+import org.antlr.runtime.tree.Tree;
+import org.eclipse.lyo.core.query.impl.CompoundTermInvocationHandler;
+import org.eclipse.lyo.core.query.impl.PropertyListInvocationHandler;
+import org.eclipse.lyo.core.query.impl.SortTermsInvocationHandler;
+import org.eclipse.lyo.core.query.impl.WildcardInvocationHandler;
+
+/**
+ * Utility methods for parsing various OSLC HTTP query
+ * parameter clauses; e.g. oslc.where
+ */
+public class QueryUtils
+{
+ /**
+ * Parse a oslc.prefix clause into a map between prefixes
+ * and corresponding URIs
+ *
+ * <p><b>Note</b>: {@link Object#toString()} of result has been overridden to
+ * return input expression.
+ *
+ * @param prefixExpression the oslc.prefix expression
+ *
+ * @return the prefix map
+ *
+ * @throws ParseException
+ */
+ public static Map<String, String>
+ parsePrefixes(
+ String prefixExpression
+ ) throws ParseException
+ {
+ OslcPrefixParser parser = new OslcPrefixParser(prefixExpression);
+
+ try {
+
+ CommonTree rawTree =
+ (CommonTree)parser.oslc_prefixes().getTree();
+ @SuppressWarnings("unchecked")
+ List<CommonTree> rawPrefixes = rawTree.getChildren();
+ PrefixMap prefixMap =
+ new PrefixMap(rawPrefixes.size());
+
+ for (CommonTree rawPrefix : rawPrefixes) {
+
+ if (rawPrefix.getType() == Token.INVALID_TOKEN_TYPE) {
+ throw ((CommonErrorNode)rawPrefix).trappedException;
+ }
+
+ String pn = rawPrefix.getChild(0).getText();
+ String uri = rawPrefix.getChild(1).getText();
+
+ uri = uri.substring(1, uri.length() - 1);
+
+ prefixMap.put(pn, uri);
+ }
+
+ return prefixMap;
+
+ } catch (RecognitionException e) {
+ throw new ParseException(e);
+ }
+ }
+
+ /**
+ * Parse a oslc.where expression
+ *
+ * @param whereExpression contents of an oslc.where HTTP query
+ * parameter
+ * @param prefixMap map between XML namespace prefixes and
+ * associated URLs
+ *
+ * @return the parsed where clause
+ *
+ * @throws ParseException
+ */
+ public static WhereClause
+ parseWhere(
+ String whereExpression,
+ Map<String, String> prefixMap
+ ) throws ParseException
+ {
+ OslcWhereParser parser = new OslcWhereParser(whereExpression);
+
+ try {
+
+ OslcWhereParser.oslc_where_return resultTree =
+ parser.oslc_where();
+ CommonTree rawTree = (CommonTree)resultTree.getTree();
+ Tree child = rawTree.getChild(0);
+
+ if (child.getType() == Token.INVALID_TOKEN_TYPE) {
+ throw ((CommonErrorNode)child).trappedException;
+ }
+
+ return (WhereClause)
+ Proxy.newProxyInstance(CompoundTerm.class.getClassLoader(),
+ new Class<?>[] { CompoundTerm.class, WhereClause.class },
+ new CompoundTermInvocationHandler(
+ rawTree, true, prefixMap));
+
+ } catch (RecognitionException e) {
+ throw new ParseException(e);
+ }
+ }
+
+ /**
+ * Parse a oslc.select expression
+ *
+ * @param selectExpression contents of an oslc.select HTTP query
+ * parameter
+ * @param prefixMap map between XML namespace prefixes and
+ * associated URLs
+ *
+ * @return the parsed select clause
+ *
+ * @throws ParseException
+ */
+ public static SelectClause
+ parseSelect(
+ String selectExpression,
+ Map<String, String> prefixMap
+ ) throws ParseException
+ {
+ OslcSelectParser parser = new OslcSelectParser(selectExpression);
+
+ try {
+
+ OslcSelectParser.oslc_select_return resultTree =
+ parser.oslc_select();
+ CommonTree rawTree = (CommonTree)resultTree.getTree();
+
+ if (rawTree.getType() == Token.INVALID_TOKEN_TYPE) {
+ throw ((CommonErrorNode)rawTree).trappedException;
+ }
+
+ if (rawTree.getType() == OslcSelectParser.PROPERTIES) {
+ return (SelectClause)
+ Proxy.newProxyInstance(SelectClause.class.getClassLoader(),
+ new Class<?>[] { SelectClause.class, PropertyList.class },
+ new PropertyListInvocationHandler(
+ (CommonTree)resultTree.getTree(),
+ prefixMap));
+ } else {
+ return (SelectClause)
+ Proxy.newProxyInstance(SelectClause.class.getClassLoader(),
+ new Class<?>[] { SelectClause.class, Wildcard.class },
+ new WildcardInvocationHandler());
+ }
+
+ } catch (RecognitionException e) {
+ throw new ParseException(e);
+ }
+ }
+
+ /**
+ * Parse a oslc.properties expression
+ *
+ * @param propertiesExpression contents of an oslc.properties HTTP query
+ * parameter
+ * @param prefixMap map between XML namespace prefixes and
+ * associated URLs
+ *
+ * @return the parsed properties clause
+ *
+ * @throws ParseException
+ */
+ public static PropertiesClause
+ parseProperties(
+ String propertiesExpression,
+ Map<String, String> prefixMap
+ ) throws ParseException
+ {
+ OslcSelectParser parser = new OslcSelectParser(propertiesExpression);
+
+ try {
+
+ OslcSelectParser.oslc_select_return resultTree =
+ parser.oslc_select();
+ CommonTree rawTree = (CommonTree)resultTree.getTree();
+
+ if (rawTree.getType() == Token.INVALID_TOKEN_TYPE) {
+ throw ((CommonErrorNode)rawTree).trappedException;
+ }
+
+ if (rawTree.getType() == OslcSelectParser.PROPERTIES) {
+ return (PropertiesClause)
+ Proxy.newProxyInstance(SelectClause.class.getClassLoader(),
+ new Class<?>[] { PropertiesClause.class, PropertyList.class },
+ new PropertyListInvocationHandler(
+ (CommonTree)resultTree.getTree(),
+ prefixMap));
+ } else {
+ return (PropertiesClause)
+ Proxy.newProxyInstance(SelectClause.class.getClassLoader(),
+ new Class<?>[] { PropertiesClause.class, Wildcard.class },
+ new WildcardInvocationHandler());
+ }
+
+ } catch (RecognitionException e) {
+ throw new ParseException(e);
+ }
+ }
+
+ /**
+ * Parse a oslc.orderBy expression
+ *
+ * @param orderByExpression contents of an oslc.orderBy HTTP query
+ * parameter
+ * @param prefixMap map between XML namespace prefixes and
+ * associated URLs
+ *
+ * @return the parsed order by clause
+ *
+ * @throws ParseException
+ */
+ public static OrderByClause
+ parseOrderBy(
+ String orderByExpression,
+ Map<String, String> prefixMap
+ ) throws ParseException
+ {
+ OslcOrderByParser parser = new OslcOrderByParser(orderByExpression);
+
+ try {
+
+ OslcOrderByParser.oslc_order_by_return resultTree =
+ parser.oslc_order_by();
+ CommonTree rawTree = (CommonTree)resultTree.getTree();
+ Tree child = rawTree.getChild(0);
+
+ if (child.getType() == Token.INVALID_TOKEN_TYPE) {
+ throw ((CommonErrorNode)child).trappedException;
+ }
+
+ return (OrderByClause)
+ Proxy.newProxyInstance(OrderByClause.class.getClassLoader(),
+ new Class<?>[] { OrderByClause.class, SortTerms.class },
+ new SortTermsInvocationHandler(rawTree, prefixMap));
+
+ } catch (RecognitionException e) {
+ throw new ParseException(e);
+ }
+ }
+
+ /**
+ * Parse a oslc.searchTerms expression
+ *
+ * <p><b>Note</b>: {@link Object#toString()} of result has been overridden to
+ * return input expression.
+ *
+ * @param searchTermsExpression contents of an oslc.searchTerms HTTP query
+ * parameter
+ *
+ * @return the parsed search terms clause
+ *
+ * @throws ParseException
+ */
+ public static SearchTermsClause
+ parseSearchTerms(
+ String searchTermsExpression
+ ) throws ParseException
+ {
+ OslcSearchTermsParser parser = new OslcSearchTermsParser(searchTermsExpression);
+
+ try {
+
+ OslcSearchTermsParser.oslc_search_terms_return resultTree =
+ parser.oslc_search_terms();
+ CommonTree rawTree = (CommonTree)resultTree.getTree();
+ Tree child = rawTree.getChild(0);
+
+ if (child.getType() == Token.INVALID_TOKEN_TYPE) {
+ throw ((CommonErrorNode)child).trappedException;
+ }
+
+ @SuppressWarnings("unchecked")
+ List<CommonTree> rawList = rawTree.getChildren();
+ StringList stringList = new StringList(rawList.size());
+
+ for (CommonTree string : rawList) {
+
+ String rawString = string.getText();
+
+ stringList.add(rawString.substring(1, rawString.length()-1));
+ }
+
+ return stringList;
+
+ } catch (RecognitionException e) {
+ throw new ParseException(e);
+ }
+ }
+
+ /**
+ * Implementation of a {@link SearchTermsClause} interface
+ */
+ 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;
+ }
+
+ /**
+ * Implementation of a Map<String, String> prefixMap
+ */
+ static class PrefixMap extends HashMap<String, String>
+ {
+ public
+ PrefixMap(int size)
+ {
+ super(size);
+ }
+
+ public String
+ toString()
+ {
+ StringBuffer buffer = new StringBuffer();
+ Iterator<String> keys = this.keySet().iterator();
+ boolean first = true;
+
+ while (keys.hasNext()) {
+
+ if (first) {
+ first = false;
+ } else {
+ buffer.append(',');
+ }
+
+ String key = keys.next();
+
+ buffer.append(key);
+ buffer.append('=');
+ buffer.append('<');
+ buffer.append(this.get(key));
+ buffer.append('>');
+ }
+
+ return buffer.toString();
+ }
+
+ private static final long serialVersionUID = 1943909246265711359L;
+ }
+}
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
index a515bca..0b66475 100644
--- 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
@@ -15,16 +15,11 @@
*******************************************************************************/
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;
+import org.eclipse.lyo.core.query.ParseException;
+import org.eclipse.lyo.core.query.QueryUtils;
/**
* Basic tests of oslc.orderBy clause parsing
@@ -37,37 +32,29 @@ public class BasicOrderByTest
*/
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 prefixes = "qm=<http://qm.example.com/ns>," +
+ "olsc=<http://open-services.net/ns/core#>";
String[] expressions = {
"+gm:priority",
"+gm:priority,-oscl:name",
"gm:tested_by{+oslc:description}",
+ "?qm:blah"
};
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));
+ Map<String, String> prefixMap =
+ QueryUtils.parsePrefixes(prefixes);
+ OrderByClause orderByClause =
+ QueryUtils.parseOrderBy(expression, prefixMap);
System.out.println(orderByClause);
- } catch (RecognitionException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
+ } catch (ParseException e) {
+ e.printStackTrace(System.out);
}
}
}
diff --git a/org.eclipse.lyo.core.query/src/test/java/org/eclipse/lyo/core/query/test/BasicPrefixesTest.java b/org.eclipse.lyo.core.query/src/test/java/org/eclipse/lyo/core/query/test/BasicPrefixesTest.java
new file mode 100644
index 0000000..ccdeef5
--- /dev/null
+++ b/org.eclipse.lyo.core.query/src/test/java/org/eclipse/lyo/core/query/test/BasicPrefixesTest.java
@@ -0,0 +1,55 @@
+/*******************************************************************************
+ * 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.Map;
+
+import org.eclipse.lyo.core.query.ParseException;
+import org.eclipse.lyo.core.query.QueryUtils;
+
+/**
+ * Basic tests of oslc.searchTerms clause parsing
+ */
+public class BasicPrefixesTest
+{
+ /**
+ * @param args
+ */
+ public static void main(String[] args)
+ {
+ String[] expressions = {
+ "qm=<http://qm.example.com/ns>," +
+ "olsc=<http://open-services.net/ns/core#>," +
+ "xs=<http://www.w3.org/2001/XMLSchema>",
+ "qm=<http://qm.example.com/ns>," +
+ "XXX>"
+ };
+
+ for (String expression : expressions) {
+
+ try {
+
+ Map<String, String> prefixMap =
+ QueryUtils.parsePrefixes(expression);
+
+ System.out.println(prefixMap);
+
+ } catch (ParseException e) {
+ e.printStackTrace(System.out);
+ }
+ }
+ }
+}
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
index aaced0e..1f7a69d 100644
--- 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
@@ -15,14 +15,8 @@
*******************************************************************************/
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.ParseException;
+import org.eclipse.lyo.core.query.QueryUtils;
import org.eclipse.lyo.core.query.SearchTermsClause;
/**
@@ -30,80 +24,28 @@ import org.eclipse.lyo.core.query.SearchTermsClause;
*/
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));
- }
+ SearchTermsClause stringList =
+ QueryUtils.parseSearchTerms(expression);
System.out.println(stringList);
- } catch (RecognitionException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
+ } catch (ParseException e) {
+ e.printStackTrace(System.out);
}
}
}
diff --git a/org.eclipse.lyo.core.query/src/test/java/org/eclipse/lyo/core/query/test/BasicSelectTest.java b/org.eclipse.lyo.core.query/src/test/java/org/eclipse/lyo/core/query/test/BasicSelectTest.java
index 0487c7e..4f18139 100644
--- a/org.eclipse.lyo.core.query/src/test/java/org/eclipse/lyo/core/query/test/BasicSelectTest.java
+++ b/org.eclipse.lyo.core.query/src/test/java/org/eclipse/lyo/core/query/test/BasicSelectTest.java
@@ -15,73 +15,44 @@
*******************************************************************************/
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.OslcSelectParser;
-import org.eclipse.lyo.core.query.PropertyList;
+import org.eclipse.lyo.core.query.ParseException;
+import org.eclipse.lyo.core.query.QueryUtils;
import org.eclipse.lyo.core.query.SelectClause;
-import org.eclipse.lyo.core.query.Wildcard;
-import org.eclipse.lyo.core.query.impl.PropertyListInvocationHandler;
-import org.eclipse.lyo.core.query.impl.WildcardInvocationHandler;
/**
* Basic tests of oslc.select clause parsing
*/
public class BasicSelectTest
{
-
- /**
- * @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 prefixes = "qm=<http://qm.example.com/ns>," +
+ "olsc=<http://open-services.net/ns/core#>";
String[] expressions = {
"qm:testcase",
"*",
"oslc:create,qm:verified",
"qm:state{oslc:verified_by{oslc:owner,qm:duration}}",
- "qm:submitted{*}"
+ "qm:submitted{*}",
+ "XXX"
};
for (String expression : expressions) {
- OslcSelectParser parser = new OslcSelectParser(expression);
-
try {
- OslcSelectParser.oslc_select_return resultTree =
- parser.oslc_select();
-
- CommonTree rawTree = (CommonTree)resultTree.getTree();
- SelectClause selectClause;
- if (rawTree.getType() == OslcSelectParser.PROPERTIES) {
- selectClause = (SelectClause)
- Proxy.newProxyInstance(SelectClause.class.getClassLoader(),
- new Class<?>[] { SelectClause.class, PropertyList.class },
- new PropertyListInvocationHandler(
- (CommonTree)resultTree.getTree(),
- prefixMap));
- } else {
- selectClause = (SelectClause)
- Proxy.newProxyInstance(SelectClause.class.getClassLoader(),
- new Class<?>[] { SelectClause.class, Wildcard.class },
- new WildcardInvocationHandler());
- }
+ Map<String, String> prefixMap =
+ QueryUtils.parsePrefixes(prefixes);
+ SelectClause selectClause =
+ QueryUtils.parseSelect(expression, prefixMap);
System.out.println(selectClause.toString());
- } catch (RecognitionException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
+ } catch (ParseException e) {
+ e.printStackTrace(System.out);
}
}
}
diff --git a/org.eclipse.lyo.core.query/src/test/java/org/eclipse/lyo/core/query/test/BasicWhereTest.java b/org.eclipse.lyo.core.query/src/test/java/org/eclipse/lyo/core/query/test/BasicWhereTest.java
index e1832b2..963c96f 100644
--- a/org.eclipse.lyo.core.query/src/test/java/org/eclipse/lyo/core/query/test/BasicWhereTest.java
+++ b/org.eclipse.lyo.core.query/src/test/java/org/eclipse/lyo/core/query/test/BasicWhereTest.java
@@ -15,39 +15,22 @@
*******************************************************************************/
package org.eclipse.lyo.core.query.test;
-import java.lang.reflect.Proxy;
-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.ComparisonTerm;
-import org.eclipse.lyo.core.query.CompoundTerm;
-import org.eclipse.lyo.core.query.InTerm;
-import org.eclipse.lyo.core.query.OslcWhereParser;
-import org.eclipse.lyo.core.query.SimpleTerm;
-import org.eclipse.lyo.core.query.Value;
+import org.eclipse.lyo.core.query.ParseException;
+import org.eclipse.lyo.core.query.QueryUtils;
import org.eclipse.lyo.core.query.WhereClause;
-import org.eclipse.lyo.core.query.impl.CompoundTermInvocationHandler;
/**
- * @author pitschke
- *
+ * Basic tests of oslc.where clause parsing
*/
public class BasicWhereTest
{
-
- /**
- * @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#");
- prefixMap.put("xs", "http://www.w3.org/2001/XMLSchema");
+ String prefixes = "qm=<http://qm.example.com/ns>," +
+ "olsc=<http://open-services.net/ns/core#>," +
+ "xs=<http://www.w3.org/2001/XMLSchema>";
String[] expressions = {
"qm:testcase=<http://example.com/tests/31459>",
@@ -56,29 +39,23 @@ public class BasicWhereTest
"qm:state in [\"Done\",\"Open\"]",
"oslc:verified_by{oslc:owner=\"Steve\" and qm:duration=-47.0} and oslc:description=\"very hairy expression\"",
"qm:submitted<\"2011-10-10T07:00:00Z\"^^\"xs:dateTime\"",
- "oslc:label>\"The End\"@en-US"
+ "oslc:label>\"The End\"@en-US",
+ "XXX"
};
for (String expression : expressions) {
- OslcWhereParser parser = new OslcWhereParser(expression);
-
try {
- OslcWhereParser.oslc_where_return resultTree =
- parser.oslc_where();
+ Map<String, String> prefixMap =
+ QueryUtils.parsePrefixes(prefixes);
WhereClause whereClause = (WhereClause)
- Proxy.newProxyInstance(CompoundTerm.class.getClassLoader(),
- new Class<?>[] { CompoundTerm.class, WhereClause.class },
- new CompoundTermInvocationHandler(
- (CommonTree)resultTree.getTree(), true,
- prefixMap));
+ QueryUtils.parseWhere(expression, prefixMap);
- System.out.println("Is where: " + whereClause);
+ System.out.println(whereClause);
- } catch (RecognitionException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
+ } catch (ParseException e) {
+ e.printStackTrace(System.out);
}
}
}