<commit_before>/**
 * 
 * Copyright 2005 LogicBlaze, Inc. http://www.logicblaze.com
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 * 
 **/
package org.xbean.spring.task;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.codehaus.jam.JAnnotatedElement;
import org.codehaus.jam.JAnnotation;
import org.codehaus.jam.JAnnotationValue;
import org.codehaus.jam.JClass;
import org.codehaus.jam.JComment;
import org.codehaus.jam.JMethod;
import org.codehaus.jam.JProperty;
import org.xbean.spring.context.impl.NamespaceHelper;
import org.xbean.spring.context.impl.PropertyEditorHelper;

import java.beans.PropertyEditor;
import java.beans.PropertyEditorManager;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * 
 * @version $Revision: 1.1 $
 */
public class SchemaGenerator {
    public static final String XBEAN_ANNOTATION = "org.xbean.XBean";

    private static final Log log = LogFactory.getLog(SchemaGenerator.class);

    private final JClass[] classes;
    private final File destFile;
    private final String defaultNamespace;
    private String metaInfDir = "target/classes/";

    private Map namespaces = new HashMap();
    private List allElements = new ArrayList();
    private SchemaElement rootElement;
    private Map xsdTypeMap;

    public SchemaGenerator(JClass[] classes, File destFile, String defaultNamespace, String metaInfDir) {
        this.classes = classes;
        this.destFile = destFile;
        this.defaultNamespace = defaultNamespace;
        this.metaInfDir = metaInfDir;

        PropertyEditorHelper.registerCustomEditors();
    }

    public void generate() throws IOException {
        loadModel();

        for (Iterator iter = namespaces.entrySet().iterator(); iter.hasNext();) {
            Map.Entry entry = (Map.Entry) iter.next();
            String namespace = (String) entry.getKey();
            List elements = (List) entry.getValue();
            Collections.sort(elements);
            if (namespace != null) {
                generatePropertiesFile(namespace, elements);
            }
            generateDocumentation(namespace, elements);
            generateSchema(namespace, elements);
        }

        if (namespaces.isEmpty()) {
            System.out.println("Warning: no namespaces found!");
        }
    }

    // Documentation generation
    // -------------------------------------------------------------------------
    protected void generatePropertiesFile(String namespace, List elements) throws IOException {
        File file = new File(metaInfDir + NamespaceHelper.createDiscoveryPathName(namespace));
        file.getParentFile().mkdirs();
        System.out.println("Generating META-INF properties file: " + file + " for namespace: " + namespace);
        PrintWriter out = new PrintWriter(new FileWriter(file));
        try {
            generatePropertiesFile(out, namespace, elements);
        }
        finally {
            out.close();
        }
    }

    protected void generatePropertiesFile(PrintWriter out, String namespace, List elements) {
        out.println("# NOTE: this file is autogenerated by XBeans");
        out.println();
        out.println("# beans");

        for (Iterator iter = elements.iterator(); iter.hasNext();) {
            SchemaElement element = (SchemaElement) iter.next();
            out.println(element.getLocalName() + " = " + element.getType().getQualifiedName());
        }
    }

    // Documentation generation
    // -------------------------------------------------------------------------
    protected void generateDocumentation(String namespace, List elements) throws IOException {
        // TODO can only handle 1 schema document so far...
        File file = new File(destFile.getParentFile(), destFile.getName() + ".html");
        System.out.println("Generating HTML documentation file: " + file + " for namespace: " + namespace);
        PrintWriter out = new PrintWriter(new FileWriter(file));
        try {
            generateDocumentation(out, namespace, elements);
        }
        finally {
            out.close();
        }
    }

