<commit_before>/* Copyright 2003 The JA-SIG Collaborative.  All rights reserved.
*  See license distributed with this file and
*  available online at http://www.uportal.org/license.html
*/

package org.jasig.portal.tools.chanpub;

import java.io.File;
import java.io.FileFilter;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.jasig.portal.ChannelCategory;
import org.jasig.portal.ChannelDefinition;
import org.jasig.portal.ChannelParameter;
import org.jasig.portal.ChannelRegistryStoreFactory;
import org.jasig.portal.ChannelType;
import org.jasig.portal.Constants;
import org.jasig.portal.EntityIdentifier;
import org.jasig.portal.IChannelRegistryStore;
import org.jasig.portal.groups.IEntity;
import org.jasig.portal.groups.IEntityGroup;
import org.jasig.portal.groups.IGroupConstants;
import org.jasig.portal.security.IAuthorizationPrincipal;
import org.jasig.portal.security.IPermission;
import org.jasig.portal.security.IPerson;
import org.jasig.portal.security.IUpdatingPermissionManager;
import org.jasig.portal.security.PersonFactory;
import org.jasig.portal.services.AuthorizationService;
import org.jasig.portal.services.GroupService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jasig.portal.utils.ResourceLoader;
import org.jasig.portal.utils.XML;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * This is a Channel Publisher tool to install uPortal channels from outside of
 * the portal or from within a channel archive.
 * Currently configured to be executed via Jakarta Ant or via a 
 * channel-definition block within a CAR deployment descriptor.
 *
 * Sample of command line arguments:
 * ant publish -Dchannel=all  (this will publish all channels that have a corresponding xml file)
 * ant publish -Dchannel=webmail.xml  (this will publish the specified channels)
 *
 * @author <NAME>, <EMAIL>
 * @author <NAME>, <EMAIL>
 * @author <NAME>, <EMAIL>
 * @version $Revision$
 */
public class ChannelPublisher
{
    private static final Log log = LogFactory.getLog(ChannelPublisher.class);

    private static final String FRAMEWORK_OWNER = IPermission.PORTAL_FRAMEWORK;
    private static final String SUBSCRIBER_ACTIVITY =
        IPermission.CHANNEL_SUBSCRIBER_ACTIVITY;
    private static final String GRANT_PERMISSION_TYPE =
        IPermission.PERMISSION_TYPE_GRANT;

    private static final int LOAD_ALL_FILES = 0;
    private static final int LOAD_ONE_FILE = 1;

    private IPerson systemUser;
    private DocumentBuilder domParser;
    private IChannelRegistryStore crs;
    private Map chanTypesNamesToIds;
    private boolean mOnCommandLine = false;
    private boolean mOverrideExisting = false;

    private static final String chanDefsLocation = "/properties/chanpub";

