java html格式嵌套,JavaSwing组件如何显示嵌套了svg的html?

家里网络访问不了sourceforge的网站……。不然很想尝试在swing中嵌入浏览器哪个djproject,看看是否支持svg。

我用JEditorPane时,使用底层的一些api,可以访问内嵌svg的html文件了。

思想如下:

使用HttpConnection访问url(JEditorPane底层也是这样访问url的)获取响应内容,然后提取embed、object中包含svg文件的标签,做特殊处理,处理步骤如下:

1.先通过svg的地址获取的资源路径,然后使用batik处理svg,转换为jpg格式的图片

2.将该jpg图片保存到一台图片服务器中(tomcat也可),并返回jpg路径

3.将原html内容中的embed、object中包含svg文件的标签内容替换为img标签,并把jpg路径放到src属性上

4.把新的html响应内容使用JEditorPane底层的解析类处理

代码如下(写的比较粗糙,主要是使用HTMLDocument、Parser、ParserCallback这几个底层的类):

importjava.awt.BorderLayout;importjava.io.BufferedReader;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.InputStreamReader;importjava.io.Reader;importjava.io.StringReader;importjava.net.URL;importjavax.swing.JEditorPane;importjavax.swing.JFrame;importjavax.swing.text.html.HTMLEditorKit.Parser;importjavax.swing.text.html.HTMLEditorKit.ParserCallback;importjavax.swing.text.html.HTMLDocument;importorg.apache.batik.transcoder.TranscoderInput;importorg.apache.batik.transcoder.TranscoderOutput;importorg.apache.batik.transcoder.image.JPEGTranscoder;importorg.dom4j.Document;importorg.dom4j.DocumentException;importorg.dom4j.DocumentHelper;importorg.dom4j.Element;publicclassTestJEditorPaneextendsJFrame{    /**    *    */    privatestaticfinallongserialVersionUID=7429079353774653739L;    /**    *@paramargs    */    publicstaticvoidmain(String[]args)throwsException{        testJEditorPane();    }        publicstaticvoidtestJEditorPane()throwsException{        JFrameframe=newJFrame();        frame.setTitle("Test");                URLurl=newURL("http://localhost:8889/springwork/user/login.do");                ReaderHTMLReader=newInputStreamReader(url.openConnection()                .getInputStream());        BufferedReaderbr=newBufferedReader(HTMLReader);        Stringline="";        Stringjpg="";        StringBuffersb=newStringBuffer();        while((line=br.readLine())!=null){            if(line.indexOf("embed")!=-1){                StringcontextPath=url.getProtocol()+"://"+url.getHost()+":"+url.getPort()+"/"+getAttribute(line,"src");                System.out.println(contextPath);                jpg=getImagePath(contextPath);                line="";            }            sb.append(line);            sb.append("\n");        }        System.out.println(sb.toString());        JEditorPanejp=newJEditorPane();        try{            jp.setSize(400,300);            jp.setPage(url);        }catch(IOExceptione){            e.printStackTrace();        }            HTMLDocumenthdoc=(HTMLDocument)jp.getDocument();    Parserp=hdoc.getParser();    ParserCallbackreceiver=hdoc.getReader(0);    BooleanignoreCharset=(Boolean)jp.getDocument().getProperty("IgnoreCharsetDirective");    p.parse(newStringReader(sb.toString()),receiver,(ignoreCharset==null)?false:ignoreCharset.booleanValue());    receiver.flush();            frame.getContentPane().add(jp,BorderLayout.CENTER);        frame.setSize(400,300);        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        frame.setLocationRelativeTo(null);        frame.pack();        frame.repaint();        frame.setVisible(true);    }        publicstaticStringgetImagePath(StringsvgUrl){        try{            JPEGTranscodert=newJPEGTranscoder();            t.addTranscodingHint(JPEGTranscoder.KEY_QUALITY,newFloat(.8));            //StringsvgURI=newFile("C:/Users/Administrator/Desktop/svg/asf-logo.svg").toURL().toString();            StringsvgURI="http://localhost:8889//springwork/styles/svgs/a_1.svg";            TranscoderInputinput=newTranscoderInput(svgURI);                        StringoutPath="D:/work/eclipse/eclipse4.2/code/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/springwork/styles/svgs/a_1.jpg";            FileOutputStreamostream=newFileOutputStream(outPath);            TranscoderOutputoutput=newTranscoderOutput(ostream);            t.transcode(input,output);            ostream.flush();            ostream.close();                        outPath="http://localhost:8888/springwork/styles/svgs/a_1.jpg";//相对路径,依据具体的图片服务器地址            returnoutPath;        }catch(Exceptione){            e.printStackTrace();            return"";        }    }publicstaticDocumentparseText(Stringstr)throwsException{Documentdocument=null;try{document=DocumentHelper.parseText(str);}catch(DocumentExceptionde){throwde;}catch(Exceptione){throwe;}returndocument;}publicstaticStringgetAttribute(Stringstr,Stringname){    try{        Documentdoc=parseText(str);        Elementele=doc.getRootElement();        returnele.attributeValue("src");    }catch(Exceptione){        return"";    }}}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值