<commit_before>// Copyright (C) 2011 jOVAL.org.  All rights reserved.
// This software is licensed under the AGPL 3.0 license available at http://www.joval.org/agpl_v3.txt

package org.joval.plugin.adapter.cisco.ios;

import java.util.Collection;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Vector;
import javax.xml.bind.JAXBElement;

import oval.schemas.common.MessageType;
import oval.schemas.common.MessageLevelEnumeration;
import oval.schemas.common.OperationEnumeration;
import oval.schemas.common.SimpleDatatypeEnumeration;
import oval.schemas.definitions.ios.GlobalObject;
import oval.schemas.systemcharacteristics.core.FlagEnumeration;
import oval.schemas.systemcharacteristics.core.ItemType;
import oval.schemas.systemcharacteristics.core.EntityItemStringType;
import oval.schemas.systemcharacteristics.ios.GlobalItem;
import oval.schemas.results.core.ResultEnumeration;

import org.joval.intf.cisco.system.IIosSession;
import org.joval.intf.cisco.system.ITechSupport;
import org.joval.intf.plugin.IAdapter;
import org.joval.intf.plugin.IRequestContext;
import org.joval.io.PerishableReader;
import org.joval.oval.CollectionException;
import org.joval.oval.OvalException;
import org.joval.util.JOVALMsg;
import org.joval.util.JOVALSystem;
import org.joval.util.SafeCLI;

/**
 * Provides Cisco IOS Global OVAL items.
 *
 * @author <NAME>
 * @version %I% %G%
 */
public class GlobalAdapter implements IAdapter {
    IIosSession session;

    public GlobalAdapter(IIosSession session) {
	this.session = session;
    }

    // Implement IAdapter

    private static Class[] objectClasses = {GlobalObject.class};

    public Class[] getObjectClasses() {
	return objectClasses;
    }

    public boolean connect() {
	return session != null;
    }

    public void disconnect() {
    }

    public Collection<JAXBElement<? extends ItemType>> getItems(IRequestContext rc) throws CollectionException, OvalException {
	Collection<JAXBElement<? extends ItemType>> items = new Vector<JAXBElement<? extends ItemType>>();
	items.add(JOVALSystem.factories.sc.ios.createGlobalItem(getItem()));
	return items;
    }

    // Private

    private GlobalItem getItem() throws CollectionException {
	List<String> lines = null;
	try {
	    lines = session.getTechSupport().getData(ITechSupport.GLOBAL);
	} catch (NoSuchElementException e) {
	    throw new CollectionException(e);
	}

	StringBuffer sb = new StringBuffer();
	for (String line : lines) {
	    if (sb.length() > 0) {
		sb.append('\n');
	    }
	    sb.append(line);
	}

	GlobalItem item = JOVALSystem.factories.sc.ios.createGlobalItem();
	if (sb.length() > 0) {
	    EntityItemStringType globalCommand = JOVALSystem.factories.sc.core.createEntityItemStringType();
	    globalCommand.setValue(sb.toString());
	    item.setGlobalCommand(globalCommand);
	}
	return item;
    }
}<commit_msg>Fixes for proper function of IOS global_test.<commit_after>// Copyright (C) 2011 jOVAL.org.  All rights reserved.
// This software is licensed under the AGPL 3.0 license available at http://www.joval.org/agpl_v3.txt

package org.joval.plugin.adapter.cisco.ios;

import java.util.Collection;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Vector;
import java.util.regex.Pattern;
import javax.xml.bind.JAXBElement;

import oval.schemas.common.MessageType;
import oval.schemas.common.MessageLevelEnumeration;
import oval.schemas.common.OperationEnumeration;
import oval.schemas.common.SimpleDatatypeEnumeration;
import oval.schemas.definitions.core.EntityObjectStringType;
import oval.schemas.definitions.ios.GlobalObject;
import oval.schemas.systemcharacteristics.core.FlagEnumeration;
import oval.schemas.systemcharacteristics.core.ItemType;
import oval.schemas.systemcharacteristics.core.EntityItemStringType;
import oval.schemas.systemcharacteristics.ios.GlobalItem;
import oval.schemas.results.core.ResultEnumeration;

