use of gate.util.GateSaxException in project gate-core by GateNLP.
the class CreoleXmlHandler method endElement.
// checkStack
/**
* Called when the SAX parser encounts the end of an XML element.
* This is where ResourceData objects get values set, and where
* they are added to the CreoleRegister when we parsed their complete
* metadata entries.
*/
@Override
public void endElement(String uri, String qName, String elementName) throws GateSaxException, SAXException {
// call characterActions
if (readCharacterStatus) {
readCharacterStatus = false;
charactersAction(new String(contentBuffer).toCharArray(), 0, contentBuffer.length());
}
if (DEBUG)
Out.prln("endElement: " + elementName);
// ////////////////////////////////////////////////////////////////
if (elementName.toUpperCase().equals("RESOURCE")) {
// check for validity of the resource data
if (!resourceData.isValid())
throw new GateSaxException("Invalid resource data: " + resourceData.getValidityMessage());
// TODO should this be a URI instead of the URL?
try {
resourceData.setXmlFileUrl(new URL(plugin.getBaseURL(), "creole.xml"));
} catch (MalformedURLException e) {
throw new GateSaxException("Couldn't load autoloading resource: " + resourceData.getName() + "; problem was: " + e, e);
}
// add the new resource data object to the creole register
register.put(resourceData.getClassName(), resourceData);
// if the resource is auto-loading, try and load it
if (resourceData.isAutoLoading())
try {
@SuppressWarnings("unused") Class<?> resClass = resourceData.getResourceClass();
// Resource res = Factory.createResource(
// resourceData.getClassName(), Factory.newFeatureMap()
// );
// resourceData.makeInstantiationPersistant(res);
} catch (ClassNotFoundException e) {
throw new GateSaxException("Couldn't load autoloading resource: " + resourceData.getName() + "; problem was: " + e);
}
// (note that they're not disjunctive or the "/OR" would have got them)
if (!currentParamDisjunction.isEmpty()) {
currentParamList.addAll(currentFlattenedDisjunction());
currentParamDisjunction.clear();
}
// End if
// add the parameter list to the resource (and reinitialise it)
resourceData.setParameterList(currentParamList);
currentParamList = new ParameterList();
// final initialization of the ResourceData
try {
resourceData.init();
} catch (Exception ex) {
throw new GateSaxException("Couldn't initialize ResourceData for " + resourceData.getName(), ex);
}
if (DEBUG)
Out.println("added: " + resourceData);
// Iterate through autoinstances and try to instanciate them
if (currentAutoinstances != null && !currentAutoinstances.isEmpty()) {
ResourceData rd = Gate.getCreoleRegister().get(resourceData.getClassName());
ParameterList existingParameters = null;
if (rd.getReferenceCount() > 1) {
// we aren't going to redefine a resource but we do need to use the
// parameters from the new instance so we get the right base URL and
// default values etc.
existingParameters = rd.getParameterList();
rd.setParameterList(resourceData.getParameterList());
}
try {
Iterator<FeatureMap> iter = currentAutoinstances.iterator();
while (iter.hasNext()) {
FeatureMap autoinstanceParams = iter.next();
iter.remove();
FeatureMap autoinstanceFeatures = null;
// map and move the hidden attribute there.
if (Gate.getHiddenAttribute(autoinstanceParams)) {
autoinstanceFeatures = Factory.newFeatureMap();
Gate.setHiddenAttribute(autoinstanceFeatures, true);
autoinstanceParams.remove(GateConstants.HIDDEN_FEATURE_KEY);
}
// Try to create the resource.
try {
// Resource res =
Factory.createResource(resourceData.getClassName(), autoinstanceParams, autoinstanceFeatures);
// resourceData.makeInstantiationPersistant(res);
// all resource instantiations are persistent
} catch (ResourceInstantiationException e) {
throw new GateSaxException("Couldn't auto-instantiate resource: " + resourceData.getName() + "; problem was: " + e);
}
// End try
}
// End while
} finally {
// resource then put them back before we break something
if (existingParameters != null)
rd.setParameterList(existingParameters);
}
}
// End if
currentAutoinstances = null;
// End RESOURCE processing
// ////////////////////////////////////////////////////////////////
} else if (elementName.toUpperCase().equals("AUTOINSTANCE") || elementName.toUpperCase().equals("HIDDEN-AUTOINSTANCE")) {
// Cache the auto-instance into the autoins
if (currentAutoinstanceParams != null)
currentAutoinstances.add(currentAutoinstanceParams);
// End AUTOINSTANCE processing
// ////////////////////////////////////////////////////////////////
} else if (elementName.toUpperCase().equals("PARAM")) {
// End PARAM processing
// ////////////////////////////////////////////////////////////////
} else if (elementName.toUpperCase().equals("NAME")) {
checkStack("endElement", "NAME");
resourceData.setName(contentStack.pop());
// End NAME processing
// ////////////////////////////////////////////////////////////////
} else if (elementName.toUpperCase().equals("IVY")) {
if (!contentStack.isEmpty())
contentStack.pop();
// End IVY processing
// ////////////////////////////////////////////////////////////////
} else if (elementName.toUpperCase().equals("REQUIRES")) {
if (!contentStack.isEmpty())
contentStack.pop();
// End REQUIRES processing
// ////////////////////////////////////////////////////////////////
} else if (elementName.toUpperCase().equals("JAR")) {
checkStack("endElement", "JAR");
// add jar file name
String jarFileName = contentStack.pop();
if (resourceData != null) {
resourceData.setJarFileName(jarFileName);
}
// add jar file URL if there is one
// if(sourceUrl != null) {
String sourceUrlName = plugin.getBaseURL().toExternalForm();
String separator = "/";
if (sourceUrlName.endsWith(separator))
separator = "";
URL jarFileUrl = null;
try {
jarFileUrl = new URL(sourceUrlName + separator + jarFileName);
if (resourceData != null) {
resourceData.setJarFileUrl(jarFileUrl);
}
// We no longer need to add the jar URL to the class loader, as this
// is done before the SAX parse
} catch (MalformedURLException e) {
throw new GateSaxException("bad URL " + jarFileUrl + e);
}
// End try
// }// End if
// End JAR processing
// ////////////////////////////////////////////////////////////////
} else if (elementName.toUpperCase().equals("CLASS")) {
checkStack("endElement", "CLASS");
resourceData.setClassName(contentStack.pop());
// End CLASS processing
// ////////////////////////////////////////////////////////////////
} else if (elementName.toUpperCase().equals("COMMENT")) {
checkStack("endElement", "COMMENT");
resourceData.setComment(contentStack.pop());
// End COMMENT processing
// ////////////////////////////////////////////////////////////////
} else if (elementName.toUpperCase().equals("HELPURL")) {
checkStack("endElement", "HELPURL");
resourceData.setHelpURL(contentStack.pop());
// End HELPURL processing
// ////////////////////////////////////////////////////////////////
} else if (elementName.toUpperCase().equals("INTERFACE")) {
checkStack("endElement", "INTERFACE");
resourceData.setInterfaceName(contentStack.pop());
// End INTERFACE processing
// ////////////////////////////////////////////////////////////////
} else if (elementName.toUpperCase().equals("ICON")) {
checkStack("endElement", "ICON");
resourceData.setIcon(contentStack.pop());
// End ICON processing
// ////////////////////////////////////////////////////////////////
} else if (elementName.toUpperCase().equals("OR")) {
currentParamList.add(currentFlattenedDisjunction());
currentParamDisjunction.clear();
// End OR processing
// ////////////////////////////////////////////////////////////////
} else if (elementName.toUpperCase().equals("PARAMETER")) {
checkStack("endElement", "PARAMETER");
currentParam.typeName = contentStack.pop();
String priorityStr = currentAttributes.getValue("PRIORITY");
// if no priority specified, assume lowest (i.e. parameters with an
// explicit priority come ahead of those without).
Integer priority = Integer.MAX_VALUE;
try {
if (priorityStr != null)
priority = Integer.valueOf(priorityStr);
} catch (NumberFormatException nfe) {
throw new GateRuntimeException("Found in creole.xml a PARAM element" + " for resource " + resourceData.getClassName() + " with a non-numeric" + " PRIORITY attribute. Check the file and try again.");
}
List<Parameter> paramList = currentParamDisjunction.get(priority);
if (paramList == null) {
paramList = new ArrayList<Parameter>();
currentParamDisjunction.put(priority, paramList);
}
paramList.add(currentParam);
if (DEBUG)
Out.prln("added param: " + currentParam);
currentParam = new Parameter(plugin);
// End PARAMETER processing
// ////////////////////////////////////////////////////////////////
} else if (elementName.toUpperCase().equals("AUTOLOAD")) {
resourceData.setAutoLoading(true);
// End AUTOLOAD processing
// ////////////////////////////////////////////////////////////////
} else if (elementName.toUpperCase().equals("PRIVATE")) {
resourceData.setPrivate(true);
// End PRIVATE processing
// ////////////////////////////////////////////////////////////////
} else if (elementName.toUpperCase().equals("TOOL")) {
resourceData.setTool(true);
// End TOOL processing
// ////////////////////////////////////////////////////////////////
} else if (elementName.toUpperCase().equals("MAIN_VIEWER")) {
resourceData.setIsMainView(true);
// End MAIN_VIEWER processing
// ////////////////////////////////////////////////////////////////
} else if (elementName.toUpperCase().equals("RESOURCE_DISPLAYED")) {
checkStack("endElement", "RESOURCE_DISPLAYED");
String resourceDisplayed = contentStack.pop();
resourceData.setResourceDisplayed(resourceDisplayed);
try {
@SuppressWarnings("unused") Class<?> resourceDisplayedClass = Gate.getClassLoader().loadClass(resourceDisplayed);
} catch (ClassNotFoundException ex) {
throw new GateRuntimeException("Couldn't get resource class from the resource name :" + resourceDisplayed + " " + ex);
}
// End try
// End RESOURCE_DISPLAYED processing
// ////////////////////////////////////////////////////////////////
} else if (elementName.toUpperCase().equals("ANNOTATION_TYPE_DISPLAYED")) {
checkStack("endElement", "ANNOTATION_TYPE_DISPLAYED");
resourceData.setAnnotationTypeDisplayed(contentStack.pop());
// End ANNOTATION_TYPE_DISPLAYED processing
// ////////////////////////////////////////////////////////////////
} else if (elementName.toUpperCase().equals("GUI")) {
// End GUI processing
// ////////////////////////////////////////////////////////////////
} else if (elementName.toUpperCase().equals("CREOLE")) {
// End CREOLE processing
// ////////////////////////////////////////////////////////////////
} else if (elementName.toUpperCase().equals("CREOLE-DIRECTORY")) {
// End CREOLE-DIRECTORY processing
// ////////////////////////////////////////////////////////////////
} else {
// arbitrary elements get added as features of the resource data
if (resourceData != null)
resourceData.getFeatures().put(elementName.toUpperCase(), ((contentStack.isEmpty()) ? null : contentStack.pop()));
}
// ////////////////////////////////////////////////////////////////
}
use of gate.util.GateSaxException in project gate-core by GateNLP.
the class XmlDocumentHandler method endDocument.
/**
* This method is called when the SAX parser encounts the end of the
* XML document.
* Here we set the content of the gate Document to be the one generated
* inside this class (tmpDocContent).
* After that we use the colector to generate all the annotation reffering
* this new gate document.
*/
@Override
public void endDocument() throws org.xml.sax.SAXException {
// replace the document content with the one without markups
doc.setContent(new DocumentContentImpl(tmpDocContent.toString()));
// fire the status listener
fireStatusChangedEvent("Total elements: " + elements);
// based on the gate document.
if (basicAS == null) {
basicAS = doc.getAnnotations(GateConstants.ORIGINAL_MARKUPS_ANNOT_SET_NAME);
}
// sort colector ascending on its id
Collections.sort(colector);
Set<Integer> testIdsSet = new HashSet<Integer>();
// create all the annotations (on this new document) from the collector
while (!colector.isEmpty()) {
CustomObject obj = colector.getFirst();
// Test to see if there are two annotation objects with the same id.
if (testIdsSet.contains(obj.getId())) {
throw new GateSaxException("Found two annotations with the same Id(" + obj.getId() + ").The document is inconsistent.");
} else {
testIdsSet.add(obj.getId());
}
// create a new annotation and add it to the annotation set
try {
// add the annotation to the Annotation Set
if (markupElementsMap == null) {
basicAS.add(obj.getId(), obj.getStart(), obj.getEnd(), obj.getElemName(), obj.getFM());
} else {
// get the type of the annotation from Map
String annotationType = markupElementsMap.get(obj.getElemName());
if (annotationType != null) {
basicAS.add(obj.getId(), obj.getStart(), obj.getEnd(), annotationType, obj.getFM());
}
}
// End if
} catch (gate.util.InvalidOffsetException e) {
Err.prln("InvalidOffsetException for annot :" + obj.getElemName() + " with Id =" + obj.getId() + ". Discarded...");
}
// End try
colector.remove(obj);
}
// End while
}
Aggregations