    private void generateDocumentation(PrintWriter out, String namespace, List elements) {
        out.println("<!-- NOTE: this file is autogenerated by XBeans -->");
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Schema for namespace: " + namespace + "</title>");
        out.println("<link rel='stylesheet' href='http://activemq.org/style.css' type='text/css'>");
        out.println("</head>");
        out.println();
        out.println("<body>");
        out.println();

        if (rootElement != null) {
            out.println("<h1>Root Element</h1>");
            out.println("<table>");
            out.println("  <tr><th>Element</th><th>Description</th><th>Class</th>");
            generateHtmlElementSummary(out, rootElement);
            out.println("</table>");
            out.println();
        }

        out.println("<h1>Element Summary</h1>");
        out.println("<table>");
        out.println("  <tr><th>Element</th><th>Description</th><th>Class</th>");
        for (Iterator iter = elements.iterator(); iter.hasNext();) {
            SchemaElement element = (SchemaElement) iter.next();
            generateHtmlElementSummary(out, element);
        }<commit_msg>updated the CSS some<commit_after>/**
 * 
 * Copyright 2005 LogicBlaze, Inc. http://www.logicblaze.com
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 * 
 **/
package org.xbean.spring.task;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.codehaus.jam.JAnnotatedElement;
import org.codehaus.jam.JAnnotation;
import org.codehaus.jam.JAnnotationValue;
import org.codehaus.jam.JClass;
import org.codehaus.jam.JComment;
import org.codehaus.jam.JMethod;
import org.codehaus.jam.JProperty;
import org.xbean.spring.context.impl.NamespaceHelper;
import org.xbean.spring.context.impl.PropertyEditorHelper;

import java.beans.PropertyEditor;
import java.beans.PropertyEditorManager;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * 
 * @version $Revision: 1.1 $
 */
public class SchemaGenerator {
    public static final String XBEAN_ANNOTATION = "org.xbean.XBean";

    private static final Log log = LogFactory.getLog(SchemaGenerator.class);

    private final JClass[] classes;
    private final File destFile;
    private final String defaultNamespace;
    private String metaInfDir = "target/classes/";

    private Map namespaces = new HashMap();
    private List allElements = new ArrayList();
    private SchemaElement rootElement;
    private Map xsdTypeMap;

    public SchemaGenerator(JClass[] classes, File destFile, String defaultNamespace, String metaInfDir) {
        this.classes = classes;
        this.destFile = destFile;
        this.defaultNamespace = defaultNamespace;
        this.metaInfDir = metaInfDir;

        PropertyEditorHelper.registerCustomEditors();
    }

    public void generate() throws IOException {
        loadModel();

        for (Iterator iter = namespaces.entrySet().iterator(); iter.hasNext();) {
            Map.Entry entry = (Map.Entry) iter.next();
            String namespace = (String) entry.getKey();
            List elements = (List) entry.getValue();
            Collections.sort(elements);
            if (namespace != null) {
                generatePropertiesFile(namespace, elements);
            }
            generateDocumentation(namespace, elements);
            generateSchema(namespace, elements);
        }

        if (namespaces.isEmpty()) {
            System.out.println("Warning: no namespaces found!");
        }
    }

    // Documentation generation
    // -------------------------------------------------------------------------
    protected void generatePropertiesFile(String namespace, List elements) throws IOException {
        File file = new File(metaInfDir + NamespaceHelper.createDiscoveryPathName(namespace));
        file.getParentFile().mkdirs();
        System.out.println("Generating META-INF properties file: " + file + " for namespace: " + namespace);
        PrintWriter out = new PrintWriter(new FileWriter(file));
        try {
            generatePropertiesFile(out, namespace, elements);
        }
        finally {
            out.close();
        }
    }

    protected void generatePropertiesFile(PrintWriter out, String namespace, List elements) {
        out.println("# NOTE: this file is autogenerated by XBeans");
        out.println();
        out.println("# beans");

        for (Iterator iter = elements.iterator(); iter.hasNext();) {
            SchemaElement element = (SchemaElement) iter.next();
            out.println(element.getLocalName() + " = " + element.getType().getQualifiedName());
        }
    }

    // Documentation generation
    // -------------------------------------------------------------------------
    protected void generateDocumentation(String namespace, List elements) throws IOException {
        // TODO can only handle 1 schema document so far...
        File file = new File(destFile.getParentFile(), destFile.getName() + ".html");
        System.out.println("Generating HTML documentation file: " + file + " for namespace: " + namespace);
        PrintWriter out = new PrintWriter(new FileWriter(file));
        try {
            generateDocumentation(out, namespace, elements);
        }
        finally {
            out.close();
        }
    }

    private void generateDocumentation(PrintWriter out, String namespace, List elements) {
        out.println("<!-- NOTE: this file is autogenerated by XBeans -->");
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Schema for namespace: " + namespace + "</title>");
        out.println("<link rel='stylesheet' href='style.css' type='text/css'>");
        out.println("<link rel='stylesheet' href='http://activemq.org/style.css' type='text/css'>");
        out.println("<link rel='stylesheet' href='http://activemq.org/style-xb.css' type='text/css'>");
        out.println("</head>");
        out.println();
        out.println("<body>");
        out.println();

        if (rootElement != null) {
            out.println("<h1>Root Element</h1>");
            out.println("<table>");
            out.println("  <tr><th>Element</th><th>Description</th><th>Class</th>");
            generateHtmlElementSummary(out, rootElement);
            out.println("</table>");
            out.println();
        }

        out.println("<h1>Element Summary</h1>");
        out.println("<table>");
        out.println("  <tr><th>Element</th><th>Description</th><th>Class</th>");
        for (Iterator iter = elements.iterator(); iter.hasNext();) {
            SchemaElement element = (SchemaElement) iter.next();
            generateHtmlElementSummary(out, element);
        }
{'fraction_non_alphanumeric': 0.14267290020957601, 'fraction_numerical': 0.0024181847493148475, 'mean_word_length': 2.6717963894643386, 'pattern_counts': {'":': 0, '<': 63, '<?xml version=': 0, '>': 63, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5710059', 'n_tokens_mistral': 3257, 'n_tokens_neox': 3163, 'n_words': 829}