refer to DTD in the plugin

Hi

I have an xml in my plugin. The xml refers to a DTD as below.

<!DOCTYPE templates PUBLIC "//UNKNOWN/" "https://raw.githubusercontent.com/3pintech/fastcode/master/resources/template.dtd">

I have now placed the DTD in the resources folder, where the xml is present.

I tried refering to the dtd as below, in my xml.

<!DOCTYPE templates SYSTEM "template.dtd">

Then deployed the plugin, the xml does not get parsed.
Intellij is looking for the DTD in the below path

C:\Program Files (x86)\JetBrains\IntelliJ IDEA 12.0.1_new\bin\template.dtd (The system cannot find the file specified)



How should i refer to the DTD in this case?

23 comments
Comment actions Permalink

You can register your DTD with bundled version packaged in your plugin via EP com.intellij.javaee.StandardResourceProvider

0
Comment actions Permalink

Hi

I m new to intellij...Could you please elaborate more.

0
Comment actions Permalink

Sorry, I gave wrong EP, use com.intellij.javaee.StandardResourceProvider

see org.jetbrains.idea.maven.dom.MavenSchemaProvider from Community sources as sample

0
Comment actions Permalink

Hi

I tried doing this.

In my plugin xml i included

<standardResourceProvider implementation="org.mypak.settings.MyProjectSchemaProvider"/>

In package org.mypak.settings,

public class MyProjectSchemaProvider implements StandardResourceProvider {

    @Override
    public void registerResources(ResourceRegistrar resourceRegistrar) {
        resourceRegistrar.addStdResource("", "/resources/template.dtd", getClass());

}

In my xml
<!DOCTYPE templates SYSTEM "template.dtd">

Could you please point out the mistake here.

My Question is a continuation of the query in the below link
https://devnet.jetbrains.com/thread/465810?tstart=0

My Query:
both my xml(example - > a.xml) and DTD (example -> a.dtd) are in the same folder and are part of my plugin. My plugin uses this xml to load some settings. In this xml (a.xml) i want to know how to refer to the DTD (a.dtd).

0
Comment actions Permalink

Why is first parameter in addStdResource() call empty? It must contain the URI for your DTD.

0
Comment actions Permalink

What should be the URI? I dont want to refer to the one in github. Instead i need to refer to the one present int he resources folder (the same place where the xml is present...the resoures folder is part of my plugin)

0
Comment actions Permalink

AFAIU you could use any URI. Does it not work? The URI will be "mapped" to the file packaged in your resources folder.

0
Comment actions Permalink

I tried doing this

@Override
    public void registerResources(ResourceRegistrar resourceRegistrar) {
        LOGGER.info("in register");
        resourceRegistrar.addStdResource("http://fastcode/resources/template.dtd", "./template.dtd", getClass());
        LOGGER.info("after register");
    }


It did not work. I get both the logs "in regeister" and "after register". Just after that i find the below warning --

WARN - pl.ExternalResourceManagerImpl - Cannot find standard resource. filename ./template.dtd.

I tried with the following values for the 1st

"http://fastcode/resources/template.dtd" , "file://fastcode_idea12/classes/template.dtd"


and 2nd parameter

"template.dtd", "/template.dtd", getPluginVirtualDirectory +"/classes/template.dtd", "../../../../resources/template.dtd"

In all the cases i get the same warning.

I used both the URI in the xml dtd definition, in both cases, code fails when it reaches the below code
final Document document = docBuilder.parse(templateStream);

Attached are screen shots that show location of the xml and dtd in the pproject and sand-box.



Attachment(s):
location of dtd and xml after plugin is installed.png
location of dtd and xml in project.png
0
Comment actions Permalink

Sorry for delay. Please try "template.dtd" as 2nd parameter.

0
Comment actions Permalink

Hi,

Even that does not work...

0
Comment actions Permalink

Does "/template.dtd" work then? Can you share the sources of your plugin?

0
Comment actions Permalink

Plugin xml:

 
 <extensions defaultExtensionNs="com.intellij">
 <applicationService serviceImplementation="org.myproject.settings.MytemplatePersistent"
                            serviceInterface="org.myproject.settings.MytemplatePersistent"/>
 
