1 /* 2 * Copyright (c) 2010, 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 26 package com.sun.tools.doclets.internal.toolkit; 27 28 import com.sun.tools.doclets.internal.toolkit.util.*; 29 30 /** 31 * A class to create content for javadoc output pages. 32 * 33 * @author Bhavesh Patel 34 */ 35 public abstract class Content { 36 37 /** 38 * Returns a string representation of the content. 39 * 40 * @return string representation of the content 41 */ 42 public String toString() { 43 StringBuilder contentBuilder = new StringBuilder(); 44 write(contentBuilder); 45 return contentBuilder.toString(); 46 } 47 48 /** 49 * Adds content to the existing content. 50 * 51 * @param content content that needs to be added 52 */ 53 public abstract void addContent(Content content); 54 55 /** 56 * Adds a string content to the existing content. 57 * 58 * @param stringContent the string content to be added 59 */ 60 public abstract void addContent(String stringContent); 61 62 /** 63 * Writes content to a StringBuilder. 64 * 65 */ 66 public abstract void write(StringBuilder contentBuilder); 67 68 /** 69 * Returns true if the content is empty. 70 * 71 * @return true if no content to be displayed else return false 72 */ 73 public abstract boolean isEmpty(); 74 75 /** 76 * Returns true if the content is valid. 77 * 78 * @return true if the content is valid else return false 79 */ 80 public boolean isValid() { 81 return !isEmpty(); 82 } 83 84 /** 85 * Checks for null values. 86 * 87 * @param t reference type to check for null values 88 * @return the reference type if not null or else throws a null pointer exception 89 */ 90 protected static <T> T nullCheck(T t) { 91 t.getClass(); 92 return t; 93 } 94 95 /** 96 * Returns true if the content ends with a newline character. Empty content 97 * is considered as ending with new line. 98 * 99 * @param contentBuilder content to test for newline character at the end 100 * @return true if the content ends with newline. 101 */ 102 protected boolean endsWithNewLine(StringBuilder contentBuilder) { 103 int contentLength = contentBuilder.length(); 104 if (contentLength == 0) { 105 return true; 106 } 107 int nlLength = DocletConstants.NL.length(); 108 if (contentLength < nlLength) { 109 return false; 110 } 111 int contentIndex = contentLength - 1; 112 int nlIndex = nlLength - 1; 113 while (nlIndex >= 0) { 114 if (contentBuilder.charAt(contentIndex) != DocletConstants.NL.charAt(nlIndex)) { 115 return false; 116 } 117 contentIndex--; 118 nlIndex--; 119 } 120 return true; 121 } 122 }