123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296 |
- /* ====================================================================
- Licensed to the Apache Software Foundation (ASF) under one or more
- contributor license agreements. See the NOTICE file distributed with
- this work for additional information regarding copyright ownership.
- The ASF licenses this file to You under the Apache License, Version 2.0
- (the "License"); you may not use this file except in compliance with
- the License. You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- ==================================================================== */
- package org.apache.poi.hwpf.converter;
-
- import java.util.LinkedHashMap;
- import java.util.Map;
-
- import org.apache.poi.util.Beta;
- import org.w3c.dom.Document;
- import org.w3c.dom.Element;
- import org.w3c.dom.Text;
-
- @Beta
- public class HtmlDocumentFacade
- {
- protected final Element body;
- protected final Document document;
- protected final Element head;
- protected final Element html;
-
- /**
- * Map from tag name, to map linking known styles and css class names
- */
- private Map<String, Map<String, String>> stylesheet = new LinkedHashMap<>();
- private Element stylesheetElement;
-
- protected Element title;
- protected Text titleText;
-
- public HtmlDocumentFacade( Document document )
- {
- this.document = document;
-
- html = document.createElement( "html" );
- document.appendChild( html );
-
- body = document.createElement( "body" );
- head = document.createElement( "head" );
- stylesheetElement = document.createElement( "style" );
- stylesheetElement.setAttribute( "type", "text/css" );
-
- html.appendChild( head );
- html.appendChild( body );
- head.appendChild( stylesheetElement );
-
- addStyleClass( body, "b", "white-space-collapsing:preserve;" );
- }
-
- public void addAuthor( String value )
- {
- addMeta( "author", value );
- }
-
- public void addDescription( String value )
- {
- addMeta( "description", value );
- }
-
- public void addKeywords( String value )
- {
- addMeta( "keywords", value );
- }
-
- public void addMeta( final String name, String value )
- {
- Element meta = document.createElement( "meta" );
- meta.setAttribute( "name", name );
- meta.setAttribute( "content", value );
- head.appendChild( meta );
- }
-
- public void addStyleClass( Element element, String classNamePrefix,
- String style )
- {
- String exising = element.getAttribute( "class" );
- String addition = getOrCreateCssClass( classNamePrefix, style );
- String newClassValue = AbstractWordUtils.isEmpty( exising ) ? addition : ( exising + " " + addition );
- element.setAttribute( "class", newClassValue );
- }
-
- protected String buildStylesheet(
- final Map<String, Map<String, String>> prefixToMapOfStyles )
- {
- StringBuilder stringBuilder = new StringBuilder();
- for ( Map<String, String> byPrefix : prefixToMapOfStyles.values() )
- {
- for ( Map.Entry<String, String> byStyle : byPrefix.entrySet() )
- {
- String style = byStyle.getKey();
- String className = byStyle.getValue();
-
- stringBuilder.append( "." );
- stringBuilder.append( className );
- stringBuilder.append( "{" );
- stringBuilder.append( style );
- stringBuilder.append( "}\n" );
- }
- }
- return stringBuilder.toString();
- }
-
- public Element createBlock()
- {
- return document.createElement( "div" );
- }
-
- public Element createBookmark( String name )
- {
- final Element basicLink = document.createElement( "a" );
- basicLink.setAttribute( "name", name );
- return basicLink;
- }
-
- public Element createHeader1()
- {
- return document.createElement( "h1" );
- }
-
- public Element createHeader2()
- {
- return document.createElement( "h2" );
- }
-
- public Element createHyperlink( String internalDestination )
- {
- final Element basicLink = document.createElement( "a" );
- basicLink.setAttribute( "href", internalDestination );
- return basicLink;
- }
-
- public Element createImage( String src )
- {
- Element result = document.createElement( "img" );
- result.setAttribute( "src", src );
- return result;
- }
-
- public Element createLineBreak()
- {
- return document.createElement( "br" );
- }
-
- public Element createListItem()
- {
- return document.createElement( "li" );
- }
-
- public Element createOption( String value, boolean selected )
- {
- Element result = document.createElement( "option" );
- result.appendChild( createText( value ) );
- if ( selected )
- {
- result.setAttribute( "selected", "selected" );
- }
- return result;
- }
-
- public Element createParagraph()
- {
- return document.createElement( "p" );
- }
-
- public Element createSelect()
- {
- return document.createElement( "select" );
- }
-
- public Element createTable()
- {
- return document.createElement( "table" );
- }
-
- public Element createTableBody()
- {
- return document.createElement( "tbody" );
- }
-
- public Element createTableCell()
- {
- return document.createElement( "td" );
- }
-
- public Element createTableColumn()
- {
- return document.createElement( "col" );
- }
-
- public Element createTableColumnGroup()
- {
- return document.createElement( "colgroup" );
- }
-
- public Element createTableHeader()
- {
- return document.createElement( "thead" );
- }
-
- public Element createTableHeaderCell()
- {
- return document.createElement( "th" );
- }
-
- public Element createTableRow()
- {
- return document.createElement( "tr" );
- }
-
- public Text createText( String data )
- {
- return document.createTextNode( data );
- }
-
- public Element createUnorderedList()
- {
- return document.createElement( "ul" );
- }
-
- public Element getBody()
- {
- return body;
- }
-
- public Document getDocument()
- {
- return document;
- }
-
- public Element getHead()
- {
- return head;
- }
-
- public String getOrCreateCssClass( String classNamePrefix, String style )
- {
- if ( !stylesheet.containsKey( classNamePrefix ) )
- stylesheet.put( classNamePrefix, new LinkedHashMap<>(
- 1) );
-
- Map<String, String> styleToClassName = stylesheet.get( classNamePrefix );
- String knownClass = styleToClassName.get( style );
- if ( knownClass != null )
- return knownClass;
-
- String newClassName = classNamePrefix + ( styleToClassName.size() + 1 );
- styleToClassName.put( style, newClassName );
- return newClassName;
- }
-
- public String getTitle()
- {
- if ( title == null )
- return null;
-
- return titleText.getTextContent();
- }
-
- public void setTitle( String titleText )
- {
- if ( AbstractWordUtils.isEmpty( titleText ) && this.title != null )
- {
- this.head.removeChild( this.title );
- this.title = null;
- this.titleText = null;
- }
-
- if ( this.title == null )
- {
- this.title = document.createElement( "title" );
- this.titleText = document.createTextNode( titleText );
- this.title.appendChild( this.titleText );
- this.head.appendChild( title );
- }
-
- this.titleText.setData( titleText );
- }
-
- public void updateStylesheet()
- {
- stylesheetElement.setTextContent( buildStylesheet( stylesheet ) );
- }
- }
|