DevFaqNewXMLFileTypeJa

XML タイプに異なる拡張子でサポートを追加するには?

(上のタイトルは英語ページにはありませんが、他のページと体裁を揃えるため日本語翻訳ページで独自に追加しました。)

> I want to add some special
> functionality to the initial handling of xml files with a xul extension, but
> then allow users to use the existing NB functionality for editing,
> validation etc. 
>
> Did you solve this without patching?
> xul という拡張子を持つ XML ファイルの初期の扱いに特別な機能をいくつか足したいのですが、
> その後、ユーザが編集したり妥当性を検査したりするときには、既存の NetBeans の機能を使わせたいです。
>
> これをパッチすることなく解決できますか?

In 5.0, it can be done without a patch. It does, however, require an impl dependency on xml-core. I'd really like this to be simpler, and a supported API.

NetBeans 5.0 では、これをパッチすることなしに実現できます。 しかし、xml-core への実装上の依存が必要とされます。 私はこれがもっとシンプルだと良いと思いますし、API でサポートされて欲しいです。

Anyway, here's how I do it for an xml flavor called SCXML:

とにかく、私が XML の一種で SCXML と呼ばれるものに対して行ったことをお見せしましょう。

1. in the manifest:

1. マニフェストファイル

Name: com/nuance/tools/xhmi/ScxmlDataLoader.class
Install-Before: org.openide.loaders.XMLDataObject, 
org.netbeans.modules.xml.core.XMLDataObject
OpenIDE-Module-Class: Loader

2. The loader:

2. ローダー

import java.io.IOException;
import org.netbeans.modules.xml.core.XMLDataLoader;
import org.openide.filesystems.FileObject;
import org.openide.loaders.DataObjectExistsException;
import org.openide.loaders.MultiDataObject;
import org.openide.loaders.UniFileLoader;
import org.openide.util.NbBundle;

public class ScxmlDataLoader extends UniFileLoader
{
   
    public static final String REQUIRED_MIME = "application/scxml+xml";
   
    private static final long serialVersionUID = 1L;
   
    public ScxmlDataLoader()
    {
        super("com.nuance.tools.xhmi.ScxmlDataObject");
    }
   
    protected String defaultDisplayName()
    {
        return NbBundle.getMessage(ScxmlDataLoader.class, "LBL_Scxml_loader_name");
    }
   
    protected void initialize()
    {
        super.initialize();
        getExtensions().addMimeType(REQUIRED_MIME);
    }
   
    protected MultiDataObject createMultiObject(FileObject primaryFile) throws DataObjectExistsException, IOException
    {
        return new ScxmlDataObject(primaryFile, this);
    }
   
    protected MultiDataObject.Entry createPrimaryEntry (MultiDataObject obj, FileObject primaryFile) {
        return new XMLDataLoader.XMLFileEntry (obj, primaryFile); //adds smart templating
    }
   
    protected String actionsContext()
    {
        return "Loaders/" + REQUIRED_MIME + "/Actions";
    }
}


3. The data object:

3. データオブジェクト

import java.io.IOException;
import org.netbeans.modules.xml.core.XMLDataObjectLook;
import org.netbeans.modules.xml.core.cookies.DataObjectCookieManager;
import org.netbeans.modules.xml.core.sync.DataObjectSyncSupport;
import org.netbeans.modules.xml.core.sync.Synchronizator;
import org.netbeans.modules.xml.core.text.TextEditorSupport;
import org.netbeans.spi.xml.cookies.CheckXMLSupport;
import org.netbeans.spi.xml.cookies.DataObjectAdapters;
import org.netbeans.spi.xml.cookies.ValidateXMLSupport;
import org.openide.filesystems.FileObject;
import org.openide.loaders.DataObjectExistsException;
import org.openide.loaders.MultiDataObject;
import org.openide.nodes.CookieSet;
import org.openide.nodes.Node;
import org.openide.text.DataEditorSupport;
import org.xml.sax.InputSource;

