1 package com.indexdata.mkjsf.pazpar2.commands;
\r
3 import java.io.Serializable;
\r
5 import org.apache.log4j.Logger;
\r
8 * Represents a complex command parameter value, in form of an expression with
\r
9 * an equality operator
\r
11 * An expression consist of a left-of-operator field or key, an equality operator (= or ~),
\r
12 * a right-of-operator value, and optionally a label describing the value for UI display.
\r
16 * <li><code>pz:id=1234</code> "My Target"</li>
\r
17 * <li><code>category~libcatalog</code> "Library Catalogs"</li>
\r
18 * <li><code>author="Steinbeck, John"</code></li>
\r
20 * @author Niels Erik
\r
23 public class Expression implements Serializable {
\r
25 private static final long serialVersionUID = -751704027842027769L;
\r
26 private static Logger logger = Logger.getLogger(Expression.class);
\r
33 * Instantiates an expression with a label
\r
35 * @param leftEntity left-of-operator field name (or 'key')
\r
36 * @param operator an equality operator
\r
37 * @param rightEntity right-of-operator value
\r
38 * @param label to be used for display, for instance in a UI control that adds or removes the expression
\r
39 * from a command parameter
\r
41 public Expression (String field, String operator, String value, String label) {
\r
42 this.leftEntity = field;
\r
43 this.operator = operator;
\r
44 this.rightEntity = value;
\r
49 * Instantiates an expression by parsing the provided expression string, which must be
\r
50 * on the form {name}({=}or{~}){value}.
\r
52 * Currently only '=' and '~' are recognized as operators
\r
55 * @param expressionString
\r
57 public Expression (String expressionString) {
\r
58 String[] parts = expressionString.split("[=~]");
\r
59 if (parts.length>0) {
\r
60 this.leftEntity = parts[0];
\r
61 this.operator = expressionString.contains("=") ? "=" : "~";
\r
63 if (parts.length>1) {
\r
64 this.rightEntity = parts[1];
\r
65 this.label=rightEntity;
\r
70 * Clones the expression
\r
72 * @return a clone of this expression
\r
74 public Expression copy() {
\r
75 logger.trace("Copying " + this.toString());
\r
76 return new Expression(leftEntity, operator, rightEntity, label);
\r
79 public String toString() {
\r
80 return leftEntity + operator + rightEntity;
\r
84 * Returns the label describing the value of the expression or,
\r
85 * if no label was provided, the value itself.
\r
87 * @return label or right-of-operator value if no label provided
\r
89 public String getLabel() {
\r
94 * Returns the left-of-operator field (or name or key).
\r
96 * @return entity left of operator
\r
98 public String getField () {
\r
103 * Returns the operator
\r
105 * @return the operator of the expression
\r
107 public String getOperator() {
\r
112 * Returns the right-of-operator value of the expression
\r
114 * @return entity right of operator
\r
116 public String getValue() {
\r
117 return rightEntity;
\r