而 FastInfoset 作為一種標準的 XML 序列化手段,對 XML 在二進位基礎上的壓縮做到了優秀的支援,但是缺乏工具直接閱讀,對使用者造成了一定的困難,利用 Notepad++ 的外掛程式功能和 JAVA JNI 技術,結合兩者的特點,直 接把 FastInfoset 檔在 Notepad++ 中打開,來解決使用者二次編輯的麻煩,更可以充分地利用 Notepad++ 對 XML 檔編輯的優勢。
FastInfoset定制了一種在二進位級別處理 XML 資訊集合的方式,通過這種特殊的序列化,與普通的 XML 文檔相比,無論在大小和解析速度上都有特殊的優勢。
Notepad++是一款優秀的HTTP://www.aliyun.com/zixun/aggregation/18444.html">文本編輯軟體,開源、免費和多種外掛程式支援使得它成為最流行的編輯軟體之一, 其中強大的外掛程式功能可以讓 Notepad++ 對各種文字檔良好的支援,如下圖所示 :
圖 1. Notepad++ 展示 XML
對 XML 而言,優秀的處理能力體現在標籤著色和樹形折疊,但是 Notepad++不能直接打開 FastInfoset 文檔,本文就是已解決這個問題為目的,利用 Notepad++的外掛程式技術,來直接打開和保存 FastInfoset文檔。 包括以下幾個問題:
打開、存儲 FastInfoset 的庫由 JAVA 提供,必須建立 JNI 的調用結構來管理代碼; 外掛程式的開發模型,包括重點使用的函數,調用的流程和開發代碼; JNI 調用。
FastInfoset 檔處理
FastInfoset 技術簡述
FastInfoset利用現實當中 XML 檔當中大量存在的重複資訊的特點,比如前面提到的 books.xml,可以看到大量重複的標籤,然後利用這種各種處理字元的技術,壓縮 XML 檔和提高存取速度。 這些技術包括動態表、原始詞集合和外部詞彙表等等。
打開和保存 FastInfoset
來自 GlassFish 的技術中的一部分,使用者可以訪問 HTTP://fi.java.net 來獲得 Fast Infoset的技術支援,由於 JAVA 的技術,FastInfoset 的介面都是使用 JAVA 編寫, 其中的部分代碼如下所示:
打開 FastInfoset 文檔
清單 1. 打開 FastInfoset文檔
DefaultContentHandler builder = new DefaultContentHandler(); Instantiate the FI SAX parser XMLReader saxReader = new SAXDocumentParser(); saxReader.setContentHandler(builder); Parse the fast infoset document InputSource inputSource = new InputSource(stream); saxReader.parse(inputSource); 保存 FastInfoset 文檔
清單 2. 保存 FastInfoset文檔
final java.io.StringReader reader = new java.io.StringReader(saveString); Get the input stream for the XML document InputStream xmlDocument = new InputStream(){ @Override public int read() thro ws IOException { // TODO Auto-generated method stub return reader.read(); } }; Set up output stream for fast infoset document OutputStream fiDocument = new FileOutputStream(new File(filePath)); Create Fast Infoset SAX serializer SAXDocumentSerializer saxDocumentSerializer = new SAXDocumentSerializer(); Set the output stream saxDocumentSerializer.setOutputStream(fiDocument); Instantiate JAXP SAX parser factory SAXParserFactory saxParserFactory = SAXParserFactory.newInstance(); /* Set parser to be namespace aware * Very important to do otherwise invalid FI documents will be * created by the SAXDocu mentSerializer */ saxParserFactory.setNamespaceAware(true); Instantiate the JAXP SAX parser SAXParser saxParser = saxParserFactory.newSAXParser(); Set the lexical handler saxParser.setProperty("HTTP://xml.org/sax/properties/lexical-handler", new FastInfosetDefaultHandler()); Parse the XML document and convert to a fast infoset document saxParser.parse(xmlDocument, saxDocumentSerializer);