<commit_before>/**
 * Copyright (C) 2009 - 2011 by OpenGamma Inc.
 *
 * Please see distribution for license.
 */
package com.opengamma.web.bundle;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import com.opengamma.OpenGammaRuntimeException;
import com.opengamma.util.ArgumentChecker;

/**
 * Parses a given bundle xml file
 */
public class BundleParser {
  
  private static final String FRAGMENT_ELEMENT = "fragment";
  private static final String ID_ATTR = "id";
  private static final String BUNDLE_ELEMENT = "bundle";

  private static final Logger s_logger = LoggerFactory.getLogger(BundleParser.class);
  
  /**
   * The bundle xml file to parse
   */
  private final File _xmlFile;
  /**
   * The base directory for web resource
   */
  private final File _baseDir;
  
  /**
   * Empty bundleManager
   */
  private final BundleManager _bundleManager = new BundleManager();
  /**
   * Elements map
   */
  private final Map<String, Element> _elementsByIdMap = new HashMap<String, Element>(); 
  
  /**
   * Creates a BundleParser with xml file
   * 
   * @param xmlFile   the xml file to parse, not null
   */
  public BundleParser(File xmlFile) {
    this(xmlFile, null);
  }
  
  /**
   * Creates a BundleParser with xml file and base dir
   * 
   * @param xmlFile   the xml file to parse, not null
   * @param baseDir   the base directory for web resource, not null
   */
  public BundleParser(File xmlFile, File baseDir) {
    validate(xmlFile, baseDir);
    _xmlFile = xmlFile;
    _baseDir = baseDir;
  }
 
  private void validate(File xmlFile, File baseDir) {
    ArgumentChecker.notNull(xmlFile, "xmlFile");
    ArgumentChecker.isTrue(xmlFile.exists(), xmlFile + "does not exists");
    
    if (baseDir != null) {
      ArgumentChecker.isTrue(baseDir.exists(), baseDir + "does not exists");
    }
  }

  public BundleManager getBundleManager() {
    DocumentBuilder builder = getDocumentBuilder();
    if (builder != null) {
      try {
        Document document = builder.parse(_xmlFile);
        processXMLDocument(document);
      } catch (SAXException ex) {
        s_logger.warn("Unable to parse : " + _xmlFile.getAbsolutePath(), ex);
      } catch (IOException ex) {
        s_logger.warn("Unable to read : " + _xmlFile.getAbsolutePath(), ex);
      }
    }
    return _bundleManager;<commit_msg>[PLAT-1002] removed validation in BundleParser for now<commit_after>/**
 * Copyright (C) 2009 - 2011 by OpenGamma Inc.
 *
 * Please see distribution for license.
 */
package com.opengamma.web.bundle;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import com.opengamma.OpenGammaRuntimeException;
import com.opengamma.util.ArgumentChecker;

/**
 * Parses a given bundle xml file
 */
public class BundleParser {
  
  private static final String FRAGMENT_ELEMENT = "fragment";
  private static final String ID_ATTR = "id";
  private static final String BUNDLE_ELEMENT = "bundle";

  private static final Logger s_logger = LoggerFactory.getLogger(BundleParser.class);
  
  /**
   * The bundle xml file to parse
   */
  private final File _xmlFile;
  /**
   * The base directory for web resource
   */
  private final File _baseDir;
  
  /**
   * Empty bundleManager
   */
  private final BundleManager _bundleManager = new BundleManager();
  /**
   * Elements map
   */
  private final Map<String, Element> _elementsByIdMap = new HashMap<String, Element>(); 
  
  /**
   * Creates a BundleParser with xml file
   * 
   * @param xmlFile   the xml file to parse, not null
   */
  public BundleParser(File xmlFile) {
    this(xmlFile, null);
  }
  
  /**
   * Creates a BundleParser with xml file and base dir
   * 
   * @param xmlFile   the xml file to parse, not null
   * @param baseDir   the base directory for web resource, not null
   */
  public BundleParser(File xmlFile, File baseDir) {
//    validate(xmlFile, baseDir);
    _xmlFile = xmlFile;
    _baseDir = baseDir;
  }
 
  private void validate(File xmlFile, File baseDir) {
    ArgumentChecker.notNull(xmlFile, "xmlFile");
    ArgumentChecker.isTrue(xmlFile.exists(), xmlFile + "does not exists");
    
    if (baseDir != null) {
      ArgumentChecker.isTrue(baseDir.exists(), baseDir + "does not exists");
    }
  }

  public BundleManager getBundleManager() {
    DocumentBuilder builder = getDocumentBuilder();
    if (builder != null) {
      try {
        Document document = builder.parse(_xmlFile);
        processXMLDocument(document);
      } catch (SAXException ex) {
        s_logger.warn("Unable to parse : " + _xmlFile.getAbsolutePath(), ex);
      } catch (IOException ex) {
        s_logger.warn("Unable to read : " + _xmlFile.getAbsolutePath(), ex);
      }
    }
    return _bundleManager;
{'fraction_non_alphanumeric': 0.10393107162089392, 'fraction_numerical': 0.005744031592173757, 'mean_word_length': 3.4152139461172744, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 54}, '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': '7248656', 'n_tokens_mistral': 1730, 'n_tokens_neox': 1685, 'n_words': 469}