import org.joval.intf.cisco.system.IIosSession;
import org.joval.intf.cisco.system.ITechSupport;
import org.joval.intf.plugin.IAdapter;
import org.joval.intf.plugin.IRequestContext;
import org.joval.io.PerishableReader;
import org.joval.oval.CollectionException;
import org.joval.oval.OvalException;
import org.joval.oval.ResolveException;
import org.joval.util.JOVALMsg;
import org.joval.util.JOVALSystem;
import org.joval.util.SafeCLI;

/**
 * Provides Cisco IOS Global OVAL items.
 *
 * @author <NAME>
 * @version %I% %G%
 */
public class GlobalAdapter implements IAdapter {
    IIosSession session;

    public GlobalAdapter(IIosSession session) {
	this.session = session;
    }

    // Implement IAdapter

    private static Class[] objectClasses = {GlobalObject.class};

    public Class[] getObjectClasses() {
	return objectClasses;
    }

    public boolean connect() {
	return session != null;
    }

    public void disconnect() {
    }

    private static final String BUILDING = "Building configuration...";
    private static final String CURRENT = "Current configuration";

    public Collection<JAXBElement<? extends ItemType>> getItems(IRequestContext rc) throws CollectionException, OvalException {
	GlobalObject gObj = (GlobalObject)rc.getObject();
	EntityObjectStringType globalCommand = gObj.getGlobalCommand();
	OperationEnumeration op = globalCommand.getOperation();

	List<String> lines = null;
	Collection<String> commands = new Vector<String>();
	try {
	    lines = session.getTechSupport().getData(ITechSupport.GLOBAL);

	    if (globalCommand.isSetVarRef()) {
		commands.addAll(rc.resolve(globalCommand.getVarRef()));
	    } else {
		commands.add((String)globalCommand.getValue());
	    }
	} catch (ResolveException e) {
	    throw new OvalException(e);
	} catch (NoSuchElementException e) {
	    throw new CollectionException(e);
	}

	Collection<GlobalItem> items = new Vector<GlobalItem>();
	for (String command : commands) {
	    for (String line : lines) {
		if (isGlobalCommand(line)) {
		    boolean add = false;

		    switch(op) {
		      case EQUALS:
			add = line.equals(command);
			break;

		      case PATTERN_MATCH:
			add = Pattern.matches(command, line);
			break;

		      default:
			throw new OvalException(JOVALSystem.getMessage(JOVALMsg.ERROR_UNSUPPORTED_OPERATION, op));
		    }

		    if (add) {
			GlobalItem item = JOVALSystem.factories.sc.ios.createGlobalItem();
			EntityItemStringType globalCommandType = JOVALSystem.factories.sc.core.createEntityItemStringType();
			globalCommandType.setValue(line);
			item.setGlobalCommand(globalCommandType);
			items.add(item);
		    }
		}
	    }
	}

	Collection<JAXBElement<? extends ItemType>> wrappedItems = new Vector<JAXBElement<? extends ItemType>>();
	for (GlobalItem item : items) {
	    wrappedItems.add(JOVALSystem.factories.sc.ios.createGlobalItem(item));
	}
	return wrappedItems;
    }

    private boolean isGlobalCommand(String line) {
	return line.length() > 0 &&		// not an empty line
	       !line.startsWith(BUILDING) &&	// not a remark
	       !line.startsWith(CURRENT) &&	// not a remark
	       !line.startsWith("!") &&		// not a comment
	       !line.startsWith(" ");		// global context command
    }
}
{'fraction_non_alphanumeric': 0.11027148054855863, 'fraction_numerical': 0.002378953260565351, 'mean_word_length': 4.447408536585366, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, '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': '6437183', 'n_tokens_mistral': 2281, 'n_tokens_neox': 2141, 'n_words': 454}