You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TextDocumentFacade.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.hwpf.converter;
  16. import org.apache.poi.util.Beta;
  17. import org.w3c.dom.Document;
  18. import org.w3c.dom.Element;
  19. import org.w3c.dom.Text;
  20. @Beta
  21. public class TextDocumentFacade
  22. {
  23. protected final Element body;
  24. protected final Document document;
  25. protected final Element head;
  26. protected final Element root;
  27. protected Element title;
  28. protected Text titleText;
  29. public TextDocumentFacade( Document document )
  30. {
  31. this.document = document;
  32. root = document.createElement( "html" );
  33. document.appendChild( root );
  34. body = document.createElement( "body" );
  35. head = document.createElement( "head" );
  36. root.appendChild( head );
  37. root.appendChild( body );
  38. title = document.createElement( "title" );
  39. titleText = document.createTextNode( "" );
  40. head.appendChild( title );
  41. }
  42. public void addAuthor( String value )
  43. {
  44. addMeta( "Author", value );
  45. }
  46. public void addDescription( String value )
  47. {
  48. addMeta( "Description", value );
  49. }
  50. public void addKeywords( String value )
  51. {
  52. addMeta( "Keywords", value );
  53. }
  54. public void addMeta( final String name, String value )
  55. {
  56. Element meta = document.createElement( "meta" );
  57. Element metaName = document.createElement( "name" );
  58. metaName.appendChild( document.createTextNode( name + ": " ) );
  59. meta.appendChild( metaName );
  60. Element metaValue = document.createElement( "value" );
  61. metaValue.appendChild( document.createTextNode( value + "\n" ) );
  62. meta.appendChild( metaValue );
  63. head.appendChild( meta );
  64. }
  65. public Element createBlock()
  66. {
  67. return document.createElement( "div" );
  68. }
  69. public Element createHeader1()
  70. {
  71. Element result = document.createElement( "h1" );
  72. result.appendChild( document.createTextNode( " " ) );
  73. return result;
  74. }
  75. public Element createHeader2()
  76. {
  77. Element result = document.createElement( "h2" );
  78. result.appendChild( document.createTextNode( " " ) );
  79. return result;
  80. }
  81. public Element createParagraph()
  82. {
  83. return document.createElement( "p" );
  84. }
  85. public Element createTable()
  86. {
  87. return document.createElement( "table" );
  88. }
  89. public Element createTableBody()
  90. {
  91. return document.createElement( "tbody" );
  92. }
  93. public Element createTableCell()
  94. {
  95. return document.createElement( "td" );
  96. }
  97. public Element createTableRow()
  98. {
  99. return document.createElement( "tr" );
  100. }
  101. public Text createText( String data )
  102. {
  103. return document.createTextNode( data );
  104. }
  105. public Element createUnorderedList()
  106. {
  107. return document.createElement( "ul" );
  108. }
  109. public Element getBody()
  110. {
  111. return body;
  112. }
  113. public Document getDocument()
  114. {
  115. return document;
  116. }
  117. public Element getHead()
  118. {
  119. return head;
  120. }
  121. public String getTitle()
  122. {
  123. if ( title == null )
  124. return null;
  125. return titleText.getTextContent();
  126. }
  127. public void setTitle( String titleText )
  128. {
  129. if ( AbstractWordUtils.isEmpty( titleText ) && this.title != null )
  130. {
  131. this.head.removeChild( this.title );
  132. this.title = null;
  133. this.titleText = null;
  134. }
  135. if ( this.title == null )
  136. {
  137. this.title = document.createElement( "title" );
  138. this.titleText = document.createTextNode( titleText );
  139. this.title.appendChild( this.titleText );
  140. this.head.appendChild( title );
  141. }
  142. this.titleText.setData( titleText );
  143. }
  144. }