Given xml representation of HIT converts them into an array of hits @throws IOException
(String xml)
| 228 | * @throws IOException |
| 229 | */ |
| 230 | public static Hit[] fromXML(String xml) throws IOException, JDOMException { |
| 231 | SAXBuilder saxBuilder = new SAXBuilder(false); |
| 232 | org.jdom.Document jdomDoc = saxBuilder.build(new StringReader(xml)); |
| 233 | Element rootElement = jdomDoc.getRootElement(); |
| 234 | if(!rootElement.getName().equalsIgnoreCase(HITS)) { |
| 235 | throw new IOException("Root element must be " + HITS); |
| 236 | } |
| 237 | |
| 238 | // rootElement is HITS |
| 239 | // this will internally contains instances of HIT |
| 240 | List<?> hitsChildren = rootElement.getChildren(HIT); |
| 241 | Hit[] hits = new Hit[hitsChildren.size()]; |
| 242 | |
| 243 | for(int i = 0; i < hitsChildren.size(); i++) { |
| 244 | Element hitElem = (Element)hitsChildren.get(i); |
| 245 | int startOffset = Integer.parseInt(hitElem.getChildText(START_OFFSET)); |
| 246 | int endOffset = Integer.parseInt(hitElem.getChildText(END_OFFSET)); |
| 247 | String docID = hitElem.getChildText(DOC_ID); |
| 248 | String annotationSetName = hitElem.getChildText(ANNOTATION_SET_NAME); |
| 249 | String queryString = hitElem.getChildText(QUERY); |
| 250 | |
| 251 | Element patternAnnotations = hitElem.getChild(PATTERN_ANNOTATIONS); |
| 252 | if(patternAnnotations == null) { |
| 253 | hits[i] = new Hit(docID, annotationSetName, startOffset, endOffset, queryString); |
| 254 | continue; |
| 255 | } |
| 256 | |
| 257 | List<?> patAnnots = patternAnnotations.getChildren(PATTERN_ANNOTATION); |
| 258 | List<PatternAnnotation> patAnnotsList = new ArrayList<PatternAnnotation>(); |
| 259 | for(int j = 0; j < patAnnots.size(); j++) { |
| 260 | Element patAnnot = (Element)patAnnots.get(j); |
| 261 | PatternAnnotation pa = new PatternAnnotation(); |
| 262 | pa.setStOffset(Integer.parseInt(patAnnot.getChildText(START))); |
| 263 | pa.setEnOffset(Integer.parseInt(patAnnot.getChildText(END))); |
| 264 | pa.setPosition(Integer.parseInt(patAnnot.getChildText(POSITION))); |
| 265 | pa.setText(patAnnot.getChildText(TEXT)); |
| 266 | pa.setType(patAnnot.getChildText(TYPE)); |
| 267 | |
| 268 | // we need to find out its features |
| 269 | Element featuresElem = patAnnot.getChild(FEATURES); |
| 270 | // more than one features possible |
| 271 | List<?> featuresElemsList = featuresElem.getChildren(FEATURE); |
| 272 | for(int k = 0; k < featuresElemsList.size(); k++) { |
| 273 | Element featureElem = (Element)featuresElemsList.get(k); |
| 274 | String key = featureElem.getChildText(KEY); |
| 275 | String value = featureElem.getChildText(VALUE); |
| 276 | pa.addFeature(key, value); |
| 277 | } |
| 278 | patAnnotsList.add(pa); |
| 279 | } |
| 280 | |
| 281 | String patternText = hitElem.getChildText(PATTERN_TEXT); |
| 282 | int leftCSO = Integer.parseInt(hitElem |
| 283 | .getChildText(LEFT_CONTEXT_START_OFFSET)); |
| 284 | int rightCEO = Integer.parseInt(hitElem |
| 285 | .getChildText(RIGHT_CONTEXT_END_OFFSET)); |
| 286 | |
| 287 | hits[i] = new Pattern(docID, annotationSetName, patternText, startOffset, endOffset, |