4A Server -  2.0
 All Classes Namespaces Files Functions Variables Enumerator
TextModification.java
Go to the documentation of this file.
1 /*
2  * Project: Server for annotations sharing
3  * Author: Ing. Jaroslav Dytrych idytrych@fit.vutbr.cz
4  * File: TextModification.java
5  * Description: Class representing modification of annotated document text
6  */
7 
8 /**
9  * @file TextModification.java
10  *
11  * @brief Class representing modification of annotated document text
12  */
13 
14 package cz.vutbr.fit.knot.annotations.app;
15 
16 /**
17  * Class representing modification of annotated document text
18  *
19  * @brief Class representing modification of annotated document text
20  * @author idytrych
21  */
22 public class TextModification {
23  /**
24  * Class for throwing exceptions which are directly related to some text
25  * modification.
26  *
27  * @brief Class for throwing exceptions which are directly related to some text modification.
28  */
29  public static class TextModificationException extends Exception {
30  /** Related text modification */
32 
33  /** Constructor */
35 
36  /**
37  * Constructor
38  *
39  * @param msg Message
40  */
41  public TextModificationException (String msg) {
42  this(msg, null);
43  }
44 
45  /**
46  * Constructor
47  *
48  * @param msg Message
49  * @param tm Related text modification
50  */
51  public TextModificationException (String msg, TextModification tm) {
52  super(msg);
53  _tm = tm;
54  }
55 
56  /**
57  * Gets related text modification
58  *
59  * @return Returns related text modification
60  */
62  return _tm;
63  }
64  }; // public static class TextModificationException
65 
66  /** XPath of element in which modification was occurred */
67  private String path;
68  /** Offset of replaced text fragment */
69  private Integer offset;
70  /** Length of replaced text fragment */
71  private Integer length;
72  /** New content of fragment */
73  private String newContent;
74  /** Mode of text modification */
75  private Integer mode;
76 
77  /**
78  * Constructor with default mode (replace)
79  * If offset is null, whole content of element was replaced.
80  *
81  * @param path XPath of element in which modification was occurred
82  * @param offset Offset of replaced text fragment in given element
83  * @param length Length of replaced text fragment
84  * @param newContent New content of fragment
85  */
86  public TextModification(String path, Integer offset, Integer length, String newContent) {
87  this.path = path;
88  this.offset = offset;
89  this.length = length;
90  this.newContent = newContent;
91  this.mode = Constants.TEXT_MOD_REPLACE;
92  }
93 
94  /**
95  * Constructor
96  * If offset is null, whole content of element was replaced.
97  *
98  * @param path XPath of element in which modification was occurred
99  * @param offset Offset of replaced text fragment in given element
100  * @param length Length of replaced text fragment
101  * @param newContent New content of fragment
102  * @param mode Mode of text modification
103  */
104  public TextModification(String path, Integer offset, Integer length, String newContent, Integer mode) {
105  this.path = path;
106  this.offset = offset;
107  this.length = length;
108  this.newContent = newContent;
109  this.mode = mode;
110  }
111 
112  /**
113  * Constructor
114  *
115  * @param path XPath of element in which modification was occurred
116  * @param newContent New content of fragment
117  * @param mode Mode of text modification
118  */
119  public TextModification(String path, String newContent, Integer mode) {
120  this.path = path;
121  this.newContent = newContent;
122  this.mode = mode;
123  }
124 
125  /**
126  * Gets length of replaced text fragment
127  *
128  * @return Returns length of replaced text fragment
129  */
130  public Integer getLength() {
131  return length;
132  }
133 
134  /**
135  * Sets length of replaced text fragment
136  *
137  * @param length Length of replaced text fragment
138  */
139  public void setLength(Integer length) {
140  this.length = length;
141  }
142 
143  /**
144  * Gets new content of document fragment
145  *
146  * @return Returns length of replaced text fragment
147  */
148  public String getNewContent() {
149  return newContent;
150  }
151 
152  /**
153  * Sets new content of document fragment
154  *
155  * @param newContent New content of document fragment
156  */
157  public void setNewContent(String newContent) {
158  this.newContent = newContent;
159  }
160 
161  /**
162  * Gets offset of replaced text fragment
163  *
164  * @return Returns offset of replaced text fragment
165  */
166  public Integer getOffset() {
167  return offset;
168  }
169 
170  /**
171  * Sets offset of replaced text fragment
172  *
173  * @param offset Offset of replaced text fragment
174  */
175  public void setOffset(Integer offset) {
176  this.offset = offset;
177  }
178 
179  /**
180  * Gets XPath of element in which modification was occurred
181  *
182  * @return Returns XPath of element in which modification was occurred
183  */
184  public String getPath() {
185  return path;
186  }
187 
188  /**
189  * Sets XPath of element in which modification was occurred
190  *
191  * @param path XPath of element in which modification was occurred
192  */
193  public void setPath(String path) {
194  this.path = path;
195  }
196 
197  /**
198  * Gets mode of text modification
199  *
200  * @return Returns mode of text modification replace/insertBefore/insertAfter
201  */
202  public Integer getMode() {
203  return mode;
204  }
205 
206  /**
207  * Sets mode of text modification
208  *
209  * @return Returns mode of text modification replace/insertBefore/insertAfter
210  */
211  public void setMode(Integer mode) {
212  this.mode = mode;
213  }
214 
215  @Override
216  public String toString() {
217  return "TextModification{" + "path=" + path + "offset=" + offset + "length=" + length + "newContent=" + newContent + '}';
218  }
219 
220  /**
221  * Returns serialized informations about text modification in XML for protocol 1.x
222  *
223  * @return Returns serialized informations about text modification in XML
224  */
225  public String toXMLString() {
226  String pathAttr = (path != null)? " path=\"" + path + "\"" : "";
227  String offsetAttr = (offset != null)? " offset=\"" + offset + "\"" : "";
228  String lengthAttr = (length != null)? " length=\"" + length + "\"" : "";
229 
230  // TODO: if mode != Constants.TEXT_MOD_REPLACE it will not work correctly
231  // It is necessary to transform such modification to supported mode
232  // and serialize in this mode.
233 
234  String resStr = "<textModification" + pathAttr;
235 
236  if (offset != null) { // modification of the part of node (not whole)
237  resStr += offsetAttr;
238  resStr += lengthAttr;
239  }
240 
241  if (newContent == null || newContent.isEmpty()) { // no new content == delete modification
242  resStr += " />";
243  } else { // new content == insert modification
244  resStr += ">";
245  resStr += "<![CDATA[";
246  resStr += newContent;
247  resStr += "]]>";
248  resStr += "</textModification>";
249  }
250 
251  return resStr;
252  }
253 
254  /**
255  * Returns serialized informations about text modification in XML for protocol V2
256  *
257  * @return Returns serialized informations about text modification in XML for protocol V2
258  */
259  public String toXMLStringV2(){
260  String resStr = "";
261  String pathStr = (path != null) ? "path=\"" + path + "\"" : "";
262  String contentStr = "<![CDATA[";
263 
264  if(newContent != null){
265  contentStr += newContent;
266  }
267  contentStr += "]]>";
268 
270  resStr = "<replace " + pathStr;
271  resStr += (offset != null) ? " offset=\"" + offset + "\"" : "";
272  resStr += (length != null) ? " length=\"" + length + "\"" : "";
273  resStr += ">" + contentStr + "</replace>";
274  }
275  else if(mode == Constants.TEXT_MOD_INS_BEFORE){
276  resStr = "<insertBefore " + pathStr + ">" + contentStr + "</insertBefore>";
277  }
278  else if(mode == Constants.TEXT_MOD_INS_AFTER){
279  resStr = "<insertAfter " + pathStr + ">" + contentStr + "</insertAfter>";
280  }
281 
282  return resStr;
283  }
284 
285 } // class TextModification
Class representing modification of annotated document text.
TextModification(String path, String newContent, Integer mode)
TextModification(String path, Integer offset, Integer length, String newContent, Integer mode)
Class for throwing exceptions which are directly related to some text modification.
TextModification(String path, Integer offset, Integer length, String newContent)