    /**
     * @param args
     * @throws Exception We let Exceptions bubble up so that ant will know 
     * that the publishing failed and can report the error message and stack
     * trace to the user.
     */
    public static void main(String[] args) throws Exception{

        /*
        
        Channel Publisher Tool Workflow.
        1) read all or specified channel.xml file
        
        ant publish -Dchannel=all or -Dchannel=webmail.xml
        
        2) validate each against the channelDefinition.dtd file
        
        3) publish one channel at a time
        
        */
        
        // determine whether user wants to publish one or all of the channels in current directory
        int mode = LOAD_ALL_FILES;

        if (args[1] != null && args[1].length() > 0) {
            // MODE = 0 for all channels in directory
            // MODE = 1 for individual channel
            if (args[1].equals("all"))
                mode = LOAD_ALL_FILES;
            else
                mode = LOAD_ONE_FILE;

        }

        ChannelPublisher publisher = getCommandLineInstance();

        // determine what mode we are in
        if (mode == LOAD_ONE_FILE) {
            System.out.println(
                "You have chosen to publish one channel.....");
            System.out.print("Publishing channel " + args[1] + ".....");
            // lets publish one channel only
            publisher.publishChannel(args[1]);
            System.out.println("Done");
        } else {
            // lets publish all channels in directory
            System.out.println ("You have chosen to publish all channels.....");

            // user has selected to publish all channel in the /channels directory
            // lets publish all channel one by one that is
            // create InputStream object to pass to next method
            File f =
                ResourceLoader.getResourceAsFile(
                    ChannelPublisher.class,
                    chanDefsLocation + "/");
            if (f.isDirectory()) {
                
                // Consider only files that end in .xml
                class ChannelDefFileFilter implements FileFilter {
                    public boolean accept(File file) {
                        return file.getName().endsWith(".xml");
                    }
                }
                File[] files = f.listFiles(new ChannelDefFileFilter());
                
                for (int j = 0; j < files.length; j++) {
                    System.out.print("Publishing channel " + files[j].getName() + ".....");
                    // lets publish one at a time
                    publisher.publishChannel(files[j].getName());
                    System.out.println("Done!");
                }
            }
        }
        System.out.println("Publishing finished.");<commit_msg>UP-993: backed out previous commit<commit_after>/* Copyright 2003 The JA-SIG Collaborative.  All rights reserved.
*  See license distributed with this file and
*  available online at http://www.uportal.org/license.html
*/

package org.jasig.portal.tools.chanpub;

import java.io.File;
import java.io.FileFilter;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.jasig.portal.ChannelCategory;
import org.jasig.portal.ChannelDefinition;
import org.jasig.portal.ChannelParameter;
import org.jasig.portal.ChannelRegistryStoreFactory;
import org.jasig.portal.ChannelType;
import org.jasig.portal.Constants;
import org.jasig.portal.EntityIdentifier;
import org.jasig.portal.IChannelRegistryStore;
import org.jasig.portal.groups.IEntity;
import org.jasig.portal.groups.IEntityGroup;
import org.jasig.portal.groups.IGroupConstants;
import org.jasig.portal.security.IAuthorizationPrincipal;
import org.jasig.portal.security.IPermission;
import org.jasig.portal.security.IPerson;
import org.jasig.portal.security.IUpdatingPermissionManager;
import org.jasig.portal.security.PersonFactory;
import org.jasig.portal.services.AuthorizationService;
import org.jasig.portal.services.GroupService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jasig.portal.utils.ResourceLoader;
import org.jasig.portal.utils.XML;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * This is a Channel Publisher tool to install uPortal channels from outside of
 * the portal or from within a channel archive.
 * Currently configured to be executed via Jakarta Ant or via a 
 * channel-definition block within a CAR deployment descriptor.
 *
 * Sample of command line arguments:
 * ant publish -Dchannel=all  (this will publish all channels that have a corresponding xml file)
 * ant publish -Dchannel=webmail.xml  (this will publish the specified channels)
 *
 * @author <NAME>, <EMAIL>
 * @author <NAME>, <EMAIL>
 * @author <NAME>, <EMAIL>
 * @version $Revision$
 */
public class ChannelPublisher
{
    private static final Log log = LogFactory.getLog(ChannelPublisher.class);

    private static final String FRAMEWORK_OWNER = IPermission.PORTAL_FRAMEWORK;
    private static final String SUBSCRIBER_ACTIVITY =
        IPermission.CHANNEL_SUBSCRIBER_ACTIVITY;
    private static final String GRANT_PERMISSION_TYPE =
        IPermission.PERMISSION_TYPE_GRANT;

    private static final int LOAD_ALL_FILES = 0;
    private static final int LOAD_ONE_FILE = 1;

    private IPerson systemUser;
    private DocumentBuilder domParser;
    private IChannelRegistryStore crs;
    private Map chanTypesNamesToIds;
    private boolean mOnCommandLine = false;
    private boolean mOverrideExisting = false;

    private static final String chanDefsLocation = "/properties/chanpub";

    public static void main(String[] args) {

        /*
        
        Channel Publisher Tool Workflow.
        1) read all or specified channel.xml file
        
        ant publish -Dchannel=all or -Dchannel=webmail.xml
        
        2) validate each against the channelDefinition.dtd file
        
        3) publish one channel at a time
        
        */

        try {

            // determine whether user wants to publish one or all of the channels in current directory
            int mode = LOAD_ALL_FILES;

            if (args[1] != null && args[1].length() > 0) {
                // MODE = 0 for all channels in directory
                // MODE = 1 for individual channel
                if (args[1].equals("all"))
                    mode = LOAD_ALL_FILES;
                else
                    mode = LOAD_ONE_FILE;

            }

            ChannelPublisher publisher = getCommandLineInstance();

            // determine what mode we are in
            if (mode == LOAD_ONE_FILE) {
                System.out.println(
                    "You have chosen to publish one channel.....");
                System.out.print("Publishing channel " + args[1] + ".....");
                // lets publish one channel only
                publisher.publishChannel(args[1]);
                System.out.println("Done");
            } else {
                // lets publish all channels in directory
                System.out.println ("You have chosen to publish all channels.....");

                // user has selected to publish all channel in the /channels directory
                // lets publish all channel one by one that is
                // create InputStream object to pass to next method
                File f =
                    ResourceLoader.getResourceAsFile(
                        ChannelPublisher.class,
                        chanDefsLocation + "/");
                if (f.isDirectory()) {
                        
                    // Consider only files that end in .xml
                    class ChannelDefFileFilter implements FileFilter {
                        public boolean accept(File file) {
                            return file.getName().endsWith(".xml");
                        }
                    }
                    File[] files = f.listFiles(new ChannelDefFileFilter());

                    for (int j = 0; j < files.length; j++) {
                        try {
                            System.out.print("Publishing channel " + files[j].getName() + ".....");
                            // lets publish one at a time
                            publisher.publishChannel(files[j].getName());
                            System.out.println("Done!");
                        } catch (Exception e) {
                            System.out.println("FAILED!");
                            log.error("Exception occurred while publishing " + files[j].getName() + " channel. ", e);
                        }
                    }
                }
            }
        }
        catch (Exception e)
        {
            // Need to revisit this and handle the error!
            System.out.println(
                "Exception occurred in main method. "
                    + e
                    + ". Please see portal.log for details");
            log.error("main() :: Exception occurred in main method.", e);
            e.printStackTrace();
        }

        // Done!
        System.out.println("Publishing finished.");
        System.exit(0);
{'fraction_non_alphanumeric': 0.08828699264940876, 'fraction_numerical': 0.003835091083413231, 'mean_word_length': 2.229360165118679, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 16}, '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': '3400201', 'n_tokens_mistral': 3334, 'n_tokens_neox': 3222, 'n_words': 955}