public class ScxmlDataObject extends MultiDataObject implements 
XMLDataObjectLook
{
    private transient final DataObjectCookieManager cookieManager;
    private transient Synchronizator synchronizator;

    public ScxmlDataObject(FileObject pf, ScxmlDataLoader loader) throws DataObjectExistsException, IOException {
        super(pf, loader);
        CookieSet cookies = getCookieSet();
        cookieManager = new DataObjectCookieManager (this, cookies);

        cookies.add((Node.Cookie) DataEditorSupport.create(this, getPrimaryEntry(), cookies));
       
        InputSource is = DataObjectAdapters.inputSource(this);
        cookies.add(new CheckXMLSupport(is));
        cookies.add(new ValidateXMLSupport(is));
       
        // editor support defines MIME type understood by EditorKits registry        
        TextEditorSupport.TextEditorSupportFactory editorFactory =
            new TextEditorSupport.TextEditorSupportFactory (this, org.netbeans.modules.xml.core.XMLDataObject.MIME_TYPE);
        editorFactory.registerCookies (cookies);

    }
   
    protected Node createNodeDelegate() {
        return new ScxmlDataNode(this);
    }
   
    ////////// XMLDataObjectLook interface /////////////////
    public DataObjectCookieManager getCookieManager() {
        return cookieManager;
    }
   
    public synchronized Synchronizator getSyncInterface() {
        if (synchronizator == null) {
            synchronizator = new DataObjectSyncSupport (ScxmlDataObject.this);
        }
        return synchronizator;
    }
}

4. The layer file:

4. レイヤーファイル

<filesystem>
    <folder name="Loaders">
        <folder name="application">
            <folder name="scxml+xml">
                <folder name="Actions">
                    <file name="org-openide-actions-OpenAction.instance"/>
                    <attr name="org-openide-actions-OpenAction.instance/org-openide-actions-FileSystemAction.instance" boolvalue="true"/>
                    <file name="org-openide-actions-FileSystemAction.instance"/>
                    <attr name="org-openide-actions-FileSystemAction.instance/sep-1.instance" boolvalue="true"/>
                    <file name="sep-1.instance">
                        <attr name="instanceClass" stringvalue="javax.swing.JSeparator"/>
                    </file>
                    <attr name="sep-1.instance/org-openide-actions-CutAction.instance" boolvalue="true"/>
                    <file name="org-openide-actions-CutAction.instance"/>
                    <attr name="org-openide-actions-CutAction.instance/org-openide-actions-CopyAction.instance" boolvalue="true"/>
                    <file name="org-openide-actions-CopyAction.instance"/>
                    <attr name="org-openide-actions-CopyAction.instance/sep-2.instance" boolvalue="true"/>
                    <file name="sep-2.instance">
                        <attr name="instanceClass" stringvalue="javax.swing.JSeparator"/>
                    </file>
                    <attr name="sep-2.instance/org-openide-actions-DeleteAction.instance" boolvalue="true"/>
                    <file name="org-openide-actions-DeleteAction.instance"/>
                    <attr name="org-openide-actions-DeleteAction.instance/org-openide-actions-RenameAction.instance" boolvalue="true"/>
                    <file name="org-openide-actions-RenameAction.instance"/>
                    <attr name="org-openide-actions-RenameAction.instance/sep-3.instance" boolvalue="true"/>
                    <file name="sep-3.instance">
                        <attr name="instanceClass" stringvalue="javax.swing.JSeparator"/>
                    </file>
                    <attr name="sep-3.instance/org-openide-actions-SaveAsTemplateAction.instance" boolvalue="true"/>
                    <file name="org-openide-actions-SaveAsTemplateAction.instance"/>
                    <attr name="org-openide-actions-SaveAsTemplateAction.instance/sep-4.instance" boolvalue="true"/>
                    <file name="sep-4.instance">
                        <attr name="instanceClass" stringvalue="javax.swing.JSeparator"/>
                    </file>
                    <attr name="sep-4.instance/org-openide-actions-ToolsAction.instance" boolvalue="true"/>
                    <file name="org-openide-actions-ToolsAction.instance"/>
                    <attr name="org-openide-actions-ToolsAction.instance/org-openide-actions-PropertiesAction.instance" boolvalue="true"/>
                    <file name="org-openide-actions-PropertiesAction.instance"/>
                </folder>
            </folder>
        </folder>
    </folder>
    <folder name="Services">
        <folder name="MIMEResolver">
            <file name="ScxmlResolver.xml" url="resources/ScxmlResolver.xml">
                <attr name="SystemFileSystem.localizingBundle" stringvalue="com.nuance.tools.xhmi.Bundle"/>
            </file>
        </folder>
    </folder>
    <folder name="Templates">
        <folder name="Other">
            <file name="ScxmlTemplate.scxml" url="resources/ScxmlTemplate.scxml">
                <attr name="SystemFileSystem.localizingBundle" stringvalue="com.nuance.tools.xhmi.Bundle"/>
                <attr name="template" boolvalue="true"/>
            </file>
        </folder>
    </folder>

5. the MIME resolver:

5. MIME リゾルバ

<MIME-resolver>
    <file>
        <ext name="scxml"/>
        <resolver mime="application/scxml+xml"/>
    </file>
</MIME-resolver>

Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo