Content.java revision 3233:b5d08bc0d224
1/* 2 * Copyright (c) 2010, 2015, 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 jdk.javadoc.internal.doclets.toolkit; 27 28import java.io.IOException; 29import java.io.StringWriter; 30import java.io.Writer; 31import java.util.Objects; 32 33import jdk.javadoc.internal.doclets.toolkit.util.DocletAbortException; 34 35 36/** 37 * A class to create content for javadoc output pages. 38 * 39 * <p><b>This is NOT part of any supported API. 40 * If you write code that depends on this, you do so at your own risk. 41 * This code and its internal interfaces are subject to change or 42 * deletion without notice.</b> 43 * 44 * @author Bhavesh Patel 45 */ 46public abstract class Content { 47 48 /** 49 * Returns a string representation of the content. 50 * 51 * @return string representation of the content 52 */ 53 @Override 54 public String toString() { 55 StringWriter out = new StringWriter(); 56 try { 57 write(out, true); 58 } catch (IOException e) { 59 // cannot happen from StringWriter 60 throw new DocletAbortException(e); 61 } 62 return out.toString(); 63 } 64 65 /** 66 * Adds content to the existing content. 67 * 68 * @param content content that needs to be added 69 */ 70 public abstract void addContent(Content content); 71 72 /** 73 * Adds a string content to the existing content. 74 * 75 * @param stringContent the string content to be added 76 */ 77 public abstract void addContent(String stringContent); 78 79 /** 80 * Writes content to a writer. 81 * 82 */ 83 public abstract boolean write(Writer writer, boolean atNewline) throws IOException ; 84 85 /** 86 * Returns true if the content is empty. 87 * 88 * @return true if no content to be displayed else return false 89 */ 90 public abstract boolean isEmpty(); 91 92 /** 93 * Returns true if the content is valid. 94 * 95 * @return true if the content is valid else return false 96 */ 97 public boolean isValid() { 98 return !isEmpty(); 99 } 100 101 /** 102 * Return the number of characters of plain text content in this object 103 * (optional operation.) 104 * @return the number of characters of plain text content in this 105 */ 106 public int charCount() { 107 return 0; 108 } 109 110 /** 111 * Checks for null values. 112 * 113 * @param t reference type to check for null values 114 * @return the reference type if not null or else throws a null pointer exception 115 */ 116 protected static <T> T nullCheck(T t) { 117 return Objects.requireNonNull(t); 118 } 119} 120