[代码阅读] 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实现方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring 3整合Quartz 2实现
- 下一篇: 巧克力情歌手---McKnight, B