<commit_before>package org.openhab;

/*
 * Copyright 2001-2005 The Apache Software Foundation.
 *
 * 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.
 */

import com.github.mustachejava.DefaultMustacheFactory;
import com.github.mustachejava.Mustache;
import com.github.mustachejava.MustacheFactory;
import org.apache.log4j.BasicConfigurator;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.openhab.data.*;
import org.openhab.schemas.config_description.v1_0.ConfigDescription;
import org.openhab.schemas.config_description.v1_0.ConfigDescriptions;
import org.openhab.schemas.thing_description.v1_0.*;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import java.io.File;
import java.io.FileWriter;
import java.util.HashMap;
import java.util.List;

/**
 * Goal which touches a timestamp file.
 * <p/>
 * NOTE: CARE FOR THE NAMESPACE IN {@link ThingDescriptions} and {@link ConfigDescriptions}.
 *
 * @goal generate-docu
 * @phase process-sources
 */
public class MyMojo extends AbstractMojo {

    private static final String XMLSCHEMA = "http://eclipse.org/smarthome/schemas/binding/v1.0.0";
    private static final String THINGXSD = "http://eclipse.org/smarthome/schemas/thing-description-1.0.0.xsd";
    private static final String CHANNEL_GROUP_TYPE = "channel-group-type";

    private ChannelList channels = new ChannelList();
    private ThingList things = new ThingList();
    private ChannelGroupList channelGroups = new ChannelGroupList();
    private BridgeList bridges = new BridgeList();
    private ConfigurationList configList = new ConfigurationList();

    public void execute() throws MojoExecutionException {
        // Configure loggers
        BasicConfigurator.configure();

        // EXAMPLE: ESH-INF
//        String eshDir = "src/test/resources/ESH-INF/";
//        parseThingDescripions(eshDir + "thing/channels.xml");
//        parseThingDescripions(eshDir + "thing/moon.xml");
//        parseThingDescripions(eshDir + "thing/sun.xml");
//        parseThingDescripions(eshDir + "thing/bridge.xml");
//        parseConfigDescriptions(eshDir + "config/config.xml");

        // EXAMPLE: ESH-INF-BOSCH
        String eshDir = "src/test/resources/ESH-INF-BOSCH/";
        parseThingDescripions(eshDir + "thing/thing-types.xml");


        try {
            // Compile mustache template
            MustacheFactory mf = new DefaultMustacheFactory();
            Mustache mustache = mf.compile("src/main/resources/templates/main.mustache");
            HashMap<String, Object> scope = new HashMap<String, Object>();
            scope.put("bridgeList", bridges);
            scope.put("thingList", things);
            scope.put("channelList", channels);
            scope.put("channelGroupList", channelGroups);
            scope.put("configList", configList);
            mustache.execute(new FileWriter("generated-docu-mustache.md"), scope).flush();
        } catch (Exception e) {
            getLog().error(e);
        }
    }

    /**<commit_msg>Adding a parameter to the Mojo to set the path to the ESH-INF directory. The Mojo now scans this directory for xml.<commit_after>package org.openhab;

/*
 * Copyright 2001-2005 The Apache Software Foundation.
 *
 * 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.
 */

import com.github.mustachejava.DefaultMustacheFactory;
import com.github.mustachejava.Mustache;
import com.github.mustachejava.MustacheFactory;
import org.apache.log4j.BasicConfigurator;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.openhab.data.*;
import org.openhab.schemas.config_description.v1_0.ConfigDescription;
import org.openhab.schemas.config_description.v1_0.ConfigDescriptions;
import org.openhab.schemas.thing_description.v1_0.*;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import java.io.File;
import java.io.FileWriter;
import java.util.HashMap;
import java.util.List;

/**
 * Goal which touches a timestamp file.
 * <p/>
 * NOTE: CARE FOR THE NAMESPACE IN {@link ThingDescriptions} and {@link ConfigDescriptions}.
 *
 * @goal generate-docu
 * @phase process-sources
 */
public class MyMojo extends AbstractMojo {

    //    private static final String XMLSCHEMA = "http://eclipse.org/smarthome/schemas/binding/v1.0.0";
//    private static final String THINGXSD = "http://eclipse.org/smarthome/schemas/thing-description-1.0.0.xsd";
    private static final String THING_SUBDIR = "thing/";
    private static final String BINDING_SUBDIR = "binding/";
    private static final String CONFIG_SUBDIR = "config/";

    private ChannelList channels = new ChannelList();
    private ThingList things = new ThingList();
    private ChannelGroupList channelGroups = new ChannelGroupList();
    private BridgeList bridges = new BridgeList();
    private ConfigurationList configList = new ConfigurationList();

    /**
     * @parameter name="esh-inf-dir" alias="eshInfDir" default-value="ESH-INF/"
     */
    private String eshDir;


    public void execute() throws MojoExecutionException {
        // Configure loggers
        BasicConfigurator.configure();

        // EXAMPLE: ESH-INF
//        eshDir = "src/test/resources/ESH-INF/";
//        parseThingDescripions(eshDir + "thing/channels.xml");
//        parseThingDescripions(eshDir + "thing/moon.xml");
//        parseThingDescripions(eshDir + "thing/sun.xml");
//        parseThingDescripions(eshDir + "thing/bridge.xml");
//        parseConfigDescriptions(eshDir + "config/config.xml");


        // EXAMPLE: ESH-INF-BOSCH
        eshDir = "src/test/resources/ESH-INF-BOSCH/";
        scanDir();

        try {
            // Compile mustache template
            MustacheFactory mf = new DefaultMustacheFactory();
            Mustache mustache = mf.compile("src/main/resources/templates/main.mustache");
            HashMap<String, Object> scope = new HashMap<String, Object>();
            scope.put("bridgeList", bridges);
            scope.put("thingList", things);
            scope.put("channelList", channels);
            scope.put("channelGroupList", channelGroups);
            scope.put("configList", configList);
            mustache.execute(new FileWriter("generated-docu-mustache.md"), scope).flush();
        } catch (Exception e) {
            getLog().error(e);
        }
    }

    /**
     * Scans the given eshDir for xml files.
     */
    private void scanDir() {
        // Scan the things directory
        File things = new File(eshDir + THING_SUBDIR);
        if (things.exists() && things.isDirectory()) {
            for (File file : things.listFiles()) {
                if (file.getName().endsWith(".xml")) {
                    getLog().info("Found thing xml: " + file.getName());
                    parseThingDescripions(file.getAbsolutePath());
                }
            }
        }

        // Scan the config directory.
        File configs = new File(eshDir + CONFIG_SUBDIR);
        if (configs.exists() && configs.isDirectory()) {
            for (File file : things.listFiles()) {
                if (file.getName().endsWith(".xml")) {
                    getLog().info("Found config xml: " + file.getName());
                    parseConfigDescriptions(file.getAbsolutePath());
                }
            }
        }
    }

    /**
{'fraction_non_alphanumeric': 0.1134057538498269, 'fraction_numerical': 0.0059687238868329955, 'mean_word_length': 3.0182254196642684, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 21}, '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': '4653674', 'n_tokens_mistral': 2415, 'n_tokens_neox': 2320, 'n_words': 597}