1 /*
2  * Copyright (c) 2010, 2016, 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 package jdk.javadoc.internal.doclets.formats.html.markup;
26 
27 import java.io.IOException;
28 import java.io.Writer;
29 
30 import jdk.javadoc.internal.doclets.toolkit.Content;
31 import jdk.javadoc.internal.doclets.toolkit.util.DocletConstants;
32 
33 /**
34  * Class for containing fixed string content for HTML tags of javadoc output.
35  *
36  *  <p><b>This is NOT part of any supported API.
37  *  If you write code that depends on this, you do so at your own risk.
38  *  This code and its internal interfaces are subject to change or
39  *  deletion without notice.</b>
40  */
41 public class FixedStringContent extends Content {
42     private final String string;
43 
44     /**
45      * Constructor to construct FixedStringContent object.
46      *
47      * @param content content for the object
48      */
FixedStringContent(CharSequence content)49     public FixedStringContent(CharSequence content) {
50         string = needEscape(content)
51                 ? escape(content)
52                 : content.toString();
53     }
54 
55     /**
56      * This method is not supported by the class.
57      *
58      * @param content content that needs to be added
59      * @throws UnsupportedOperationException always
60      */
61     @Override
addContent(Content content)62     public void addContent(Content content) {
63         throw new UnsupportedOperationException();
64     }
65 
66     /**
67      * Adds content for the StringContent object.  The method escapes
68      * HTML characters for the string content that is added.
69      *
70      * @param strContent string content to be added
71      * @throws UnsupportedOperationException always
72      */
73     @Override
addContent(CharSequence strContent)74     public void addContent(CharSequence strContent) {
75         throw new UnsupportedOperationException();
76     }
77 
78     /**
79      * {@inheritDoc}
80      */
81     @Override
isEmpty()82     public boolean isEmpty() {
83         return string.isEmpty();
84     }
85 
86     @Override
charCount()87     public int charCount() {
88         return RawHtml.charCount(string);
89     }
90 
91     /**
92      * {@inheritDoc}
93      */
94     @Override
toString()95     public String toString() {
96         return string;
97     }
98 
99     /**
100      * {@inheritDoc}
101      */
102     @Override
write(Writer out, boolean atNewline)103     public boolean write(Writer out, boolean atNewline) throws IOException {
104         out.write(string);
105         return string.endsWith(DocletConstants.NL);
106     }
107 
needEscape(CharSequence cs)108     private boolean needEscape(CharSequence cs) {
109         for (int i = 0; i < cs.length(); i++) {
110             switch (cs.charAt(i)) {
111                 case '<':
112                 case '>':
113                 case '&':
114                     return true;
115             }
116         }
117         return false;
118     }
escape(CharSequence s)119     private String escape(CharSequence s) {
120         StringBuilder sb = new StringBuilder();
121         for (int i = 0; i < s.length(); i++) {
122             char ch = s.charAt(i);
123             switch (ch) {
124                 case '<': sb.append("&lt;");  break;
125                 case '>': sb.append("&gt;");  break;
126                 case '&': sb.append("&amp;"); break;
127                 default:  sb.append(ch);      break;
128             }
129         }
130         return sb.toString();
131     }
132 
133 }
134