 <standardResourceProvider implementation="org.myproject.settings.MyTemplateSchemaProvider"/>

*************************************************************************************************************************************************************************************

 
 
 MyTemplateSchemaProvider:
 

public class MyTemplateSchemaProvider implements StandardResourceProvider {
    private static final Logger LOGGER = Logger.getInstance(MyTemplateSchemaProvider.class.getName());
    @Override
    public void registerResources(ResourceRegistrar resourceRegistrar) {
        LOGGER.info("in register");
        //VirtualFile vf = getPluginVirtualDirectory();
        resourceRegistrar.addStdResource("file://myProject_idea12/classes/template.dtd", "/template.dtd", getClass());
        LOGGER.info("after register");
    }
}

*************************************************************************************************************************************************************************************
MytemplatePersistent :

@State(
        name = "MytemplatePersistent",    // must be equal to the class name
        storages = {
                @Storage(id = "myproject_templates", file = "$APP_CONFIG$/myproject_templates.xml")
        }
)

public class MytemplatePersistent implements PersistentStateComponent<MytemplatePersistent> {

     List<MyTemplateDetails> myTemplateDetailsList=new ArrayList<MyTemplateDetails>();

    //Map<String,MyTemplateDetails>  myTemplateDetailsMap=new HashMap<String, MyTemplateDetails>();
    public MytemplatePersistent getState() {
        System.out.println("+++++++++++++++==getState");
        return this;
    }
    public static MytemplatePersistent getInstance() {
        return ServiceManager.getService(MytemplatePersistent.class);      // this returns null
    }

    public void loadState(MytemplatePersistent mytemplatePersistent) {
        System.out.println("+++++++++++++++==load");
        XmlSerializerUtil.copyBean(mytemplatePersistent, this);

    }

    public List<MyTemplateDetails> getMyTemplateDetailsList() {
        return myTemplateDetailsList;
    }

    public void setMyTemplateDetailsList(List<MyTemplateDetails> myTemplateDetailsList) {
        this.myTemplateDetailsList = myTemplateDetailsList;
    }
    public void addTemplate(MyTemplateDetails myTemplateDetails)   {
        this.myTemplateDetailsList.add(myTemplateDetails)  ;
    }
    public List<MyTemplateDetails> getTemplate()   {
        return this.myTemplateDetailsList;
    }
 }
 
 
 
 public class MySettingsConfigurable extends SearchableConfigurable.Parent.Abstract {
    private List<Configurable> myPanels=new ArrayList<Configurable>();

    @Override
    protected Configurable[] buildConfigurables() {
        myPanels.add(new TemplateSettingsConfigurable());
        myPanels.add(new DBTemplateSettingsConfigurable());
        myPanels.add(new DBDetailsConfigurable());
        return myPanels.toArray(new Configurable[myPanels.size()]);
    }

    @Override
    public String getId() {
        return "";
    }

    @Nls
    @Override
    public String getDisplayName() {
        return "MyProject";
    }

    @Nullable
    @Override
    public String getHelpTopic() {
        return null;
    }

}

*************************************************************************************************************************************************************************************

public class TemplateSettingsConfigurable implements Configurable {

