|
|
|
|
Re: Different editors for same file-extensions [message #972141 is a reply to message #945222] |
Mon, 05 November 2012 11:42 |
Hazem ELRAFFIEE Messages: 61 Registered: September 2012 |
Member |
|
|
I implemented solution number 1 Actually a friend of mine did it for me I would like to share what I reached:
Class XMLRootVersionHandler
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class XMLRootVersionHandler extends DefaultHandler {
String localQualifiedTagName = null;
String localAttributeName = null;
String localVersion = null;
boolean nextIsVersion = false;
public void startElement(String uri, String localName, String qname, Attributes attr) throws SAXException {
if(qname != null && qname.equals(localQualifiedTagName))
{
if(localAttributeName==null){
nextIsVersion = true;
}else{
localVersion = attr.getValue(localAttributeName);
throw new XMLRootVersionTerminatorException();
}
}
}
public void characters(char[] characters, int start, int length) throws SAXException {
if(nextIsVersion){
localVersion = new String(characters, start, length);
throw new XMLRootVersionTerminatorException();
}
}
public String getVersion(){
return localVersion;
}
public void setStringsToSearch(String qualifiedTagName, String attributeName){
localQualifiedTagName = qualifiedTagName;
localAttributeName = attributeName;
}
}
Class XMLRootVersionDescriber
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.util.Hashtable;
import org.eclipse.core.internal.content.ContentMessages;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExecutableExtension;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.content.IContentDescription;
import org.eclipse.core.runtime.content.XMLContentDescriber;
import org.eclipse.osgi.util.NLS;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;
@SuppressWarnings({"rawtypes", "unchecked" })
public class XMLRootVersionDescriber extends XMLContentDescriber implements IExecutableExtension {
Hashtable<String, String> describerParameters = null;
static final String qualifiedTagNameParameter = "qualifiedTagName";
static final String versionParameter = "version";
static final String attributeNameParameter = "attributeName";
/* (Intentionally not included in javadoc)
* @see IContentDescriber#describe(InputStream, IContentDescription)
*/
public int describe(InputStream contents, IContentDescription description) throws IOException {
// super.describe will have consumed some chars, need to rewind
contents.reset();
// Check to see if we matched our criteria.
int checkCriteria = checkCriteria(new InputSource(contents));
System.out.println("v2 Return (1) : " + checkCriteria);
return checkCriteria;
}
/* (Intentionally not included in javadoc)
* @see IContentDescriber#describe(Reader, IContentDescription)
*/
public int describe(Reader contents, IContentDescription description) throws IOException {
int checkCriteria = checkCriteria(new InputSource(contents));
System.out.println("v2 Return (2) : " + checkCriteria);
return checkCriteria;
}
private int checkCriteria(InputSource contents) throws IOException {
XMLRootVersionHandler handler = new XMLRootVersionHandler();
try {
XMLReader saxReader = XMLReaderFactory.createXMLReader();
handler.setStringsToSearch(
describerParameters.get(qualifiedTagNameParameter),
describerParameters.get(attributeNameParameter)
);
saxReader.setContentHandler(handler);
saxReader.parse(contents);
}catch (XMLRootVersionTerminatorException e){
//ok
String version = (String) handler.getVersion();
if (version != null && version.equals(describerParameters.get(versionParameter))){
return VALID;
}
}catch (SAXException e) {
// we may be handed any kind of contents... it is normal we fail to parse
return INVALID;
}catch (Exception e) {
// we may be handed any kind of contents... it is normal we fail to parse
return INVALID;
}
return INVALID;
}
/* (Intentionally not included in javadoc)
* @see IExecutableExtension#setInitializationData
*/
public void setInitializationData(final IConfigurationElement config, final String propertyName, final Object data) throws CoreException {
if (data instanceof Hashtable) {
if(((Hashtable) data).get(qualifiedTagNameParameter) != null && ((Hashtable) data).get(versionParameter) != null ){
describerParameters = (Hashtable) data;
}
}
if (describerParameters == null) {
String message = NLS.bind(ContentMessages.content_badInitializationData, XMLRootVersionDescriber.class.getName());
throw new CoreException(new Status(IStatus.ERROR, ContentMessages.OWNER_NAME, 0, message, null));
}
}
}
Class XMLRootVersionTerminatorException
import org.xml.sax.SAXException;
public class XMLRootVersionTerminatorException extends SAXException {
private static final long serialVersionUID = 4280571238180794197L;
}
"plugin.xml" file should have the following:
<plugin>
<extension point="org.eclipse.ui.editors">
<editor
class="path.to.editor.ui.MSXMLEditor"
default="true"
id="path.to.editor.ui.MSXMLEditor"
name="MSXML Editor">
<contentTypeBinding
contentTypeId="path.to.editor.content-type">
</contentTypeBinding>
</editor>
</extension>
<extension
point="org.eclipse.core.contenttype.contentTypes">
<content-type
base-type="org.eclipse.core.runtime.xml"
file-extensions="msxml"
id="path.to.editor.content-type"
name="MSXML File"
priority="normal">
<describer class="path.to.editor.XMLRootVersionDescriber">
<parameter
name="qualifiedTagName"
value="msxml">
</parameter>
<parameter
name="attributeName"
value="version">
</parameter>
<parameter
name="version"
value="1.3">
</parameter>
</describer>
</content-type>
</extension>
</plugin>
Describer parameters are:
- qualifiedTagName: Name of the Tag to search for the version inside it. If second parameter "attributeName" is not specified, qualified tag contents will be considered as the version.
- attributeName: If version is specified inside an attribute in the qualified tag, specify this attribute name here.
- version: The version of your xml that this editor is supposed to handle
[Updated on: Mon, 05 November 2012 12:29] Report message to a moderator
|
|
|
Powered by
FUDForum. Page generated in 0.04174 seconds