📄 htmlwriter.java
字号:
/**
* Adds the header to the top of the </CODE>Document</CODE>
*/
protected void initHeader() {
if (header != null) {
try {
add(header.paragraph());
}
catch(Exception e) {
throw new ExceptionConverter(e);
}
}
}
/**
* Adds the header to the top of the </CODE>Document</CODE>
*/
protected void initFooter() {
if (footer != null) {
try {
// Set the page number. HTML has no notion of a page, so it should always
// add up to 1
footer.setPageNumber(pageN + 1);
add(footer.paragraph());
}
catch(Exception e) {
throw new ExceptionConverter(e);
}
}
}
/**
* Writes a Metatag in the header.
*
* @param meta the element that has to be written
* @throws IOException
*/
protected void writeHeader(Meta meta) throws IOException {
addTabs(2);
writeStart(HtmlTags.META);
switch(meta.type()) {
case Element.HEADER:
write(HtmlTags.NAME, ((Header) meta).getName());
break;
case Element.SUBJECT:
write(HtmlTags.NAME, HtmlTags.SUBJECT);
break;
case Element.KEYWORDS:
write(HtmlTags.NAME, HtmlTags.KEYWORDS);
break;
case Element.AUTHOR:
write(HtmlTags.NAME, HtmlTags.AUTHOR);
break;
}
write(HtmlTags.CONTENT, HtmlEncoder.encode(meta.getContent()));
writeEnd();
}
/**
* Writes a link in the header.
*
* @param header the element that has to be written
* @throws IOException
*/
protected void writeLink(Header header) throws IOException {
addTabs(2);
writeStart(HtmlTags.LINK);
write(HtmlTags.REL, header.getName());
write(HtmlTags.TYPE, HtmlTags.TEXT_CSS);
write(HtmlTags.REFERENCE, header.getContent());
writeEnd();
}
/**
* Writes a JavaScript section or, if the markup attribute HtmlTags.URL is set, a JavaScript reference in the header.
*
* @param header the element that has to be written
* @throws IOException
*/
protected void writeJavaScript(Header header) throws IOException {
addTabs(2);
writeStart(HtmlTags.SCRIPT);
write(HtmlTags.LANGUAGE, HtmlTags.JAVASCRIPT);
if (markup.size() > 0) {
/* JavaScript reference example:
*
* <script language="JavaScript" src="/myPath/MyFunctions.js"/>
*/
writeMarkupAttributes(markup);
os.write(GT);
writeEnd(HtmlTags.SCRIPT);
}
else {
/* JavaScript coding convention:
*
* <script language="JavaScript" type="text/javascript">
* <!--
* // ... JavaScript methods ...
* //-->
* </script>
*/
write(HtmlTags.TYPE, Markup.HTML_VALUE_JAVASCRIPT);
os.write(GT);
addTabs(2);
write(new String(BEGINCOMMENT) + "\n");
write(header.getContent());
addTabs(2);
write("//" + new String(ENDCOMMENT));
addTabs(2);
writeEnd(HtmlTags.SCRIPT);
}
}
/**
* Writes some comment.
* <P>
* This method writes some comment.
*
* @param comment the comment that has to be written
* @throws IOException
*/
protected void writeComment(String comment) throws IOException {
addTabs(2);
os.write(BEGINCOMMENT);
write(comment);
os.write(ENDCOMMENT);
}
// public methods
/**
* Changes the standardfont.
*
* @param standardfont The font
*/
public void setStandardFont(Font standardfont) {
this.standardfont = standardfont;
}
/**
* Checks if a given font is the same as the font that was last used.
*
* @param font the font of an object
* @return true if the font differs
*/
public boolean isOtherFont(Font font) {
try {
Font cFont = (Font) currentfont.peek();
if (cFont.compareTo(font) == 0) return false;
return true;
}
catch(EmptyStackException ese) {
if (standardfont.compareTo(font) == 0) return false;
return true;
}
}
/**
* Sets the basepath for images.
* <P>
* This is especially useful if you add images using a file,
* rather than an URL. In PDF there is no problem, since
* the images are added inline, but in HTML it is sometimes
* necessary to use a relative path or a special path to some
* images directory.
*
* @param imagepath the new imagepath
*/
public void setImagepath(String imagepath) {
this.imagepath = imagepath;
}
/**
* Resets the imagepath.
*/
public void resetImagepath() {
imagepath = null;
}
/**
* Changes the header of this document.
*
* @param header the new header
*/
public void setHeader(HeaderFooter header) {
this.header = header;
}
/**
* Changes the footer of this document.
*
* @param footer the new footer
*/
public void setFooter(HeaderFooter footer) {
this.footer = footer;
}
/**
* Signals that a <CODE>String</CODE> was added to the <CODE>Document</CODE>.
*
* @param string a String to add to the HTML
* @return <CODE>true</CODE> if the string was added, <CODE>false</CODE> if not.
* @throws DocumentException when a document isn't open yet, or has been closed
*/
public boolean add(String string) {
if (pause) {
return false;
}
try
{
write(string);
return true;
}
catch(IOException ioe) {
throw new ExceptionConverter(ioe);
}
}
/**
* Writes the HTML representation of an element.
*
* @param element the element
* @param indent the indentation
* @throws IOException
*/
protected void write(Element element, int indent) throws IOException {
Properties styleAttributes = null;
switch(element.type()) {
case Element.MARKED: {
try {
add(element);
} catch (DocumentException e) {
e.printStackTrace();
}
return;
}
case Element.CHUNK:
{
Chunk chunk = (Chunk) element;
// if the chunk contains an image, return the image representation
Image image = chunk.getImage();
if (image != null) {
write(image, indent);
return;
}
if (chunk.isEmpty()) return;
HashMap attributes = chunk.getAttributes();
if (attributes != null && attributes.get(Chunk.NEWPAGE) != null) {
return;
}
boolean tag = isOtherFont(chunk.getFont()) || markup.size() > 0;
if (tag) {
// start span tag
addTabs(indent);
writeStart(HtmlTags.SPAN);
if (isOtherFont(chunk.getFont())) {
write(chunk.getFont(), null);
}
writeMarkupAttributes(markup);
os.write(GT);
}
if (attributes != null && attributes.get(Chunk.SUBSUPSCRIPT) != null) {
// start sup or sub tag
if (((Float)attributes.get(Chunk.SUBSUPSCRIPT)).floatValue() > 0) {
writeStart(HtmlTags.SUP);
}
else {
writeStart(HtmlTags.SUB);
}
os.write(GT);
}
// contents
write(HtmlEncoder.encode(chunk.getContent()));
if (attributes != null && attributes.get(Chunk.SUBSUPSCRIPT) != null) {
// end sup or sub tag
os.write(LT);
os.write(FORWARD);
if (((Float)attributes.get(Chunk.SUBSUPSCRIPT)).floatValue() > 0) {
write(HtmlTags.SUP);
}
else {
write(HtmlTags.SUB);
}
os.write(GT);
}
if (tag) {
// end tag
writeEnd(Markup.HTML_TAG_SPAN);
}
return;
}
case Element.PHRASE:
{
Phrase phrase = (Phrase) element;
styleAttributes = new Properties();
if (phrase.hasLeading()) styleAttributes.setProperty(Markup.CSS_KEY_LINEHEIGHT, phrase.getLeading() + "pt");
// start tag
addTabs(indent);
writeStart(Markup.HTML_TAG_SPAN);
writeMarkupAttributes(markup);
write(phrase.getFont(), styleAttributes);
os.write(GT);
currentfont.push(phrase.getFont());
// contents
for (Iterator i = phrase.iterator(); i.hasNext(); ) {
write((Element) i.next(), indent + 1);
}
// end tag
addTabs(indent);
writeEnd(Markup.HTML_TAG_SPAN);
currentfont.pop();
return;
}
case Element.ANCHOR:
{
Anchor anchor = (Anchor) element;
styleAttributes = new Properties();
if (anchor.hasLeading()) styleAttributes.setProperty(Markup.CSS_KEY_LINEHEIGHT, anchor.getLeading() + "pt");
// start tag
addTabs(indent);
writeStart(HtmlTags.ANCHOR);
if (anchor.getName() != null) {
write(HtmlTags.NAME, anchor.getName());
}
if (anchor.getReference() != null) {
write(HtmlTags.REFERENCE, anchor.getReference());
}
writeMarkupAttributes(markup);
write(anchor.getFont(), styleAttributes);
os.write(GT);
currentfont.push(anchor.getFont());
// contents
for (Iterator i = anchor.iterator(); i.hasNext(); ) {
write((Element) i.next(), indent + 1);
}
// end tag
addTabs(indent);
writeEnd(HtmlTags.ANCHOR);
currentfont.pop();
return;
}
case Element.PARAGRAPH:
{
Paragraph paragraph = (Paragraph) element;
styleAttributes = new Properties();
if (paragraph.hasLeading()) styleAttributes.setProperty(Markup.CSS_KEY_LINEHEIGHT, paragraph.getTotalLeading() + "pt");
// start tag
addTabs(indent);
writeStart(HtmlTags.DIV);
writeMarkupAttributes(markup);
String alignment = HtmlEncoder.getAlignment(paragraph.getAlignment());
if (!"".equals(alignment)) {
write(HtmlTags.ALIGN, alignment);
}
write(paragraph.getFont(), styleAttributes);
os.write(GT);
currentfont.push(paragraph.getFont());
// contents
for (Iterator i = paragraph.iterator(); i.hasNext(); ) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -