in uimaj-ep-cas-editor-ide/src/main/java/org/apache/uima/caseditor/ide/DefaultCasDocumentProvider.java [215:418]
protected ICasDocument createDocument(Object element) throws CoreException {
if (element instanceof FileEditorInput) {
FileEditorInput fileInput = (FileEditorInput) element;
IFile casFile = fileInput.getFile();
// Try to find a type system for the CAS file
// TODO: Change to only use full path
IFile typeSystemFile = null;
// First check if a type system is already known or was
// set by the editor for this specific CAS.
// apply that type system only if the setting is active in the preferences
String typeSystemFileString = null;
String document = casFile.getFullPath().toPortableString();
if (typeSystemForNextDocumentOnly.get(document) != null) {
// the type system was already set internally. Use this one and forget the information.
typeSystemFileString = typeSystemForNextDocumentOnly.get(document);
typeSystemForNextDocumentOnly.put(document, null);
}
IPreferenceStore prefStore = CasEditorIdePlugin.getDefault().getPreferenceStore();
boolean useLastTypesystem = prefStore
.getBoolean(CasEditorIdePreferenceConstants.CAS_EDITOR_REMEMBER_TYPESYSTEM);
if (typeSystemFileString == null && useLastTypesystem) {
typeSystemFileString = documentToTypeSystemMap.get(document);
}
if (typeSystemFileString != null)
typeSystemFile = ResourcesPlugin.getWorkspace().getRoot()
.getFile(new Path(typeSystemFileString));
// use search strategies for finding the type system
if (typeSystemFile == null || !typeSystemFile.exists()) {
Map<Integer, ITypeSystemSearchStrategy> searchStrategies = TypeSystemSearchStrategyFactory
.instance().getSearchStrategies();
// TODO sort again for user preference settings
Collection<ITypeSystemSearchStrategy> values = searchStrategies.values();
for (ITypeSystemSearchStrategy eachStrategy : values) {
IFile findTypeSystem = eachStrategy.findTypeSystem(casFile);
if (findTypeSystem != null && findTypeSystem.exists()) {
typeSystemFile = findTypeSystem;
break;
}
}
}
// If non was found get it from project
if (typeSystemFile == null)
typeSystemFile = TypeSystemLocationPropertyPage.getTypeSystemLocation(casFile.getProject());
if (typeSystemFile != null && typeSystemFile.exists()) {
if (!typeSystemFile.isSynchronized(IResource.DEPTH_ZERO)) {
typeSystemFile.refreshLocal(IResource.DEPTH_ZERO, new NullProgressMonitor());
}
// TODO: Update this comment!
// 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 prefFile = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(
getPreferenceFileForTypeSystem(typeSystemFile.getFullPath().toPortableString())));
PreferenceStore tsPrefStore = typeSystemPreferences
.get(prefFile.getFullPath().toPortableString());
// If lookup for store failed ...
if (tsPrefStore == null) {
if (prefFile.exists()) {
tsPrefStore = new PreferenceStore(prefFile.getName());
try {
tsPrefStore.load(prefFile.getContents()); // TODO: Close stream!
} catch (IOException e) {
e.printStackTrace(); // TODO: Handle this correctly!
}
} else {
// UIMA-2245
// DotCorpus to Eclipse PreferenceStore migration code.
// If there is DotCorpus style file and not yet a preference store file
// the settings from the DotCorpus style file should be written into a preference store
// file.
IFile styleFile = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(
getStyleFileForTypeSystem(typeSystemFile.getFullPath().toPortableString())));
if (styleFile.exists()) {
InputStream styleFileIn = null;
DotCorpus dotCorpus;
try {
styleFileIn = styleFile.getContents();
dotCorpus = DotCorpusSerializer.parseDotCorpus(styleFileIn);
} finally {
if (styleFileIn != null)
try {
styleFileIn.close();
} catch (IOException e) {
CasEditorPlugin.log(e);
}
}
if (dotCorpus != null) {
tsPrefStore = new PreferenceStore(prefFile.getName());
for (AnnotationStyle style : dotCorpus.getAnnotationStyles()) {
AnnotationStyle.putAnnotatationStyleToStore(tsPrefStore, style);
}
for (String shownType : dotCorpus.getShownTypes()) {
tsPrefStore.putValue(shownType + ".isShown", "true");
}
ByteArrayOutputStream prefOut = new ByteArrayOutputStream();
try {
tsPrefStore.save(prefOut, "");
} catch (IOException e) {
// Should never happen!
CasEditorPlugin.log(e);
}
// TODO: Do we need to handle exceptions here?
prefFile.create(new ByteArrayInputStream(prefOut.toByteArray()), IFile.FORCE, null);
}
}
}
// No preference defined, lets use defaults
if (tsPrefStore == null) {
tsPrefStore = new PreferenceStore(prefFile.getName());
CAS cas = DocumentUimaImpl.getVirginCAS(typeSystemFile);
TypeSystem ts = cas.getTypeSystem();
Collection<AnnotationStyle> defaultStyles = getConfiguredAnnotationStyles(tsPrefStore,
ts);
Collection<AnnotationStyle> newStyles = DefaultColors.assignColors(ts, defaultStyles);
// TODO: Settings defaults must be moved to the AnnotationEditor
for (AnnotationStyle style : newStyles) {
AnnotationStyle.putAnnotatationStyleToStore(tsPrefStore, style);
}
}
typeSystemPreferences.put(prefFile.getFullPath().toPortableString(), tsPrefStore);
}
documentToTypeSystemMap.put(document, typeSystemFile.getFullPath().toPortableString());
IPreferenceStore store = sessionPreferenceStores.get(getTypesystemId(element));
if (store == null) {
PreferenceStore newStore = new PreferenceStore();
sessionPreferenceStores.put(getTypesystemId(element), newStore);
newStore.addPropertyChangeListener(new SaveSessionPreferencesTrigger(element));
String sessionPreferenceString = typeSystemFile
.getPersistentProperty(new QualifiedName("", CAS_EDITOR_SESSION_PROPERTIES));
if (sessionPreferenceString != null) {
try {
newStore.load(new ByteArrayInputStream(
sessionPreferenceString.getBytes(StandardCharsets.UTF_8)));
} catch (IOException e) {
CasEditorPlugin.log(e);
}
}
}
// TODO:
// Preferences are bound to the type system
// Changed in one place, then it should change in all places
CAS cas = DocumentUimaImpl.getVirginCAS(typeSystemFile);
ICasDocument doc = new DocumentUimaImpl(cas, casFile,
typeSystemFile.getFullPath().makeRelative().toString());
elementErrorStatus.remove(element);
return doc;
} else {
String message;
if (typeSystemFile != null) {
message = "Cannot find type system!\nPlease place a valid type system in this path:\n"
+ typeSystemFile.getFullPath().toString();
} else
message = "Type system is not set, please choose a type system to open the CAS.";
IStatus status = new Status(IStatus.ERROR, "org.apache.uima.dev",
CasDocumentProvider.TYPE_SYSTEM_NOT_AVAILABLE_STATUS_CODE, message, null);
elementErrorStatus.put(element, status);
}
}
return null;
}