【问题标题】:What protocols are allowed in hrefs in Swing's HTMLEditorKit class?Swing 的 HTMLEditorKit 类中的 href 中允许哪些协议?
【发布时间】:2013-10-27 18:56:33
【问题描述】:

我使用一个应用程序(AppWorx!请有人创建此标签!),它允许以 html 格式输入有关计划作业的文档。

我一直在尝试创建具有如下链接的待命文档:

<a href="tel:+1806xxxxxxx">1 (806) xxx - xxxx</a>

该页面显示在 Java 应用程序本身内,并且任何指向 http:// 的链接都会在用户的浏览器窗口中打开。但是像上面这样的电话链接会导致一个大的错误窗口弹出,显示以下错误:

java.net.MalformedURLException: For input string: "+1806xxxxxxx"
    at java.net.URL.<init>(Unknown Source)
    at java.net.URL.<init>(Unknown Source)
    at java.net.URL.<init>(Unknown Source)
    at com.appworx.client.screen.modmgr.M$2.hyperlinkUpdate(NotesPanel.java:191)
    at javax.swing.JEditorPane.fireHyperlinkUpdate(Unknown Source)
    at javax.swing.text.html.HTMLEditorKit$LinkController.activateLink(Unknown Source)
    at javax.swing.text.html.HTMLEditorKit$LinkController.mouseClicked(Unknown Source)
    at java.awt.AWTEventMulticaster.mouseClicked(Unknown Source)

其他协议也会失败(http 除外)。如果我有一个 mailto: 链接,而不是收到上述错误,它会将我带到电子邮件地址的域部分。

我相信编译该应用程序所使用的此类的任何版本都已存在数年(也许很多年)。

谁能告诉我这个类的限制是什么,或者是否存在解决方法?

Appworx 的文档表明,除非从 jnlp 调用应用程序,否则即使是 http 链接也不起作用(这是否是沙盒的东西?)。尽管如此,这里没有人以任何其他方式启动应用程序。

【问题讨论】:

  • "..links 将不起作用,除非从 jnlp 调用应用程序(这是否是沙盒的东西?)" 他们可能正在使用JNLP API 打开 URL(1)。该 API 仅适用于应用程序。使用 JWS 启动。 1) 例如如demo. of the BasicService 中所见(使用BasicService.showDocument(URL))。
  • 这不值得作为答案,但是 tinyurl 链接可以工作,而且它们显然可以重定向到其他协议。

标签: java html swing protocols htmleditorkit


【解决方案1】:

谁能告诉我这个类的限制是什么?

为了让您了解 EditorKit 类的过时程度,从 Java 7 开始 HTMLEditorKit支持 HTML 3.2 版(带有一些扩展),并且正在向 4.0 版迁移”。毫不奇怪,URL 类(负责您发布的跟踪中的 MalformedURLException)仅支持基本协议,其中一些您在上面提到:

  • http
  • https
  • FTP
  • 文件
  • 罐子

谁能告诉我是否存在解决方法?

好吧,您可以亲自动手编写代码(如果您可以访问它),使用自定义协议处理程序并注册它。幸运的是,tel 协议已经有了一个,由J2ME Polish project 提供:

package de.enough.polish.browser.protocols;

import java.io.IOException;

import javax.microedition.io.StreamConnection;
import javax.microedition.midlet.MIDlet;

import de.enough.polish.browser.ProtocolHandler;

/**
 * Protocol handler to handle the <code>tel:</code> protocol. This class calls the given phonenumber on MIDP 2.0 phones.
 * Example: &lt;a href=&quot;tel:+441231234567#22&quot;&gt;Call Me&lt;/a&gt;
 * Note that this handler requires MIDP 2.0 or higher.
 * The tel protocol handler allows you to separate the phone number and the dialtone (dtmf) that should be send after
 * establishing the phone call using the '#' sign.
 * 
 * This protocol could actually be realized using the ExternalProtocolHandler as well, however in this
 * way we can deal post dial tones (DTMF) in a better way - in the HTML code they just need to be 
 * separated from the phonenumber using a '#'.
 */
public class TelProtocolHandler
extends ProtocolHandler
{
    private MIDlet midlet;

    /**
     * Creates an TellProtocolHandler object using the default "tel" protocol name.
     * 
     * @param midlet the midlet object of the application
     */
    public TelProtocolHandler(MIDlet midlet)
    {
        this( "tel", midlet );
    }

    /**
     * Creates an TelProtocolHandler object using the specified protocol name.
     * 
     * @param protocolName the name of the protocol to handle
     * @param midlet the midlet object of the application
     */
    public TelProtocolHandler(String protocolName, MIDlet midlet)
    {
        super( protocolName );
        this.midlet = midlet;
    }


    /* (non-Javadoc)
     * @see de.enough.polish.browser.ProtocolHandler#getConnection(java.lang.String)
     */
    public StreamConnection getConnection(String url) throws IOException
    {
        this.midlet.platformRequest( "tel:" + extractMsisdn(url));
        return null;
    }

    /**
     * Strips the MSISDN part off an url. 
     * In contrast to other protocol handlers, the external protocol handler only uses a single colon to
     * separate the external protocol from the folllowing protocol, e.g. external:http://www.j2mepolish.org
     * 
     * @param url the url to remove the protocol from
     * 
     * @return the host and part part of the given url
     */
    protected String extractMsisdn(String url)
    {
        String msisdn = url.substring(this.protocolName.length() + 1);
        String separator = null;
        //#if polish.dtmf.separator:defined
            //#= separator = "${polish.dtmf.separator}";
            //# if (!separator.equals("#")) {
                //# int pos = msisdn.indexOf('#');
                //# if (pos != -1) {
                    //# msisdn = msisdn.substring(0, pos) + separator + msisdn.substring(pos + 1); 
                //# }
            //# }
        //#endif
        return msisdn;
    }

}

希望对您有所帮助!

【讨论】:

  • 这确实有帮助。我会检查一下我们对代码的访问权限。
  • @JohnO 感谢您的接受。我很高兴听到这个。我应该补充一下,我没有从项目的 github 站点获取代码(现在找不到实际的 URL;抱歉),以防您发现它有点过时。
  • 不用谢...您明确回答了问题,给出了解决方案(我是否可以实施),并解决了问题。没有比这更好的答案了。
  • @JohnO 好吧,我希望你/你的团队可以修改代码。与此同时,我发现URL。它已经 5 岁了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-22
  • 1970-01-01
  • 1970-01-01
  • 2017-12-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多