    private JTable table1;
    private JButton addButton;
    private JButton editButton;
    private JButton deleteButton;
 .
 .
 .
 .
 .
 .
 @Override
    public JComponent createComponent() {
        /// myPatternsModel=(AbstractTableModel)table1.getModel();
        LOGGER.log(Level.INFO, "createComponent");
        MytemplatePersistent mytemplatePersistent = MytemplatePersistent.getInstance();
        if (mytemplatePersistent == null || mytemplatePersistent.getMyTemplateDetailsList().size() == 0) {
            try {
                LOGGER.log(Level.INFO, "createComponent...inside if");
                new XMLUtil().initialiseSettings(TEMPLATE_PREFERENCE_NAME.ALL_TEMPLATES.toString(), false, "templates-config.xml");
                mytemplatePersistent = MytemplatePersistent.getInstance();
            } catch (Exception e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
        }
        LOGGER.log(Level.INFO, "createComponent...after if");
        final MytemplatePersistent mytemplatePersistentFinalObj = mytemplatePersistent;
        details = mytemplatePersistent.getMyTemplateDetailsList();
 .
 .
 .
 .
 .

*************************************************************************************************************************************************************************************

 
 
 
 XMLUtil:

 
  public void initialiseSettings(final String templatePreferenceName, boolean isDbImport, String fileName) throws Exception {
        LOGGER.info("initialiseSettings");
        File base = new File(PathUtil.getJarPathForClass(getClass()));
        File file;
        InputStream in;
        if (base.isDirectory()) {
            LOGGER.info( "initialiseSettings..base.isDir true");
            file = new File(base.getParentFile(), fileName);
            in=new FileInputStream(file)     ;
        } else {
            LOGGER.info( "initialiseSettings...false");
            ZipFile zip = new ZipFile(base);
            in = zip.getInputStream(zip.getEntry(fileName));
            LOGGER.info( "initialiseSettings...in" + in.toString());
        }

       /* final Project currentPrj = ProjectManager.getInstance().getOpenProjects()[0];
        currentPrj.
        InputStream is = getClass().getResourceAsStream("templates-config.xml");*/
        updatePersistentStateComponent(in, TEMPLATE_PREFERENCE_NAME.getTemplatePrefix(templatePreferenceName),
                templatePreferenceName, false, isDbImport);
        LOGGER.info( "initialiseSettings...updated");
    }
 
 
  public static void updatePersistentStateComponent(final InputStream templateStream, final String templatePrefix,
                                                      final String allTemplatesPreferenceKey, final boolean init, boolean isDbImport) throws Exception {
        //TemplateUtil.templatePrefix = templatePrefix;
        final List<TemplateSettingsContainer> templateSettingsContainers = new ArrayList<TemplateSettingsContainer>();

        final String allTemplates = getTemplateSettings(templateStream, templatePrefix, templateSettingsContainers, init);
 .
 .
 .
 .
 
 
  private static String getTemplateSettings(final InputStream templateStream, final String templatePrefix,
                                              final List<TemplateSettingsContainer> templateSettingsContainers, final boolean init) throws Exception {

        final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        final Project currentPrj = ProjectManager.getInstance().getOpenProjects()[0];
        InputStream entityStream = null;
        final StringBuilder allTemplates = new StringBuilder(EMPTY_STR);
        try {
            final DocumentBuilder docBuilder = factory.newDocumentBuilder();
 LOGGER.info("before parse");
            final Document document = docBuilder.parse(templateStream);  //execution stops here ---- A
            LOGGER.info("after parse");
 
 .
 .
 .
 ..


*************************************************************************************************************************************************************************************

 
 
 templates-config.xml:

 
 <?xml version="1.0" encoding="UTF-8"?>
<!--<!DOCTYPE templates PUBLIC "//UNKNOWN/" "https://raw.githubusercontent.com/3pintech/fastcode/master/resources/template.dtd">-->
<!DOCTYPE templates SYSTEM "file://myProject_idea12/classes/template.dtd">
 .
 .
 ..
*************************************************************************************************************************************************************************************

 
 
 the above xml and dtd are in the resources folder (refer screen shot attached)
 I build the plugin, prepare for deployment, then install plugin from disc
 when i try to load the settings of my project, ecection stops in XMLUtil.getTemplateSettings  --- docBuilder.parse

 
 everything woks fine when i use the below in the xml
 <!DOCTYPE templates PUBLIC "//UNKNOWN/" "https://raw.githubusercontent.com/3pintech/fastcode/master/resources/template.dtd">

 
 but now i want to change it to refer to the dtd present along with the xml in plugin resources.



Attachment(s):
location of dtd and xml in project.png
0
Comment actions Permalink

It works for me. You don't have to reference any "real" DTD file anywhere, you must use Public ID/URI which then will be mapped automatically by IDE to the local file.



Attachment(s):
dtd.png
0
Comment actions Permalink

Hi

I made the below changes

in my xml:

<!DOCTYPE templates PUBLIC "//UNKNOWN/" "http://fastcode/resources/template.dtd">

********************************************************************************************************************************************************

MyTemplateSchemaProvider :

public class MyTemplateSchemaProvider implements StandardResourceProvider {
    private static final Logger LOGGER = Logger.getInstance(MyTemplateSchemaProvider.class.getName());
    @Override
    public void registerResources(ResourceRegistrar resourceRegistrar) {
        LOGGER.info("in register");
        //VirtualFile vf = getPluginVirtualDirectory();
        resourceRegistrar.addStdResource("http://fastcode/resources/template.dtd", "template.dtd", getClass());
        LOGGER.info("after register");
    }
}

********************************************************************************************************************************************************

It throws

java.net.ConnectException: Connection timed out

(when the code to parse the xml arrives)

********************************************************************************************************************************************************

In the log i find the trace given in MyTemplateSchemaProvider class, but there is a warning

INFO - MyTemplateSchemaProvider - in register
2015-12-17 16:10:46,734 [   8310]   INFO - MyTemplateSchemaProvider - after register
2015-12-17 16:10:46,740 [   8316]   WARN - pl.ExternalResourceManagerImpl - Cannot find standard resource. filename:template.dtd class=null
2015-12-17 16:10:46,828 [


My resoures folder looks like a normal folder. The src and resource have the same icon (attached the screen shot). In yours,, the icon for resources folder is different from that of src. Is it some theing special about it. Could this cause any trouble.



Attachment(s):
resources.png
0
Comment actions Permalink

Right click on folder, choose "Mark Directory As" -> "Resources Root"


0
Comment actions Permalink

It is marked as source root

I tried to unmark as source root

but i dont see option mark as resource root


I m using intellij 12.0.1



Attachment(s):
mark dir res.png
0
Comment actions Permalink

Make sure in compiler options ".dtd" is included in "Resource patterns" to be copied and check your compiler output/JAR.

0
Comment actions Permalink

I changed the compiler options still same



Attachment(s):
jar structure.png
project settings.png
compiler settings.png
0
Comment actions Permalink

Could you try to reproduce if it works with IntelliJ IDEA 15?


0
Comment actions Permalink

The same error ...it is not able to find the file.

1.) I installed the zip file created from idea 12 --- got the above error.
2.) took the code to idea 15, built it and created the zip, installed it, it did not get installed at all --- i cannot see my menu nor settings.....no error..

0
Comment actions Permalink

Please try this sample plugin (adjust IntelliJ SDK path in project settings)



Attachment(s):
dtdResolve.zip
0
Comment actions Permalink

Hi

In intellij 12, i get the same error
INFO - s.impl.stores.FileBasedStorage - Document was not loaded for $APP_CONFIG$/plainTextFiles.xml file is null
2016-01-14 09:32:05,089 [   8653]   INFO - pl.PushedFilePropertiesUpdater - File properties pushed in 5 ms
2016-01-14 09:32:05,183 [   8747]   INFO -       MyTemplateSchemaProvider - in register
2016-01-14 09:32:05,183 [   8747]   INFO -       MyTemplateSchemaProvider - after register
2016-01-14 09:32:05,188 [   8752]   WARN - pl.ExternalResourceManagerImpl - Cannot find standard resource. filename:myDtd.dtd class=null
2016-01-14 09:32:05,277 [   8841]   INFO - s.impl.stores.FileBasedStorage - Document was not loaded for $APP_CONFIG$/applicationLibraries.xml file is null
2016-01-14 09:32:05,363 [   8927]   


In intellij 15

i dont see the trace at all. so i dont know if it is even executing the particular code.

0
Comment actions Permalink

I'm sorry but I'm out of ideas. It works for me (and many other plugins) so it must be a configuration/setup error in your project.

0

Please sign in to leave a comment.