View Javadoc

1   /*
2    * ModeShape (http://www.modeshape.org)
3    * See the COPYRIGHT.txt file distributed with this work for information
4    * regarding copyright ownership.  Some portions may be licensed
5    * to Red Hat, Inc. under one or more contributor license agreements.
6    * See the AUTHORS.txt file in the distribution for a full listing of 
7    * individual contributors.
8    *
9    * ModeShape is free software. Unless otherwise indicated, all code in ModeShape
10   * is licensed to you under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation; either version 2.1 of
12   * the License, or (at your option) any later version.
13   * 
14   * ModeShape is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17   * Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this software; if not, write to the Free
21   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23   */
24  package org.modeshape.jcr.query.qom;
25  
26  import javax.jcr.PropertyType;
27  import javax.jcr.RepositoryException;
28  import javax.jcr.Value;
29  import org.modeshape.graph.query.model.Literal;
30  
31  /**
32   * Implementation of the literal value static operand for the JCR Query Object Model and the Graph API.
33   */
34  public class JcrLiteral extends Literal implements javax.jcr.query.qom.Literal, JcrStaticOperand {
35  
36      private static final long serialVersionUID = 1L;
37  
38      private final Value jcrValue;
39  
40      public static Object rawValue( Value value ) throws RepositoryException {
41          switch (value.getType()) {
42              case PropertyType.BINARY:
43                  return value.getBinary();
44              case PropertyType.BOOLEAN:
45                  return value.getBoolean();
46              case PropertyType.DATE:
47                  return value.getDate();
48              case PropertyType.DECIMAL:
49                  return value.getDecimal();
50              case PropertyType.DOUBLE:
51                  return value.getDouble();
52              case PropertyType.LONG:
53                  return value.getLong();
54              case PropertyType.NAME:
55              case PropertyType.PATH:
56              case PropertyType.REFERENCE:
57              case PropertyType.STRING:
58              case PropertyType.URI:
59              case PropertyType.WEAKREFERENCE:
60              default:
61                  return value.getString();
62          }
63      }
64  
65      /**
66       * @param value the JCR value
67       * @throws RepositoryException if there is a problem obtaining the raw value from the supplied JCR value.
68       */
69      public JcrLiteral( Value value ) throws RepositoryException {
70          super(rawValue(value));
71          this.jcrValue = value;
72      }
73  
74      public JcrLiteral( Value value,
75                         Object rawValue ) {
76          super(rawValue);
77          this.jcrValue = value;
78      }
79  
80      /**
81       * {@inheritDoc}
82       * 
83       * @see javax.jcr.query.qom.Literal#getLiteralValue()
84       */
85      @Override
86      public Value getLiteralValue() {
87          return jcrValue;
88      }
89  }