use of gate.util.InvalidOffsetException in project gate-core by GateNLP.
the class AnnotationEditor method initListeners.
protected void initListeners() {
// resize the window when the table changes.
featuresEditor.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
// the table has changed size -> resize the window too!
popupWindow.pack();
}
});
KeyAdapter keyAdapter = new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
hideTimer.stop();
}
};
typeCombo.getEditor().getEditorComponent().addKeyListener(keyAdapter);
MouseListener windowMouseListener = new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent evt) {
hideTimer.stop();
}
// allow a JWindow to be dragged with a mouse
@Override
public void mousePressed(MouseEvent me) {
pressed = me;
}
};
MouseMotionListener windowMouseMotionListener = new MouseMotionAdapter() {
Point location;
// allow a JWindow to be dragged with a mouse
@Override
public void mouseDragged(MouseEvent me) {
location = popupWindow.getLocation(location);
int x = location.x - pressed.getX() + me.getX();
int y = location.y - pressed.getY() + me.getY();
popupWindow.setLocation(x, y);
pinnedButton.setSelected(true);
}
};
popupWindow.getRootPane().addMouseListener(windowMouseListener);
popupWindow.getRootPane().addMouseMotionListener(windowMouseMotionListener);
InputMap inputMap = ((JComponent) popupWindow.getContentPane()).getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
actionMap = ((JComponent) popupWindow.getContentPane()).getActionMap();
// add the key-action bindings of this Component to the parent window
solAction = new StartOffsetLeftAction("", MainFrame.getIcon("extend-left"), SOL_DESC, KeyEvent.VK_LEFT);
solButton.setAction(solAction);
setShortCuts(inputMap, SOL_KEY_STROKES, "solAction");
actionMap.put("solAction", solAction);
sorAction = new StartOffsetRightAction("", MainFrame.getIcon("extend-right"), SOR_DESC, KeyEvent.VK_RIGHT);
sorButton.setAction(sorAction);
setShortCuts(inputMap, SOR_KEY_STROKES, "sorAction");
actionMap.put("sorAction", sorAction);
delAction = new DeleteAnnotationAction("", MainFrame.getIcon("remove-annotation"), "Delete the annotation", KeyEvent.VK_DELETE);
delButton.setAction(delAction);
inputMap.put(KeyStroke.getKeyStroke("alt DELETE"), "delAction");
actionMap.put("delAction", delAction);
eolAction = new EndOffsetLeftAction("", MainFrame.getIcon("extend-left"), EOL_DESC, KeyEvent.VK_LEFT);
eolButton.setAction(eolAction);
setShortCuts(inputMap, EOL_KEY_STROKES, "eolAction");
actionMap.put("eolAction", eolAction);
eorAction = new EndOffsetRightAction("", MainFrame.getIcon("extend-right"), EOR_DESC, KeyEvent.VK_RIGHT);
eorButton.setAction(eorAction);
setShortCuts(inputMap, EOR_KEY_STROKES, "eorAction");
actionMap.put("eorAction", eorAction);
pinnedButton.setToolTipText("<html>Press to pin window in place" + " <font color=#667799><small>Ctrl-P" + " </small></font></html>");
inputMap.put(KeyStroke.getKeyStroke("control P"), "toggle pin");
actionMap.put("toggle pin", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
pinnedButton.doClick();
}
});
DismissAction dismissAction = new DismissAction("", null, "Close the window", KeyEvent.VK_ESCAPE);
dismissButton.setAction(dismissAction);
inputMap.put(KeyStroke.getKeyStroke("ESCAPE"), "dismissAction");
inputMap.put(KeyStroke.getKeyStroke("alt ESCAPE"), "dismissAction");
actionMap.put("dismissAction", dismissAction);
ApplyAction applyAction = new ApplyAction("Apply", null, "", KeyEvent.VK_ENTER);
inputMap.put(KeyStroke.getKeyStroke("alt ENTER"), "applyAction");
actionMap.put("applyAction", applyAction);
typeCombo.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
String newType = typeCombo.getSelectedItem().toString();
if (ann == null || ann.getType().equals(newType))
return;
// annotation editing
Integer oldId = ann.getId();
Annotation oldAnn = ann;
set.remove(ann);
try {
set.add(oldId, oldAnn.getStartNode().getOffset(), oldAnn.getEndNode().getOffset(), newType, oldAnn.getFeatures());
Annotation newAnn = set.get(oldId);
// update the selection to the new annotation
getOwner().selectAnnotation(new AnnotationDataImpl(set, newAnn));
editAnnotation(newAnn, set);
owner.annotationChanged(newAnn, set, oldAnn.getType());
} catch (InvalidOffsetException ioe) {
throw new GateRuntimeException(ioe);
}
}
});
hideTimer = new Timer(HIDE_DELAY, new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
annotationEditorInstance.setVisible(false);
}
});
hideTimer.setRepeats(false);
AncestorListener textAncestorListener = new AncestorListener() {
@Override
public void ancestorAdded(AncestorEvent event) {
if (wasShowing) {
annotationEditorInstance.setVisible(true);
}
wasShowing = false;
}
@Override
public void ancestorRemoved(AncestorEvent event) {
if (isShowing()) {
wasShowing = true;
popupWindow.dispose();
}
}
@Override
public void ancestorMoved(AncestorEvent event) {
}
private boolean wasShowing = false;
};
owner.getTextComponent().addAncestorListener(textAncestorListener);
}
use of gate.util.InvalidOffsetException in project gate-core by GateNLP.
the class DocumentStaxUtils method readAnnotationSet.
/**
* Processes an AnnotationSet element from the given reader and fills
* the given annotation set with the corresponding annotations. The
* reader must initially be positioned on the starting AnnotationSet
* tag and will be left positioned on the correspnding closing tag.
*
* @param xsr the reader
* @param annotationSet the annotation set to fill.
* @param nodeIdToOffsetMap a map mapping node IDs (Integer) to their
* offsets in the text (Long). If null, we assume that the
* node ids and offsets are the same (useful if parsing an
* annotation set in isolation).
* @param allAnnotIds a set to contain all annotation IDs specified in
* the annotation set. It should initially be empty and will
* be updated if any of the annotations in this set specify
* an ID.
* @param requireAnnotationIds whether annotations are required to
* specify their IDs. If true, it is an error for an
* annotation to omit the Id attribute. If false, it is an
* error for the Id to be present. If null, we have not yet
* determined what style of XML this is.
* @return <code>requireAnnotationIds</code>. If the passed in
* value was null, and we have since determined what it should
* be, the updated value is returned.
* @throws XMLStreamException
*/
public static Boolean readAnnotationSet(XMLStreamReader xsr, AnnotationSet annotationSet, Map<Integer, Long> nodeIdToOffsetMap, Set<Integer> allAnnotIds, Boolean requireAnnotationIds) throws XMLStreamException {
List<AnnotationObject> collectedAnnots = new ArrayList<AnnotationObject>();
while (xsr.nextTag() == XMLStreamConstants.START_ELEMENT) {
xsr.require(XMLStreamConstants.START_ELEMENT, null, "Annotation");
AnnotationObject annObj = new AnnotationObject();
annObj.setElemName(xsr.getAttributeValue(null, "Type"));
try {
int startNodeId = Integer.parseInt(xsr.getAttributeValue(null, "StartNode"));
if (nodeIdToOffsetMap != null) {
Long startOffset = nodeIdToOffsetMap.get(startNodeId);
if (startOffset != null) {
annObj.setStart(startOffset);
} else {
throw new XMLStreamException("Invalid start node ID", xsr.getLocation());
}
} else {
// no offset map, so just use the ID as an offset
annObj.setStart(Long.valueOf(startNodeId));
}
} catch (NumberFormatException nfe) {
throw new XMLStreamException("Non-integer value found for StartNode", xsr.getLocation());
}
try {
int endNodeId = Integer.parseInt(xsr.getAttributeValue(null, "EndNode"));
if (nodeIdToOffsetMap != null) {
Long endOffset = nodeIdToOffsetMap.get(endNodeId);
if (endOffset != null) {
annObj.setEnd(endOffset);
} else {
throw new XMLStreamException("Invalid end node ID", xsr.getLocation());
}
} else {
// no offset map, so just use the ID as an offset
annObj.setEnd(Long.valueOf(endNodeId));
}
} catch (NumberFormatException nfe) {
throw new XMLStreamException("Non-integer value found for EndNode", xsr.getLocation());
}
String annotIdString = xsr.getAttributeValue(null, "Id");
if (annotIdString == null) {
if (requireAnnotationIds == null) {
// if one annotation doesn't specify Id than all must
requireAnnotationIds = Boolean.FALSE;
} else {
if (requireAnnotationIds.booleanValue()) {
// if we were expecting an Id but didn't get one...
throw new XMLStreamException("New style GATE XML format requires that every annotation " + "specify its Id, but an annotation with no Id was found", xsr.getLocation());
}
}
} else {
// we have an ID attribute
if (requireAnnotationIds == null) {
// if one annotation specifies an Id then all must
requireAnnotationIds = Boolean.TRUE;
} else {
if (!requireAnnotationIds.booleanValue()) {
// if we were expecting not to have an Id but got one...
throw new XMLStreamException("Old style GATE XML format requires that no annotation " + "specifies its Id, but an annotation with an Id was found", xsr.getLocation());
}
}
try {
Integer annotationId = Integer.valueOf(annotIdString);
if (allAnnotIds.contains(annotationId)) {
throw new XMLStreamException("Annotation IDs must be unique " + "within an annotation set. Found duplicate ID", xsr.getLocation());
}
allAnnotIds.add(annotationId);
annObj.setId(annotationId);
} catch (NumberFormatException nfe) {
throw new XMLStreamException("Non-integer annotation ID found", xsr.getLocation());
}
}
// get the features of this annotation
annObj.setFM(readFeatureMap(xsr));
// readFeatureMap leaves xsr on the </Annotation> tag
collectedAnnots.add(annObj);
}
// now process all found annotations.to add to the set
Iterator<AnnotationObject> collectedAnnotsIt = collectedAnnots.iterator();
while (collectedAnnotsIt.hasNext()) {
AnnotationObject annObj = collectedAnnotsIt.next();
try {
if (annObj.getId() != null) {
annotationSet.add(annObj.getId(), annObj.getStart(), annObj.getEnd(), annObj.getElemName(), annObj.getFM());
} else {
annotationSet.add(annObj.getStart(), annObj.getEnd(), annObj.getElemName(), annObj.getFM());
}
} catch (InvalidOffsetException ioe) {
// to offset map
throw new XMLStreamException("Invalid offset when creating annotation " + annObj, ioe);
}
}
return requireAnnotationIds;
}
use of gate.util.InvalidOffsetException in project gate-core by GateNLP.
the class EmailDocumentFormat method unpackMarkup.
/**
* Unpack the markup in the document. This converts markup from the
* native format (e.g. EMAIL) into annotations in GATE format.
* Uses the markupElementsMap to determine which elements to convert, and
* what annotation type names to use.
* It always tryes to parse te doc's content. It doesn't matter if the
* sourceUrl is null or not.
*
* @param doc The gate document you want to parse.
*/
@Override
public void unpackMarkup(gate.Document doc) throws DocumentFormatException {
if ((doc == null) || (doc.getSourceUrl() == null && doc.getContent() == null)) {
throw new DocumentFormatException("GATE document is null or no content found. Nothing to parse!");
}
// End if
setNewLineProperty(doc);
// create an EmailDocumentHandler
EmailDocumentHandler emailDocHandler = null;
emailDocHandler = new gate.email.EmailDocumentHandler(doc, this.markupElementsMap, this.element2StringMap);
StatusListener statusListener = new StatusListener() {
@Override
public void statusChanged(String text) {
// this is implemented in DocumentFormat.java and inherited here
fireStatusChanged(text);
}
};
// Register a status listener with it
emailDocHandler.addStatusListener(statusListener);
try {
// Call the method that creates annotations on the gate document
emailDocHandler.annotateMessages();
// Process the body annotations and search for paragraphs
AnnotationSet bodyAnnotations = doc.getAnnotations(GateConstants.ORIGINAL_MARKUPS_ANNOT_SET_NAME).get("body");
if (bodyAnnotations != null && !bodyAnnotations.isEmpty()) {
Iterator<Annotation> iter = bodyAnnotations.iterator();
while (iter.hasNext()) {
Annotation a = iter.next();
annotateParagraphs(doc, a.getStartNode().getOffset().intValue(), a.getEndNode().getOffset().intValue(), GateConstants.ORIGINAL_MARKUPS_ANNOT_SET_NAME);
}
// End while
}
// End if
} catch (IOException e) {
throw new DocumentFormatException("Couldn't create a buffered reader ", e);
} catch (InvalidOffsetException e) {
throw new DocumentFormatException(e);
} finally {
emailDocHandler.removeStatusListener(statusListener);
}
// End try
}
use of gate.util.InvalidOffsetException in project gate-core by GateNLP.
the class UimaDocumentFormat method unpackCasMarkup.
/**
* Convert UIMA CAS markups to GATE markups.
* @param doc XML document already parsed
* @throws DocumentFormatException error when parsing the file
*/
private void unpackCasMarkup(Document doc) throws DocumentFormatException {
AnnotationSet inputAS = doc.getAnnotations("Original markups");
AnnotationSet outputAS = doc.getAnnotations("Original markups");
// set format specific names
String casPrefix;
String idName;
if (!inputAS.get("CAS").isEmpty()) {
casPrefix = "uima.cas.";
idName = "_id";
} else if (!inputAS.get("xmi:XMI").isEmpty()) {
casPrefix = "cas:";
idName = "xmi:id";
} else {
throw new DocumentFormatException("The document \"" + doc.getName() + "\" is neither of XCAS nor XMICAS format.");
}
// get array/list contained elements annotations
for (Annotation annotation : inputAS) {
if (annotation.getType().matches(casPrefix + "[a-zA-Z]+(List|Array)")) {
try {
String elements = doc.getContent().getContent(annotation.getStartNode().getOffset(), annotation.getEndNode().getOffset()).toString();
// add contained values as a feature to the array annotation
if (!elements.trim().equals("")) {
annotation.getFeatures().put("elements", elements);
}
} catch (InvalidOffsetException e) {
throw new DocumentFormatException(e);
}
}
}
// get document content from SOFA annotations
Set<Annotation> sofaSet = inputAS.get(casPrefix + "Sofa");
if (sofaSet.size() > 1) {
Out.prln("More than one UIMA SOFA, annotation offsets won't be correct.");
}
StringBuilder documentContent = new StringBuilder();
for (Annotation annotation : sofaSet) {
documentContent.append((String) annotation.getFeatures().get("sofaString"));
}
doc.setContent(new DocumentContentImpl(documentContent.toString()));
// remove SOFA annotations
inputAS.removeAll(sofaSet);
// remove non document annotations
inputAS.removeAll(inputAS.get("CAS"));
inputAS.removeAll(inputAS.get("xmi:XMI"));
inputAS.removeAll(inputAS.get("cas:NULL"));
// get the views members, views will be added later as annotation sets
List<List<String>> viewList = new ArrayList<List<String>>();
for (Annotation view : inputAS.get(casPrefix + "View")) {
viewList.add(Arrays.asList(((String) view.getFeatures().get("members")).split("\\s+")));
}
inputAS.removeAll(inputAS.get(casPrefix + "View"));
// fill a map with the id as key and the entity name as value
// this is specific to the Temis Luxid CAS format
Map<String, String> entityMap = new HashMap<String, String>();
for (Annotation entity : inputAS.get("com.temis.uima.Entity")) {
FeatureMap features = entity.getFeatures();
entityMap.put((String) features.get(idName), (String) features.get("value"));
}
try {
// for each UIMA annotation
for (Annotation annotation : new HashSet<Annotation>(inputAS)) {
FeatureMap features = Factory.newFeatureMap();
features.putAll(annotation.getFeatures());
String start = (String) features.get("begin");
String end = (String) features.get("end");
String id = (String) features.get(idName);
// UIMA feature
features.remove("begin");
// UIMA feature
features.remove("end");
// GATE feature
features.remove("isEmptyAndSpan");
// UIMA XCAS feature
features.remove("_indexed");
if (start == null || end == null) {
// no offsets so add it as a GATE document feature
features.remove(idName);
for (Map.Entry<Object, Object> entry : features.entrySet()) {
doc.getFeatures().put(annotation.getType() + '_' + id + '.' + entry.getKey(), entry.getValue());
}
} else {
// offsets so add it as a GATE document annotation
String entityReference = (String) features.get("_ref_entity");
String type = entityMap.containsKey(entityReference) ? entityMap.get(entityReference) : annotation.getType();
Integer gateId = outputAS.add(Long.valueOf(start), Long.valueOf(end), type, features);
int viewCount = 0;
for (List<String> viewMembers : viewList) {
if (viewMembers.contains(id)) {
// add the annotation to the annotation set
doc.getAnnotations("CasView" + viewCount).add(outputAS.get(gateId));
}
viewCount++;
}
}
// delete UIMA annotation
inputAS.remove(annotation);
}
} catch (InvalidOffsetException e) {
throw new DocumentFormatException("Couldn't create annotation.", e);
}
}
use of gate.util.InvalidOffsetException in project gate-core by GateNLP.
the class InlineXMLExporter method export.
@Override
public void export(Document doc, OutputStream out, FeatureMap options) throws IOException {
Integer rootID = null;
AnnotationSet withRoot = null;
AnnotationSet originalMarkups = null;
AnnotationSet backupOriginalMarkups = null;
try {
AnnotationSet allAnnots = doc.getAnnotations((String) options.get("annotationSetName"));
if (!(Boolean) options.get("includeOriginalMarkups")) {
originalMarkups = doc.getAnnotations(GateConstants.ORIGINAL_MARKUPS_ANNOT_SET_NAME);
backupOriginalMarkups = new AnnotationSetImpl(originalMarkups);
originalMarkups.clear();
}
// first transfer the annotation types from a list to a set
@SuppressWarnings("unchecked") Set<String> types2Export = new HashSet<String>((List<String>) options.get("annotationTypes"));
// then get the annotations for export
AnnotationSet annots2Export = allAnnots.get(types2Export);
withRoot = new AnnotationSetImpl(doc);
withRoot.addAll(annots2Export);
String rootType = (String) options.get("rootElement");
if (rootType != null && !"".equals(rootType)) {
// add the root element to the set
rootID = withRoot.add(0L, doc.getContent().size(), (String) options.get("rootElement"), Factory.newFeatureMap());
}
// create a writer using the specified encoding
OutputStreamWriter writer = new OutputStreamWriter(out, (String) options.get("encoding"));
// write the document
writer.write(doc.toXml(withRoot, (Boolean) options.get("includeFeatures")));
// make sure it gets written
writer.flush();
} catch (InvalidOffsetException e) {
throw new IOException(e);
} finally {
// delete the fake root element
if (rootID != null)
withRoot.remove(withRoot.get(rootID));
// restore the original markups
if (backupOriginalMarkups != null)
originalMarkups.addAll(backupOriginalMarkups);
}
}
Aggregations