日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

[代码阅读] ECS toString实现方法

發布時間:2023/12/10 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [代码阅读] ECS toString实现方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

引言

??? ECS 提供了一種編程方式來生成以不同標記語言編寫的文檔。它設計為通過面向對象的抽象來生成所有標簽。
??? ECS 目前版本為1.4.2 ,支持 HTML 4.0 和 XML 。

??? 因為工作原因,作者粗略讀了ECS的部分原代碼,著重了解ECS如果通過toString方法實現HTML代碼的生成。如有不足之處,請指出。

前期準備

??? 下載Jakarta ECS http://jakarta.apache.org/ecs

開始

??? ECS 將HTML的標簽都做為一個JavaBean實現,放在org.apache.ecs.html下,每個元素都有相應的getter和setter方法實現對象屬性的存取。并通過toString方法將元素轉化為標準的html代碼。
??? toString方法是如何實現的呢?我們先看看ECS主要類結構:

???



??? 原來任何元素都是從ConcreteElement繼承過來的,而ConcreteElement又繼承了ElementAttributes。 ConcreteElement實現了元素addElement方法,ElementAttributes實現了元素addAttribute的方法。
??? ConcreteElement和ElementAttributes都采用了hashtable的方法存取數據。我們看看它們的關鍵代碼:

??? ConcreteElement關鍵代碼:
???
以下內容為程序代碼:

??????? private Hashtable registry = new Hashtable(4); // keep a list of elements that need to be added to the element
???????
??????? private Vector registryList = new Vector(2);

??????? ……

???????
??????? public Element addElementToRegistry(String hashcode,Element element)
??????? {
??????????? if ( hashcode == null || element == null )
??????????????? return(this);

??????????? element.setFilterState(getFilterState());
??????????? if(ECSDefaults.getDefaultPrettyPrint() != element.getPrettyPrint())
??????????????? element.setPrettyPrint(getPrettyPrint());
??????????? registry.put(hashcode,element);
??????????? if(!registryList.contains(hashcode))
??????????????? registryList.addElement(hashcode);
??????????? return(this);
??????? }
??????? ……
???

???
??? ElementAttributes 和ConcreteElement不同,hashtable的聲明是在超類GenericElement中實現的。
??? ElementAttributes關鍵代碼:
???
以下內容為程序代碼:

??????? public Element addAttribute(String s, int i)
??????? {
??????????? getElementHashEntry().put(s, new Integer(i));
??????????? return this;
??????? }
???

???
??? GenericElement關鍵代碼:
???
以下內容為程序代碼:

??????? private Hashtable element;
??????? ……
??????? public GenericElement()
??????? {
??????? ……
??????????? element = new Hashtable(4);
??????????? ……
??????? }
??????? ……
??????? protected Hashtable getElementHashEntry()
??????? {
??????????? return element;
??????? }
???

???
??? 元素的toString的真正實現也在GenericElement里
???
以下內容為程序代碼:

??????? public final String toString()
??????? {
??????????? StringWriter stringwriter = new StringWriter();
??????????? String s = null;
??????????? try
??????????? {
??????????????? output(stringwriter);
??????????????? stringwriter.flush();
??????????????? s = stringwriter.toString();
??????????????? stringwriter.close();
??????????? }
??????????? catch(UnsupportedEncodingException unsupportedencodingexception)
??????????? {
??????????????? unsupportedencodingexception.printStackTrace();
??????????? }
??????????? catch(IOException ioexception)
??????????? {
??????????????? ioexception.printStackTrace();
??????????? }
??????????? return s;
???????

轉載于:https://www.cnblogs.com/vanuan/archive/2005/12/23/9576191.html

總結

以上是生活随笔為你收集整理的[代码阅读] ECS toString实现方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。