Tue, 14 May 2013 10:14:53 -0700
8011668: Allow HTMLWriter.getResource to take Content args
Reviewed-by: darcy
1 /*
2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
26 package com.sun.tools.doclets.formats.html.markup;
28 import java.io.IOException;
29 import java.io.Writer;
31 import com.sun.tools.doclets.internal.toolkit.Content;
32 import com.sun.tools.doclets.internal.toolkit.util.*;
34 /**
35 * Class for generating raw HTML content to be added to HTML pages of javadoc output.
36 *
37 * <p><b>This is NOT part of any supported API.
38 * If you write code that depends on this, you do so at your own risk.
39 * This code and its internal interfaces are subject to change or
40 * deletion without notice.</b>
41 *
42 * @author Bhavesh Patel
43 */
44 public class RawHtml extends Content {
46 private String rawHtmlContent;
48 public static final Content nbsp = new RawHtml(" ");
50 /**
51 * Constructor to construct a RawHtml object.
52 *
53 * @param rawHtml raw HTML text to be added
54 */
55 public RawHtml(String rawHtml) {
56 rawHtmlContent = nullCheck(rawHtml);
57 }
59 /**
60 * This method is not supported by the class.
61 *
62 * @param content content that needs to be added
63 * @throws DocletAbortException this method will always throw a
64 * DocletAbortException because it
65 * is not supported.
66 */
67 public void addContent(Content content) {
68 throw new DocletAbortException();
69 }
71 /**
72 * This method is not supported by the class.
73 *
74 * @param stringContent string content that needs to be added
75 * @throws DocletAbortException this method will always throw a
76 * DocletAbortException because it
77 * is not supported.
78 */
79 public void addContent(String stringContent) {
80 throw new DocletAbortException();
81 }
83 /**
84 * {@inheritDoc}
85 */
86 public boolean isEmpty() {
87 return rawHtmlContent.isEmpty();
88 }
90 /**
91 * {@inheritDoc}
92 */
93 @Override
94 public String toString() {
95 return rawHtmlContent;
96 }
98 private enum State { TEXT, ENTITY, TAG, STRING };
100 @Override
101 public int charCount() {
102 State state = State.TEXT;
103 int count = 0;
104 for (int i = 0; i < rawHtmlContent.length(); i++) {
105 char c = rawHtmlContent.charAt(i);
106 switch (state) {
107 case TEXT:
108 switch (c) {
109 case '<':
110 state = State.TAG;
111 break;
112 case '&':
113 state = State.ENTITY;
114 count++;
115 break;
116 default:
117 count++;
118 }
119 break;
121 case ENTITY:
122 if (!Character.isLetterOrDigit(c))
123 state = State.TEXT;
124 break;
126 case TAG:
127 switch (c) {
128 case '"':
129 state = State.STRING;
130 break;
131 case '>':
132 state = State.TEXT;
133 break;
134 }
135 break;
137 case STRING:
138 switch (c) {
139 case '"':
140 state = State.TAG;
141 break;
142 }
143 }
144 }
145 return count;
146 }
148 /**
149 * {@inheritDoc}
150 */
151 @Override
152 public boolean write(Writer out, boolean atNewline) throws IOException {
153 out.write(rawHtmlContent);
154 return rawHtmlContent.endsWith(DocletConstants.NL);
155 }
156 }