4A Server -  2.0
 All Classes Namespaces Files Functions Variables Enumerator
UpdatableFragment.java
Go to the documentation of this file.
1 /*
2  * Project: Server for annotations sharing
3  * Subproject: Position of fragment of text in XML document
4  * Authors: Michael Angelov
5  * Edited by: Ing. Jaroslav Dytrych
6  * File: UpdatableFragment.java
7  * Description: Class for XML document fragment
8  */
9 
10 /**
11  * @file UpdatableFragment.java
12  *
13  * @brief Class for XML document fragment
14  */
15 
16 package cz.vutbr.fit.knot.annotations.fragmentUpdater;
17 
18 import java.util.ArrayList;
19 import java.util.Arrays;
20 import java.util.Iterator;
21 import javax.xml.xpath.XPathExpression;
22 import javax.xml.xpath.XPathExpressionException;
23 
24 /**
25  * Class for XML document fragment
26  *
27  * @brief Class for XML document fragment
28  * @author Michael Angelov
29  */
30 public class UpdatableFragment {
31 
32  private String XPathString;
34  private int offset;
35  private int length;
36  private String text;
37  private int wordCount;
38 
39  /**
40  * Getter for XPath expression
41  *
42  * @return fragment's XPath expression
43  */
45  return XPathExpression;
46  }
47 
48  /**
49  * Setter for XPath expression
50  *
51  * @param XPathExpression set fragment's XPath expression
52  */
54  this.XPathExpression = XPathExpression;
55  }
56 
57  /**
58  * Getter for string representation of XPath expression
59  *
60  * @return string representation of XPath expression
61  */
62  public String getXPathString() {
63  return XPathString;
64  }
65 
66  /**
67  * Setter for string representation of XPath expression
68  *
69  * @param XPathString string representation of XPath expression
70  */
71  public void setXPathString(String XPathString) {
72  this.XPathString = XPathString;
73  }
74 
75  /**
76  * Getter for length of fragment's string
77  *
78  * @return length of fragment's string
79  */
80  public int getLength() {
81  return length;
82  }
83 
84  /**
85  * Setter for length of fragment's string
86  *
87  * @param length length of fragment's string
88  */
89  public void setLength(int length) {
90  this.length = length;
91  }
92 
93  /**
94  * Getter for offset of fragment's string
95  *
96  * @return offset of fragment's string
97  */
98  public int getOffset() {
99  return offset;
100  }
101 
102  /**
103  * Setter for offset of fragment's string
104  *
105  * @param offset offset of fragment's string
106  */
107  public void setOffset(int offset) {
108  this.offset = offset;
109  }
110 
111  /**
112  * Getter for fragment's string content
113  *
114  * @return fragment's string content
115  */
116  public String getText() {
117  return text;
118  }
119 
120  /**
121  * Setter for fragment's string content
122  *
123  * @param text fragment's string content
124  */
125  public void setText(String text) {
126  this.text = text;
127  }
128 
129  /**
130  * Getter for fragment's string word count
131  *
132  * @return number of words in string with following delimiter:
133  * @verbatim
134  * \s+
135  * @endverbatim
136  */
137  public int getWordCount() {
138  return wordCount;
139  }
140 
141  /**
142  * Constructor
143  *
144  * @param XPathString string representation of XPath
145  * @param offset offset of fragment's string
146  * @param length length of fragment's string
147  * @param text string content of fragment
148  */
149  public UpdatableFragment(String XPathString, int offset, int length, String text) throws XPathExpressionException {
150  this.XPathString = XPathString;
151  this.offset = offset;
152  this.length = length;
153  this.text = text;
154 
155  this.XPathExpression = XPathHelper.XPathStringToExpression(XPathString);
156  if (text != null) {
157  this.wordCount = text.split("\\s+").length;
158  }
159  else {
160  this.wordCount = 0;
161  }
162  }
163 
164  /**
165  * Compares this with other object and returns, whether objects are same type
166  * and have same content.
167  *
168  * @param obj Object to compare with
169  * @return If object is same type and have same content, returns true, false otherwise
170  */
171  @Override
172  public boolean equals(Object obj) {
173  if (obj == null) {
174  return false;
175  }
176  if (getClass() != obj.getClass()) {
177  return false;
178  }
179  final UpdatableFragment other = (UpdatableFragment) obj;
180  if ((this.XPathString == null) ? (other.XPathString != null) : !this.XPathString.equals(other.XPathString)) {
181  return false;
182  }
183  if (this.offset != other.offset) {
184  return false;
185  }
186  if (this.length != other.length) {
187  return false;
188  }
189  if ((this.text == null) ? (other.text != null) : !this.text.equals(other.text)) {
190  return false;
191  }
192  return true;
193  } // equals()
194 
195  /**
196  * Gets hash code of this fragment
197  *
198  * @return Hash code
199  */
200  @Override
201  public int hashCode() {
202  int hash = 7;
203  hash = 47 * hash + (this.XPathString != null ? this.XPathString.hashCode() : 0);
204  hash = 47 * hash + this.offset;
205  hash = 47 * hash + this.length;
206  hash = 47 * hash + (this.text != null ? this.text.hashCode() : 0);
207  return hash;
208  }
209 
210  /**
211  * Overriden toString() method
212  *
213  * @return string representation of class
214  */
215  @Override
216  public String toString() {
217  return "XPath: " + XPathString
218  + "\nOffset: " + offset
219  + "\nLength: " + length
220  + "\nText: " + text;
221  }
222 
223  /**
224  * Uppercase XPath up to /text() (necessary for finding fragment)
225  * and complete fragments of path (e.g. "/DIV/" -> "/DIV[1]/")
226  *
227  * @param path XPath to improve
228  * @return Returns modified XPath
229  */
230  public static String improveXPath(String path) {
231  if (path == null) {
232  return null;
233  }
234  if (path.equals("")) {
235  return path;
236  }
237 
238  // uppercase path without "text()" and all following
239  String newPath = "";
240  int tPos = path.indexOf("text()");
241  if (tPos > 0) {
242  newPath = path.substring(tPos);
243  }
244  if (tPos < 0) {
245  tPos = path.length();
246  }
247  newPath = path.toUpperCase().substring(0, tPos) + newPath;
248 
249  // add [1]
250  String[] splitted = newPath.split("/");
251  newPath = "";
252  ArrayList<String> sList = new ArrayList<String>(Arrays.asList(splitted));
253  Iterator<String> sIt = sList.iterator();
254  String pathFragment = sIt.next(); // first fragment, probably empty
255  if (!pathFragment.isEmpty()) { // if path don't starts with "/"
256  if (pathFragment.endsWith("]")) {
257  newPath = pathFragment;
258  } else {
259  newPath = pathFragment + "[1]";
260  }
261  }
262  for (; sIt.hasNext();) { // for second and all other fragments
263  pathFragment = sIt.next();
264  if (pathFragment.endsWith("]")) {
265  newPath = newPath + "/" + pathFragment;
266  } else {
267  newPath = newPath + "/" + pathFragment + "[1]";
268  }
269  }
270 
271  return newPath;
272  } // improveXPath()
273 } // class UpdatableFragment
UpdatableFragment(String XPathString, int offset, int length, String text)