I have special kind of .xml files which I need to open through my own eclipse editor. So I added a custom content type and bound with my editor.
This is my Describer class.
public class TEPFileContentDescriber implements IContentDescriber {
@Override
public int describe(InputStream contents, IContentDescription description)
throws IOException {
try{
XmlUtil.parseSuite(contents);
System.out.println("VALID");
return IContentDescriber.VALID;
}
catch(Exception e){
System.out.println("INVALID");
return IContentDescriber.INVALID;
}
}
@Override
public QualifiedName[] getSupportedOptions() {
// TODO Auto-generated method stub
return null;
}
}
In the console I can see the correct status has been triggered, but the Project Explorer still treats all xml files as my type, so the editor complains other files when it tries to open.
1) Is there any other methods I need to Override here?
2) Can I use a separate file icon as well, only for my content type?
The describe
method must only return VALID
if the XML is actually correct for your application.
So in your code XmlUtil.parseSuite
must throw an exception if the XML is not correct for your app. So, for example, if your describer is given an Ant build script XML you must reject it.
You should also return INDETERMINATE
if the input looks like XML but is not for your app.
You can extend XMLContentDescriber
to do some of the work for you.
For example this is the Ant plugin content describer:
public final class AntBuildfileContentDescriber extends XMLContentDescriber {
private int checkCriteria(InputSource contents) throws IOException {
AntHandler antHandler = new AntHandler();
try {
if (!antHandler.parseContents(contents)) {
return INDETERMINATE;
}
}
catch (SAXException e) {
// we may be handed any kind of contents... it is normal we fail to parse
return INDETERMINATE;
}
catch (ParserConfigurationException e) {
// some bad thing happened - force this describer to be disabled
String message = "Internal Error: XML parser configuration error during content description for Ant buildfiles"; //$NON-NLS-1$
throw new RuntimeException(message);
}
// Check to see if we matched our criteria.
if (antHandler.hasRootProjectElement()) {
if (antHandler.hasProjectDefaultAttribute() || antHandler.hasTargetElement() || antHandler.hasAntElement()) {
// project and default attribute or project and target element(s)
// or project and top level ant element(s) (classpath, import, macrodef, path, property, taskdef, typedef)
return VALID;
}
// only a top level project element...maybe an Ant buildfile
return INDETERMINATE;
}
return INDETERMINATE;
}
@Override
public int describe(InputStream contents, IContentDescription description) throws IOException {
// call the basic XML describer to do basic recognition
if (super.describe(contents, description) == INVALID) {
return INVALID;
}
// super.describe will have consumed some chars, need to rewind
contents.reset();
// Check to see if we matched our criteria.
return checkCriteria(new InputSource(contents));
}
public int describe(Reader contents, IContentDescription description) throws IOException {
// call the basic XML describer to do basic recognition
if (super.describe(contents, description) == INVALID) {
return INVALID;
}
// super.describe will have consumed some chars, need to rewind
contents.reset();
// Check to see if we matched our criteria.
return checkCriteria(new InputSource(contents));
}
}
You could use this as a basis and alter checkCriteria
to do your checks.
The eclipse-editors are file-base not content-based ... you can check your file as described in your answer, but the file type is determinded by the file-ending.
that concept is used in eclipse as well as on default file systems like from windows or similar...
maybe you should introduce a custom filetype, something like myfile.tepxml
you can define certain content-types wich are a combination of file name and file extension, but they are customized defined, as a part of your eclipse setting and not part of your editor (locked are pre-defined from eclipse)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.