Author: joern
Date: Sat Oct 2 15:27:30 2010
New Revision: 1003813
URL: http://svn.apache.org/viewvc?rev=1003813&view=rev
Log:
UIMA-1887 Added support to open a CAS file outside of a Cas Editor Project. Its
simply assumed that the type system is located at the project root directory
and is named TypeSystem.xml.
Modified:
uima/uimaj/trunk/uimaj-ep-cas-editor/src/main/java/org/apache/uima/caseditor/core/model/TypesystemElement.java
uima/uimaj/trunk/uimaj-ep-cas-editor/src/main/java/org/apache/uima/caseditor/editor/DefaultCasDocumentProvider.java
uima/uimaj/trunk/uimaj-ep-cas-editor/src/main/java/org/apache/uima/caseditor/editor/DocumentUimaImpl.java
Modified:
uima/uimaj/trunk/uimaj-ep-cas-editor/src/main/java/org/apache/uima/caseditor/core/model/TypesystemElement.java
URL:
http://svn.apache.org/viewvc/uima/uimaj/trunk/uimaj-ep-cas-editor/src/main/java/org/apache/uima/caseditor/core/model/TypesystemElement.java?rev=1003813&r1=1003812&r2=1003813&view=diff
==============================================================================
---
uima/uimaj/trunk/uimaj-ep-cas-editor/src/main/java/org/apache/uima/caseditor/core/model/TypesystemElement.java
(original)
+++
uima/uimaj/trunk/uimaj-ep-cas-editor/src/main/java/org/apache/uima/caseditor/core/model/TypesystemElement.java
Sat Oct 2 15:27:30 2010
@@ -29,6 +29,7 @@ import org.apache.uima.cas.TypeSystem;
import org.apache.uima.caseditor.CasEditorPlugin;
import org.apache.uima.caseditor.core.model.delta.INlpElementDelta;
import org.apache.uima.caseditor.core.util.MarkerUtil;
+import org.apache.uima.caseditor.editor.DocumentUimaImpl;
import org.apache.uima.resource.ResourceInitializationException;
import org.apache.uima.resource.metadata.FsIndexDescription;
import org.apache.uima.resource.metadata.TypePriorities;
@@ -89,7 +90,7 @@ public class TypesystemElement extends A
((NlpModel) mProject.getParent()).asyncExcuteQueue(clearMarkers);
try {
- return getCASInternal();
+ return DocumentUimaImpl.getVirginCAS(mTypesytemFile);
} catch (final CoreException e) {
Runnable createMarker = new Runnable() {
public void run() {
@@ -106,60 +107,6 @@ public class TypesystemElement extends A
}
}
- private CAS getCASInternal() throws CoreException {
- ResourceSpecifierFactory resourceSpecifierFactory =
UIMAFramework.getResourceSpecifierFactory();
-
- IFile extensionTypeSystemFile = mTypesytemFile;
-
- InputStream inTypeSystem;
-
- if (extensionTypeSystemFile != null && extensionTypeSystemFile.exists()) {
- inTypeSystem = extensionTypeSystemFile.getContents();
- } else {
- return null;
- }
-
- XMLInputSource xmlTypeSystemSource = new XMLInputSource(inTypeSystem, new
File(""));
-
- XMLParser xmlParser = UIMAFramework.getXMLParser();
-
- TypeSystemDescription typeSystemDesciptor;
-
- try {
- typeSystemDesciptor = (TypeSystemDescription)
xmlParser.parse(xmlTypeSystemSource);
-
- typeSystemDesciptor.resolveImports();
- } catch (InvalidXMLException e) {
-
- String message = e.getMessage() != null ? e.getMessage() : "";
-
- IStatus s = new Status(IStatus.ERROR, CasEditorPlugin.ID, IStatus.OK,
message, e);
-
- throw new CoreException(s);
- }
-
- TypePriorities typePriorities =
resourceSpecifierFactory.createTypePriorities();
-
- FsIndexDescription indexDesciptor = new FsIndexDescription_impl();
- indexDesciptor.setLabel("TOPIndex");
- indexDesciptor.setTypeName("uima.cas.TOP");
- indexDesciptor.setKind(FsIndexDescription.KIND_SORTED);
-
- CAS cas;
- try {
- cas = CasCreationUtils.createCas(typeSystemDesciptor, typePriorities,
- new FsIndexDescription[] { indexDesciptor });
- } catch (ResourceInitializationException e) {
- String message = e.getMessage() != null ? e.getMessage() : "";
-
- IStatus s = new Status(IStatus.ERROR, CasEditorPlugin.ID, IStatus.OK,
message, e);
-
- throw new CoreException(s);
- }
-
- return cas;
- }
-
/**
* Retrieves the name.
*/
Modified:
uima/uimaj/trunk/uimaj-ep-cas-editor/src/main/java/org/apache/uima/caseditor/editor/DefaultCasDocumentProvider.java
URL:
http://svn.apache.org/viewvc/uima/uimaj/trunk/uimaj-ep-cas-editor/src/main/java/org/apache/uima/caseditor/editor/DefaultCasDocumentProvider.java?rev=1003813&r1=1003812&r2=1003813&view=diff
==============================================================================
---
uima/uimaj/trunk/uimaj-ep-cas-editor/src/main/java/org/apache/uima/caseditor/editor/DefaultCasDocumentProvider.java
(original)
+++
uima/uimaj/trunk/uimaj-ep-cas-editor/src/main/java/org/apache/uima/caseditor/editor/DefaultCasDocumentProvider.java
Sat Oct 2 15:27:30 2010
@@ -19,16 +19,29 @@
package org.apache.uima.caseditor.editor;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+import org.apache.uima.cas.CAS;
import org.apache.uima.cas.Type;
+import org.apache.uima.cas.TypeSystem;
import org.apache.uima.caseditor.CasEditorPlugin;
+import org.apache.uima.caseditor.core.model.DefaultColors;
import org.apache.uima.caseditor.core.model.DocumentElement;
import org.apache.uima.caseditor.core.model.INlpElement;
+import org.apache.uima.caseditor.core.model.dotcorpus.DotCorpus;
+import org.apache.uima.caseditor.core.model.dotcorpus.DotCorpusSerializer;
import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.text.IDocument;
import org.eclipse.ui.part.FileEditorInput;
@@ -36,15 +49,35 @@ import org.eclipse.ui.part.FileEditorInp
public class DefaultCasDocumentProvider extends
org.apache.uima.caseditor.editor.CasDocumentProvider {
+ /**
+ * This map resolved an opened document to its associated style object id.
+ *
+ * The tracking is done in the provider because the document element itself
+ * does not has any link to the style object.
+ */
+ private Map<String, String> documentToTypeSystemMap = new HashMap<String,
String>();
+
+ private Map<String, EditorAnnotationStatus> sharedEditorStatus = new
HashMap<String, EditorAnnotationStatus>();
+
+ /**
+ * This map resolves a type system to a style.
+ *
+ * TODO: Right now styles are not closed, how are they
+ * deleted when they are not longer needed ?!
+ */
+ private Map<String, DotCorpus> styles = new HashMap<String, DotCorpus>();
+
@Override
protected IDocument createDocument(Object element) throws CoreException {
if (element instanceof FileEditorInput) {
FileEditorInput fileInput = (FileEditorInput) element;
- IFile file = fileInput.getFile();
+ IFile casFile = fileInput.getFile();
- INlpElement nlpElement = CasEditorPlugin.getNlpModel().findMember(file);
+ INlpElement nlpElement =
CasEditorPlugin.getNlpModel().findMember(casFile);
+ // Thats the case if the CAS is inside a Cas Editor Project
+ // and part of a corpus folder
if (nlpElement instanceof DocumentElement) {
try {
@@ -62,7 +95,8 @@ public class DefaultCasDocumentProvider
elementErrorStatus.put(element, new Status(IStatus.ERROR,
CasEditorPlugin.ID, IStatus.OK,
"There is a problem with the document: " + e.getMessage(),
e));
}
- } else {
+ } else if (CasEditorPlugin.getNlpModel().
+ findMember(casFile.getProject()) instanceof INlpElement) {
IStatus status;
if (nlpElement == null) {
@@ -75,11 +109,135 @@ public class DefaultCasDocumentProvider
elementErrorStatus.put(element, status);
}
+ // handle the non Cas Editor project case here
+ else {
+
+ // Try to find a type system for the CAS file
+
+ // TODO: Discuss logic to resolve type system on dev list
+ // For now it will just be assumed that the type system is placed
+ // in a file called TypeSystem.xml at the root of the project
+
+ IFile typeSystemFile = casFile.getProject().getFile("TypeSystem.xml");
+
+ if (typeSystemFile.exists()) {
+
+ // Try to load a style file for the type system
+ // Should be named: ts file name, prefixed with .style-
+ // If it does not exist, create it when it is changed
+ // Creating it after the default is changed means that
+ // colors could change completely when the a type is
+ // added or removed to the type system
+
+ IFile styleFile =
casFile.getProject().getFile(".style-TypeSystem.xml");
+
+ DotCorpus dotCorpus =
styles.get(styleFile.getFullPath().toPortableString());
+
+ if (dotCorpus == null) {
+ if (styleFile.exists()) {
+ InputStream styleFileIn = null;;
+ try {
+ styleFileIn = styleFile.getContents();
+ dotCorpus = DotCorpusSerializer.parseDotCorpus(styleFileIn);
+ }
+ finally {
+ if (styleFileIn != null)
+ try {
+ styleFileIn.close();
+ } catch (IOException e) {
+ CasEditorPlugin.log(e);
+ }
+ }
+ }
+
+ if (dotCorpus == null) {
+ dotCorpus = new DotCorpus();
+
+ // Initialize colors
+ CAS cas = DocumentUimaImpl.getVirginCAS(typeSystemFile);
+ TypeSystem ts = cas.getTypeSystem();
+
+ Collection<AnnotationStyle> defaultStyles =
dotCorpus.getAnnotationStyles();
+
+ Collection<AnnotationStyle> newStyles =
DefaultColors.assignColors(ts, defaultStyles);
+
+ for (AnnotationStyle style : newStyles) {
+ dotCorpus.setStyle(style);
+ }
+ }
+
+ styles.put(styleFile.getFullPath().toPortableString(), dotCorpus);
+ }
+
+ documentToTypeSystemMap.put(casFile.getFullPath().toPortableString(),
+ typeSystemFile.getFullPath().toPortableString());
+
+ // TODO:
+ // Preferences are bound to the type system
+ // Changed in one place, then it should change in all places
+
+ CAS cas = DocumentUimaImpl.getVirginCAS(typeSystemFile);
+
+ DocumentFormat documentFormat;
+
+ // Which file format to use ?
+ if (casFile.getName().endsWith("xmi")) {
+ documentFormat = DocumentFormat.XMI;
+ } else if (casFile.getName().endsWith("xcas")) {
+ documentFormat = DocumentFormat.XCAS;
+ } else {
+ throw new CoreException(new Status(IStatus.ERROR,
"org.apache.uima.dev",
+ "Unkown file format!"));
+ }
+
+ InputStream casIn = casFile.getContents();
+
+ org.apache.uima.caseditor.editor.ICasDocument doc;
+
+ try {
+ doc = new DocumentUimaImpl(cas, casIn, documentFormat);
+ } finally {
+ try {
+ casIn.close();
+ } catch (IOException e) {
+ // TODO: how to handle this error ?
+ // Throw an exception, or continue to display file and
+ // leave stream open ?
+ e.printStackTrace();
+ }
+ }
+
+ AnnotationDocument document = new AnnotationDocument();
+ document.setDocument(doc);
+
+ return document;
+ }
+ else {
+ IStatus status = new Status(IStatus.ERROR, "org.apache.uima.dev",
IStatus.OK,
+ "Cannot find type system!\nPlease place a valid type system
in this path:\n" +
+ typeSystemFile.getLocation().toOSString(), null);
+
+ elementErrorStatus.put(element, status);
+ }
+ }
+
}
return null;
}
+ // Needed for non Cas Editor project case
+ @Override
+ protected void disposeElementInfo(Object element, ElementInfo info) {
+ super.disposeElementInfo(element, info);
+
+ // Remove the mapping of document to type system
+ if (element instanceof FileEditorInput) {
+ FileEditorInput editorInput = (FileEditorInput) element;
+
documentToTypeSystemMap.remove(editorInput.getFile().getFullPath().toPortableString());
+ }
+ }
+
@Override
protected void doSaveDocument(IProgressMonitor monitor, Object element,
IDocument document,
boolean overwrite) throws CoreException {
@@ -112,6 +270,27 @@ public class DefaultCasDocumentProvider
fireElementDirtyStateChanged(element, false);
}
+ private String getTypesystemId(Object element) {
+ if (element instanceof FileEditorInput) {
+ FileEditorInput editorInput = (FileEditorInput) element;
+ return
documentToTypeSystemMap.get(editorInput.getFile().getFullPath().toPortableString());
+ }
+
+ return null;
+ }
+
+ // get access to style for element
+ private DotCorpus getStyle(Object element) {
+ String tsId = getTypesystemId(element);
+
+ int lastSlashIndex = tsId.lastIndexOf("/");
+
+ String styleId = tsId.substring(0, lastSlashIndex + 1);
+ styleId = styleId + ".style-" + tsId.substring(lastSlashIndex + 1);
+
+ return styles.get(styleId);
+ }
+
private INlpElement getNlpElement(Object element) {
if (element instanceof FileEditorInput) {
FileEditorInput fileInput = (FileEditorInput) element;
@@ -124,6 +303,46 @@ public class DefaultCasDocumentProvider
return null;
}
+ private void saveStyles(Object element) {
+ String tsId = getTypesystemId(element);
+
+ int lastSlashIndex = tsId.lastIndexOf("/");
+
+ String styleId = tsId.substring(0, lastSlashIndex + 1);
+ styleId = styleId + ".style-" + tsId.substring(lastSlashIndex + 1);
+
+ DotCorpus style = styles.get(styleId);
+
+ // serialize ...
+ IFile dotCorpusFile = ResourcesPlugin.getWorkspace().getRoot().getFile(
+ Path.fromPortableString(styleId));
+
+ ByteArrayOutputStream dotCorpusBytes = new ByteArrayOutputStream();
+
+ try {
+ DotCorpusSerializer.serialize(style, dotCorpusBytes);
+ } catch (CoreException e) {
+ // will not fail, writing to memory
+ CasEditorPlugin.log(e);
+ }
+
+ try {
+ if (dotCorpusFile.exists()) {
+ dotCorpusFile.setContents(new
ByteArrayInputStream(dotCorpusBytes.toByteArray()),
+ true, false, null);
+ }
+ else {
+ dotCorpusFile.create(new
ByteArrayInputStream(dotCorpusBytes.toByteArray()),
+ true, null);
+ }
+ }
+ catch (CoreException e) {
+ // might fail if writing is not possible
+ // for some reason
+ CasEditorPlugin.log(e);
+ }
+ }
+
@Override
public AnnotationStyle getAnnotationStyle(Object element, Type type) {
@@ -132,33 +351,62 @@ public class DefaultCasDocumentProvider
INlpElement nlpElement = getNlpElement(element);
- return nlpElement.getNlpProject().getDotCorpus().getAnnotation(type);
+ if (nlpElement != null) {
+ return nlpElement.getNlpProject().getDotCorpus().getAnnotation(type);
+ }
+ else {
+ DotCorpus dotCorpus = getStyle(element);
+
+ return dotCorpus.getAnnotation(type);
+ }
}
@Override
public void setAnnotationStyle(Object element, AnnotationStyle style) {
INlpElement nlpElement = getNlpElement(element);
- nlpElement.getNlpProject().getDotCorpus().setStyle(style);
+ if (nlpElement != null) {
+ nlpElement.getNlpProject().getDotCorpus().setStyle(style);
+ }
+ else {
+ DotCorpus dotCorpus = getStyle(element);
+ dotCorpus.setStyle(style);
+
+ saveStyles(element);
+ }
}
@Override
protected Collection<String> getShownTypes(Object element) {
INlpElement nlpElement = getNlpElement(element);
- return nlpElement.getNlpProject().getDotCorpus().getShownTypes();
+ if (nlpElement != null) {
+ return nlpElement.getNlpProject().getDotCorpus().getShownTypes();
+ }
+ else {
+ DotCorpus dotCorpus = getStyle(element);
+
+ return dotCorpus.getShownTypes();
+ }
}
@Override
protected void addShownType(Object element, Type type) {
INlpElement nlpElement = getNlpElement(element);
- nlpElement.getNlpProject().getDotCorpus().addShownType(type.getName());
-
- try {
- nlpElement.getNlpProject().getDotCorpus().serialize();
- } catch (CoreException e) {
- CasEditorPlugin.log(e);
+ if (nlpElement != null) {
+ nlpElement.getNlpProject().getDotCorpus().addShownType(type.getName());
+
+ try {
+ nlpElement.getNlpProject().getDotCorpus().serialize();
+ } catch (CoreException e) {
+ CasEditorPlugin.log(e);
+ }
+ }
+ else {
+ DotCorpus dotCorpus = getStyle(element);
+
+ dotCorpus.setShownType(type.getName());
}
}
@@ -166,12 +414,19 @@ public class DefaultCasDocumentProvider
protected void removeShownType(Object element, Type type) {
INlpElement nlpElement = getNlpElement(element);
- nlpElement.getNlpProject().getDotCorpus().removeShownType(type.getName());
-
- try {
- nlpElement.getNlpProject().getDotCorpus().serialize();
- } catch (CoreException e) {
- CasEditorPlugin.log(e);
+ if (nlpElement != null) {
+
nlpElement.getNlpProject().getDotCorpus().removeShownType(type.getName());
+
+ try {
+ nlpElement.getNlpProject().getDotCorpus().serialize();
+ } catch (CoreException e) {
+ CasEditorPlugin.log(e);
+ }
+ }
+ else {
+ DotCorpus dotCorpus = getStyle(element);
+
+ dotCorpus.removeShownType(type.getName());
}
}
@@ -179,7 +434,17 @@ public class DefaultCasDocumentProvider
protected EditorAnnotationStatus getEditorAnnotationStatus(Object element) {
INlpElement nlpElement = getNlpElement(element);
- return nlpElement.getNlpProject().getEditorAnnotationStatus();
+ if (nlpElement != null) {
+ return nlpElement.getNlpProject().getEditorAnnotationStatus();
+ }
+ else {
+ EditorAnnotationStatus status =
sharedEditorStatus.get(getTypesystemId(element));
+
+ if (status == null)
+ status = new EditorAnnotationStatus(CAS.TYPE_NAME_ANNOTATION, null);
+
+ return status;
+ }
}
@Override
@@ -187,6 +452,11 @@ public class DefaultCasDocumentProvider
EditorAnnotationStatus editorAnnotationStatus) {
INlpElement nlpElement = getNlpElement(element);
-
nlpElement.getNlpProject().setEditorAnnotationStatus(editorAnnotationStatus);
+ if (nlpElement != null) {
+
nlpElement.getNlpProject().setEditorAnnotationStatus(editorAnnotationStatus);
+ }
+ else {
+ sharedEditorStatus.put(getTypesystemId(element), editorAnnotationStatus);
+ }
}
}
Modified:
uima/uimaj/trunk/uimaj-ep-cas-editor/src/main/java/org/apache/uima/caseditor/editor/DocumentUimaImpl.java
URL:
http://svn.apache.org/viewvc/uima/uimaj/trunk/uimaj-ep-cas-editor/src/main/java/org/apache/uima/caseditor/editor/DocumentUimaImpl.java?rev=1003813&r1=1003812&r2=1003813&view=diff
==============================================================================
---
uima/uimaj/trunk/uimaj-ep-cas-editor/src/main/java/org/apache/uima/caseditor/editor/DocumentUimaImpl.java
(original)
+++
uima/uimaj/trunk/uimaj-ep-cas-editor/src/main/java/org/apache/uima/caseditor/editor/DocumentUimaImpl.java
Sat Oct 2 15:27:30 2010
@@ -19,6 +19,7 @@
package org.apache.uima.caseditor.editor;
+import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
@@ -29,6 +30,8 @@ import javax.xml.parsers.ParserConfigura
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
+import org.apache.uima.ResourceSpecifierFactory;
+import org.apache.uima.UIMAFramework;
import org.apache.uima.cas.CAS;
import org.apache.uima.cas.ConstraintFactory;
import org.apache.uima.cas.FSIndex;
@@ -47,7 +50,17 @@ import org.apache.uima.cas.text.Annotati
import org.apache.uima.caseditor.CasEditorPlugin;
import org.apache.uima.caseditor.editor.util.Span;
import org.apache.uima.caseditor.editor.util.StrictTypeConstraint;
+import org.apache.uima.resource.ResourceInitializationException;
+import org.apache.uima.resource.metadata.FsIndexDescription;
+import org.apache.uima.resource.metadata.TypePriorities;
+import org.apache.uima.resource.metadata.TypeSystemDescription;
+import org.apache.uima.resource.metadata.impl.FsIndexDescription_impl;
+import org.apache.uima.util.CasCreationUtils;
+import org.apache.uima.util.InvalidXMLException;
+import org.apache.uima.util.XMLInputSource;
+import org.apache.uima.util.XMLParser;
import org.apache.uima.util.XMLSerializer;
+import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
@@ -339,4 +352,60 @@ public class DocumentUimaImpl extends Ab
"Unkown file format!", null));
}
}
+
+ public static CAS getVirginCAS(IFile typeSystemFile) throws CoreException {
+ ResourceSpecifierFactory resourceSpecifierFactory =
UIMAFramework.getResourceSpecifierFactory();
+
+ IFile extensionTypeSystemFile = typeSystemFile;
+
+ InputStream inTypeSystem;
+
+ if (extensionTypeSystemFile != null && extensionTypeSystemFile.exists()) {
+ inTypeSystem = extensionTypeSystemFile.getContents();
+ } else {
+ return null;
+ }
+
+ XMLInputSource xmlTypeSystemSource = new XMLInputSource(inTypeSystem, new
File(""));
+
+ XMLParser xmlParser = UIMAFramework.getXMLParser();
+
+ TypeSystemDescription typeSystemDesciptor;
+
+ try {
+ typeSystemDesciptor = (TypeSystemDescription)
xmlParser.parse(xmlTypeSystemSource);
+
+ typeSystemDesciptor.resolveImports();
+ } catch (InvalidXMLException e) {
+
+ String message = e.getMessage() != null ? e.getMessage() : "";
+
+ // TODO: Change plugin ID
+ IStatus s = new Status(IStatus.ERROR, "org.apache.uima.dev", IStatus.OK,
message, e);
+
+ throw new CoreException(s);
+ }
+
+ TypePriorities typePriorities =
resourceSpecifierFactory.createTypePriorities();
+
+ FsIndexDescription indexDesciptor = new FsIndexDescription_impl();
+ indexDesciptor.setLabel("TOPIndex");
+ indexDesciptor.setTypeName("uima.cas.TOP");
+ indexDesciptor.setKind(FsIndexDescription.KIND_SORTED);
+
+ CAS cas;
+ try {
+ cas = CasCreationUtils.createCas(typeSystemDesciptor, typePriorities,
+ new FsIndexDescription[] { indexDesciptor });
+ } catch (ResourceInitializationException e) {
+ String message = e.getMessage() != null ? e.getMessage() : "";
+
+ // TODO: Change plugin ID
+ IStatus s = new Status(IStatus.ERROR, "org.apache.uima.dev", IStatus.OK,
message, e);
+
+ throw new CoreException(s);
+ }
+
+ return cas;
+ }
}