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
67 this.rightEntity = "";
\r
73 * Clones the expression
\r
75 * @return a clone of this expression
\r
77 public Expression copy() {
\r
78 logger.trace("Copying " + this.toString());
\r
79 return new Expression(leftEntity, operator, rightEntity, label);
\r
82 public String toString() {
\r
83 return leftEntity + operator + rightEntity;
\r
87 * Returns the label describing the value of the expression or,
\r
88 * if no label was provided, the value itself.
\r
90 * @return label or right-of-operator value if no label provided
\r
92 public String getLabel() {
\r
97 * Returns the left-of-operator field (or name or key).
\r
99 * @return entity left of operator
\r
101 public String getField () {
\r
106 * Returns the operator
\r
108 * @return the operator of the expression
\r
110 public String getOperator() {
\r
115 * Returns the right-of-operator value of the expression
\r
117 * @return entity right of operator
\r
119 public String getValue() {
\r
120 return rightEntity;
\r