1 /*
   2  * Copyright (c) 2003, 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 
  26 package jdk.javadoc.internal.doclets.formats.html;
  27 
  28 import java.io.IOException;
  29 
  30 import javax.lang.model.element.PackageElement;
  31 import javax.lang.model.element.TypeElement;
  32 import javax.lang.model.type.TypeMirror;
  33 
  34 import jdk.javadoc.internal.doclets.toolkit.AnnotationTypeFieldWriter;
  35 import jdk.javadoc.internal.doclets.toolkit.AnnotationTypeOptionalMemberWriter;
  36 import jdk.javadoc.internal.doclets.toolkit.AnnotationTypeRequiredMemberWriter;
  37 import jdk.javadoc.internal.doclets.toolkit.AnnotationTypeWriter;
  38 import jdk.javadoc.internal.doclets.toolkit.ClassWriter;
  39 import jdk.javadoc.internal.doclets.toolkit.ConstantsSummaryWriter;
  40 import jdk.javadoc.internal.doclets.toolkit.MemberSummaryWriter;
  41 import jdk.javadoc.internal.doclets.toolkit.PackageSummaryWriter;
  42 import jdk.javadoc.internal.doclets.toolkit.SerializedFormWriter;
  43 import jdk.javadoc.internal.doclets.toolkit.WriterFactory;
  44 import jdk.javadoc.internal.doclets.toolkit.util.ClassTree;
  45 import jdk.javadoc.internal.doclets.toolkit.util.VisibleMemberMap;
  46 
  47 import static jdk.javadoc.internal.doclets.toolkit.util.VisibleMemberMap.Kind.*;
  48 
  49 /**
  50  * The factory that returns HTML writers.
  51  *
  52  *  <p><b>This is NOT part of any supported API.
  53  *  If you write code that depends on this, you do so at your own risk.
  54  *  This code and its internal interfaces are subject to change or
  55  *  deletion without notice.</b>
  56  *
  57  * @author Jamie Ho
  58  * @since 1.5
  59  */
  60 public class WriterFactoryImpl implements WriterFactory {
  61 
  62     private final ConfigurationImpl configuration;
  63     public WriterFactoryImpl(ConfigurationImpl configuration) {
  64         this.configuration = configuration;
  65     }
  66 
  67     /**
  68      * {@inheritDoc}
  69      */
  70     @Override
  71     public ConstantsSummaryWriter getConstantsSummaryWriter() throws Exception {
  72         return new ConstantsSummaryWriterImpl(configuration);
  73     }
  74 
  75     /**
  76      * {@inheritDoc}
  77      */
  78     @Override
  79     public PackageSummaryWriter getPackageSummaryWriter(PackageElement packageElement,
  80             PackageElement prevPkg, PackageElement nextPkg) throws Exception {
  81         return new PackageWriterImpl(configuration, packageElement, prevPkg, nextPkg);
  82     }
  83 
  84     /**
  85      * {@inheritDoc}
  86      */
  87     @Override
  88     public ClassWriter getClassWriter(TypeElement typeElement, TypeElement prevClass,
  89             TypeElement nextClass, ClassTree classTree) throws IOException {
  90         return new ClassWriterImpl(configuration, typeElement, prevClass, nextClass, classTree);
  91     }
  92 
  93     /**
  94      * {@inheritDoc}
  95      */
  96     @Override
  97     public AnnotationTypeWriter getAnnotationTypeWriter(TypeElement annotationType,
  98             TypeMirror prevType, TypeMirror nextType) throws Exception {
  99         return new AnnotationTypeWriterImpl(configuration, annotationType, prevType, nextType);
 100     }
 101 
 102     /**
 103      * {@inheritDoc}
 104      */
 105     @Override
 106     public AnnotationTypeFieldWriter
 107             getAnnotationTypeFieldWriter(AnnotationTypeWriter annotationTypeWriter) throws Exception {
 108         TypeElement te = annotationTypeWriter.getAnnotationTypeElement();
 109         return new AnnotationTypeFieldWriterImpl(
 110             (SubWriterHolderWriter) annotationTypeWriter, te);
 111     }
 112 
 113     /**
 114      * {@inheritDoc}
 115      */
 116     @Override
 117     public AnnotationTypeOptionalMemberWriter
 118             getAnnotationTypeOptionalMemberWriter(
 119         AnnotationTypeWriter annotationTypeWriter) throws Exception {
 120         TypeElement te = annotationTypeWriter.getAnnotationTypeElement();
 121         return new AnnotationTypeOptionalMemberWriterImpl(
 122             (SubWriterHolderWriter) annotationTypeWriter, te);
 123     }
 124 
 125     /**
 126      * {@inheritDoc}
 127      */
 128     @Override
 129     public AnnotationTypeRequiredMemberWriter
 130             getAnnotationTypeRequiredMemberWriter(AnnotationTypeWriter annotationTypeWriter) throws Exception {
 131         TypeElement te = annotationTypeWriter.getAnnotationTypeElement();
 132         return new AnnotationTypeRequiredMemberWriterImpl(
 133             (SubWriterHolderWriter) annotationTypeWriter, te);
 134     }
 135 
 136     /**
 137      * {@inheritDoc}
 138      */
 139     @Override
 140     public EnumConstantWriterImpl getEnumConstantWriter(ClassWriter classWriter)
 141             throws Exception {
 142         return new EnumConstantWriterImpl((SubWriterHolderWriter) classWriter,
 143                 classWriter.getTypeElement());
 144     }
 145 
 146     /**
 147      * {@inheritDoc}
 148      */
 149     @Override
 150     public FieldWriterImpl getFieldWriter(ClassWriter classWriter)
 151             throws Exception {
 152         return new FieldWriterImpl((SubWriterHolderWriter) classWriter, classWriter.getTypeElement());
 153     }
 154 
 155     /**
 156      * {@inheritDoc}
 157      */
 158     @Override
 159     public PropertyWriterImpl getPropertyWriter(ClassWriter classWriter)
 160             throws Exception {
 161         return new PropertyWriterImpl((SubWriterHolderWriter) classWriter,
 162                 classWriter.getTypeElement());
 163     }
 164 
 165     /**
 166      * {@inheritDoc}
 167      */
 168     @Override
 169     public MethodWriterImpl getMethodWriter(ClassWriter classWriter)
 170             throws Exception {
 171         return new MethodWriterImpl((SubWriterHolderWriter) classWriter, classWriter.getTypeElement());
 172     }
 173 
 174     /**
 175      * {@inheritDoc}
 176      */
 177     @Override
 178     public ConstructorWriterImpl getConstructorWriter(ClassWriter classWriter)
 179             throws Exception {
 180         return new ConstructorWriterImpl((SubWriterHolderWriter) classWriter,
 181                 classWriter.getTypeElement());
 182     }
 183 
 184     /**
 185      * {@inheritDoc}
 186      */
 187     @Override
 188     public MemberSummaryWriter getMemberSummaryWriter(
 189             ClassWriter classWriter, VisibleMemberMap.Kind memberType)
 190             throws Exception {
 191         switch (memberType) {
 192             case CONSTRUCTORS:
 193                 return getConstructorWriter(classWriter);
 194             case ENUM_CONSTANTS:
 195                 return getEnumConstantWriter(classWriter);
 196             case FIELDS:
 197                 return getFieldWriter(classWriter);
 198             case PROPERTIES:
 199                 return getPropertyWriter(classWriter);
 200             case INNER_CLASSES:
 201                 return new NestedClassWriterImpl((SubWriterHolderWriter)
 202                     classWriter, classWriter.getTypeElement());
 203             case METHODS:
 204                 return getMethodWriter(classWriter);
 205             default:
 206                 return null;
 207         }
 208     }
 209 
 210     /**
 211      * {@inheritDoc}
 212      */
 213     @Override
 214     public MemberSummaryWriter getMemberSummaryWriter(
 215         AnnotationTypeWriter annotationTypeWriter, VisibleMemberMap.Kind memberType)
 216     throws Exception {
 217         switch (memberType) {
 218             case ANNOTATION_TYPE_FIELDS:
 219                 return (AnnotationTypeFieldWriterImpl)
 220                     getAnnotationTypeFieldWriter(annotationTypeWriter);
 221             case ANNOTATION_TYPE_MEMBER_OPTIONAL:
 222                 return (AnnotationTypeOptionalMemberWriterImpl)
 223                     getAnnotationTypeOptionalMemberWriter(annotationTypeWriter);
 224             case ANNOTATION_TYPE_MEMBER_REQUIRED:
 225                 return (AnnotationTypeRequiredMemberWriterImpl)
 226                     getAnnotationTypeRequiredMemberWriter(annotationTypeWriter);
 227             default:
 228                 return null;
 229         }
 230     }
 231 
 232     /**
 233      * {@inheritDoc}
 234      */
 235     @Override
 236     public SerializedFormWriter getSerializedFormWriter() throws Exception {
 237         return new SerializedFormWriterImpl(configuration);
 238     }
 239 }