01 /*
02 * Copyright (c) 1995-2010, The University of Sheffield. See the file
03 * COPYRIGHT.txt in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt
04 *
05 * This file is part of GATE (see http://gate.ac.uk/), and is free
06 * software, licenced under the GNU Library General Public License,
07 * Version 2, June 1991 (in the distribution as file licence.html,
08 * and also available at http://gate.ac.uk/gate/licence.html).
09 *
10 * Eric Sword, 09/03/08
11 *
12 * $Id$
13 */
14 package gate.jape.constraint;
15
16 import gate.*;
17
18 /**
19 * Accessor that returns the underlying string of an annotation in a document.
20 *
21 * @version $Revision$
22 * @author esword
23 */
24 public class StringAccessor extends MetaPropertyAccessor {
25 /**
26 * Return the underlying string for the annotation. Context
27 * must be a {@link Document} or an {@link AnnotationSet} which
28 * points to the document.
29 */
30 public Object getValue(Annotation annot, AnnotationSet context) {
31 if(annot == null) return null;
32
33 Document doc = context.getDocument();
34
35 String retVal = doc.getContent().toString().substring(
36 annot.getStartNode().getOffset().intValue(),
37 annot.getEndNode().getOffset().intValue());
38 if (retVal == null)
39 return "";
40 return retVal;
41 }
42
43 /**
44 * Always returns "string", the name of the meta-property which this
45 * accessor provides.
46 */
47 @Override
48 public Object getKey() {
49 return "string";
50 }
51
52 }
|