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 */
25
26package com.sun.tools.doclets.formats.html.markup;
27
28import java.io.IOException;
29import java.io.Writer;
30
31import com.sun.tools.doclets.internal.toolkit.Content;
32import com.sun.tools.doclets.internal.toolkit.util.*;
33
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@Deprecated
45public class RawHtml extends Content {
46
47    private String rawHtmlContent;
48
49    public static final Content nbsp = new RawHtml("&nbsp;");
50
51    /**
52     * Constructor to construct a RawHtml object.
53     *
54     * @param rawHtml raw HTML text to be added
55     */
56    public RawHtml(String rawHtml) {
57        rawHtmlContent = nullCheck(rawHtml);
58    }
59
60    /**
61     * This method is not supported by the class.
62     *
63     * @param content content that needs to be added
64     * @throws DocletAbortException this method will always throw a
65     *                              DocletAbortException because it
66     *                              is not supported.
67     */
68    public void addContent(Content content) {
69        throw new DocletAbortException("not supported");
70    }
71
72    /**
73     * This method is not supported by the class.
74     *
75     * @param stringContent string content that needs to be added
76     * @throws DocletAbortException this method will always throw a
77     *                              DocletAbortException because it
78     *                              is not supported.
79     */
80    public void addContent(String stringContent) {
81        throw new DocletAbortException("not supported");
82    }
83
84    /**
85     * {@inheritDoc}
86     */
87    public boolean isEmpty() {
88        return rawHtmlContent.isEmpty();
89    }
90
91    /**
92     * {@inheritDoc}
93     */
94    @Override
95    public String toString() {
96        return rawHtmlContent;
97    }
98
99    private enum State { TEXT, ENTITY, TAG, STRING }
100
101    @Override
102    public int charCount() {
103        return charCount(rawHtmlContent);
104    }
105
106    static int charCount(String htmlText) {
107        State state = State.TEXT;
108        int count = 0;
109        for (int i = 0; i < htmlText.length(); i++) {
110            char c = htmlText.charAt(i);
111            switch (state) {
112                case TEXT:
113                    switch (c) {
114                        case '<':
115                            state = State.TAG;
116                            break;
117                        case '&':
118                            state = State.ENTITY;
119                            count++;
120                            break;
121                        default:
122                            count++;
123                    }
124                    break;
125
126                case ENTITY:
127                    if (!Character.isLetterOrDigit(c))
128                        state = State.TEXT;
129                    break;
130
131                case TAG:
132                    switch (c) {
133                        case '"':
134                            state = State.STRING;
135                            break;
136                        case '>':
137                            state = State.TEXT;
138                            break;
139                    }
140                    break;
141
142                case STRING:
143                    switch (c) {
144                        case '"':
145                            state = State.TAG;
146                            break;
147                    }
148            }
149        }
150        return count;
151    }
152
153    /**
154     * {@inheritDoc}
155     */
156    @Override
157    public boolean write(Writer out, boolean atNewline) throws IOException {
158        out.write(rawHtmlContent);
159        return rawHtmlContent.endsWith(DocletConstants.NL);
160    }
161}
162