All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sun.enterprise.admin.cli.schemadoc.HtmlFormat Maven / Gradle / Ivy

There is a newer version: 8.0.0-JDK17-M9
Show newest version
/*
 * Copyright (c) 2010, 2018 Oracle and/or its affiliates. All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v. 2.0, which is available at
 * http://www.eclipse.org/legal/epl-2.0.
 *
 * This Source Code may also be made available under the following Secondary
 * Licenses when the conditions for such availability set forth in the
 * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
 * version 2 with the GNU Classpath Exception, which is available at
 * https://www.gnu.org/software/classpath/license.html.
 *
 * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
 */

package com.sun.enterprise.admin.cli.schemadoc;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.glassfish.api.admin.config.PropertyDesc;
import org.jvnet.hk2.annotations.Service;
import org.jvnet.hk2.config.Attribute;
import org.jvnet.hk2.config.types.Property;

@Service(name = "html")
public class HtmlFormat implements SchemaOutputFormat {
    private PrintWriter tocWriter;
    private PrintWriter detail;
    private Set toc = new HashSet();
    private File dir;
    private Map defs;

    @SuppressWarnings({ "IOResourceOpenedButNotSafelyClosed" })
    @Override
    public void output(Context context) {
        dir = context.getDocDir();
        defs = context.getClassDefs();
        try {
            try {
                tocWriter = new PrintWriter(new FileWriter(new File(dir, "toc.HTML")));
                detail = new PrintWriter(new FileWriter(new File(dir, "detail.HTML")));
            } catch (IOException e) {
                e.printStackTrace();
                throw new RuntimeException(e.getMessage());
            }
            println(tocWriter,
                    "");
            println(detail, "");
            copyResources();
            buildToc(defs.get(context.getRootClassName()));
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            println(tocWriter, "");
            footer(tocWriter);
            footer(detail);
            if (tocWriter != null) {
                tocWriter.close();
            }
            if (detail != null) {
                detail.close();
            }
        }
    }

    private void buildDetail(ClassDef def) {
        println(detail, "

"); println(detail, ""); println(detail, String.format(""); println(detail, ""); printHeaderRow(detail, "attribute", "type", "default", "required"); final Map map = def.getAttributes(); if (map != null) { for (Entry entry : map.entrySet()) { println(detail, String.format("", entry.getKey())); printAttributeData(entry.getValue()); } } println(detail, "
%s%s", def.isDeprecated() ? "deprecated" : "", def.getXmlName(), def.isDeprecated() ? " - DEPRECATED" : "")); println(detail, "
%s
"); printPropertyData(def); } private void println(final PrintWriter writer, final String text) { writer.println(text); writer.flush(); } private void printAttributeData(final Attribute annotation) { printKeyValue(detail, annotation != null ? annotation.dataType().getName() : null); printKeyValue(detail, annotation != null ? annotation.defaultValue() : null); printKeyValue(detail, annotation != null && annotation.required()); } private void printPropertyData(final ClassDef def) { final Set properties = def.getProperties(); if (properties != null && !properties.isEmpty()) { println(detail, ""); println(detail, ""); println(detail, ""); println(detail, ""); println(detail, ""); println(detail, ""); println(detail, ""); println(detail, ""); println(detail, ""); println(detail, ""); println(detail, ""); for (PropertyDesc property : properties) { println(detail, ""); println(detail, String.format("", property.name())); println(detail, String.format("", property.defaultValue())); println(detail, String.format("", property.values().length == 0 ? "" : Arrays.toString(property.values()))); println(detail, String.format("", property.description())); println(detail, ""); } println(detail, "
Properties
namedefaultvaluesdescription
%s%s%s%s
"); println(detail, ""); } } private void buildToc(final ClassDef def) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { if (def != null/* && !"Named".equals(def.getSimpleName())*/) { if (!toc.contains(def)) { buildDetail(def); } toc.add(def); println(tocWriter, "
    "); println(tocWriter, "
  • " + link(def)); for (Entry aggType : def.getAggregatedTypes().entrySet()) { if (!Property.class.getName().equals(aggType.getValue()) && defs != null) { buildToc(defs.get(aggType.getValue())); } } for (ClassDef subclass : def.getSubclasses()) { buildToc(subclass); } println(tocWriter, "
"); } } private String link(final ClassDef def) { return String.format("
%s", def.isDeprecated() ? "class=\"deprecated\"" : "", def.getXmlName(), def.getXmlName()); } private void footer(final PrintWriter writer) { writer.println(""); writer.flush(); writer.close(); } private void copyResources() throws IOException { copy("/schemadoc.css"); copy("/index.html"); } private void copy(final String resource) throws IOException { InputStreamReader reader = null; PrintWriter writer = null; try { try { InputStream stream = getClass().getClassLoader().getResourceAsStream(resource); reader = new InputStreamReader(stream); writer = new PrintWriter(new File(dir, resource)); char[] bytes = new char[8192]; int read; while ((read = reader.read(bytes)) != -1) { writer.write(bytes, 0, read); } } finally { if (reader != null) { reader.close(); } } } finally { if (writer != null) { writer.close(); } } } private void printKeyValue(final PrintWriter writer, Object value) { println(writer, ""); if (value != null) { if (value instanceof Class) { println(writer, ((Class) value).getSimpleName()); } else { println(writer, value.toString().trim()); } } println(writer, ""); } private void printHeaderRow(final PrintWriter writer, final String... values) { writer.println(""); for (String value : values) { writer.println(String.format("%s", value != null ? value.trim() : " ")); } writer.println(""); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy