001/* 002 * Copyright (c) 2010, 2022, Oracle and/or its affiliates. All rights reserved. 003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 004 * 005 * This code is free software; you can redistribute it and/or modify it 006 * under the terms of the GNU General Public License version 2 only, as 007 * published by the Free Software Foundation. Oracle designates this 008 * particular file as subject to the "Classpath" exception as provided 009 * by Oracle in the LICENSE file that accompanied this code. 010 * 011 * This code is distributed in the hope that it will be useful, but WITHOUT 012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 014 * version 2 for more details (a copy is included in the LICENSE file that 015 * accompanied this code). 016 * 017 * You should have received a copy of the GNU General Public License version 018 * 2 along with this work; if not, write to the Free Software Foundation, 019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 020 * 021 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 022 * or visit www.oracle.com if you need additional information or have any 023 * questions. 024 */ 025 026package org.jdrupes.mdoclet.internal.doclets.formats.html.markup; 027 028import java.io.IOException; 029import java.io.Writer; 030 031import org.jdrupes.mdoclet.internal.doclets.toolkit.Content; 032 033/** 034 * Class for containing immutable string content for HTML tags of javadoc output. 035 * Newlines are always represented by {@code \n}. 036 * Any special HTML characters will be escaped if and when the content is written out. 037 */ 038public class Text extends Content { 039 040 private final String string; 041 042 public static final Text EMPTY = Text.of(""); 043 044 /** 045 * Creates a new object containing immutable text. 046 * 047 * @param content the text content 048 * @return the object 049 */ 050 public static Text of(CharSequence content) { 051 return new Text(content); 052 } 053 054 /** 055 * Constructs an immutable text object. 056 * 057 * @param content content for the object 058 */ 059 private Text(CharSequence content) { 060 assert checkNewlines(content); 061 string = content.toString(); 062 } 063 064 @Override 065 public boolean isEmpty() { 066 return string.isEmpty(); 067 } 068 069 @Override 070 public int charCount() { 071 return charCount(string); 072 } 073 074 static int charCount(CharSequence cs) { 075 return cs.length(); 076 } 077 078 @Override 079 public String toString() { 080 return string; 081 } 082 083 @Override 084 public boolean write(Writer out, String newline, boolean atNewline) 085 throws IOException { 086 out.write(Entity.escapeHtmlChars(string).replace("\n", newline)); 087 return string.endsWith("\n"); 088 } 089 090 /** 091 * The newline character, to be used when creating {@code Content} nodes. 092 */ 093 public static final String NL = "\n"; 094 095 /** 096 * Returns a given string with all newlines in the form {@code \n}. 097 * 098 * The sequences of interest are {@code \n}, {@code \r\n}, and {@code \r}. 099 * {@code \n} is already in the right form, so can be ignored, 100 * leaving code to handle {@code \r\n}, and {@code \r}. 101 * 102 * @param text the string 103 * @return the string with newlines in the form {@code \n} 104 */ 105 public static CharSequence normalizeNewlines(CharSequence text) { 106 // fast-track when the input is a string with no \r characters 107 if (text instanceof String s && s.indexOf('\r') != -1) { 108 return text; 109 } else { 110 var sb = new StringBuilder(); 111 var s = text.toString(); 112 int sLen = s.length(); 113 int start = 0; 114 int pos; 115 while ((pos = s.indexOf('\r', start)) != -1) { 116 sb.append(s, start, pos); 117 sb.append('\n'); 118 pos++; 119 if (pos < sLen && s.charAt(pos) == '\n') { 120 pos++; 121 } 122 start = pos; 123 } 124 sb.append(s.substring(start)); 125 return sb; 126 } 127 } 128 129 /** 130 * Check for the absence of {@code \r} characters. 131 * @param cs the characters to be checked 132 * @return {@code true} if there are no {@code \r} characters, and {@code false} otherwise 133 */ 134 static boolean checkNewlines(CharSequence cs) { 135 return !cs.toString().contains("\r"); 136 } 137 138}