All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sf.gluebooster.java.booster.basic.text.xml.File2XmlEventReader Maven / Gradle / Ivy

package net.sf.gluebooster.java.booster.basic.text.xml;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.security.InvalidParameterException;
import java.util.HashMap;
import java.util.Map;

import javax.swing.text.html.HTML;

import net.sf.gluebooster.java.booster.basic.container.ExceptionThrowingMapImplementation;

/**
 * Changes the contents of a xml file into an XmlReader.
 * @author Christian Bauer
 *
 */
@SuppressWarnings("rawtypes")
public class File2XmlEventReader extends ExceptionThrowingMapImplementation> 
{
  /**
   * Key in the map to indicate that missing files should result in a skip and no exception
   */
  public static String SKIP_MISSING_SOURCE_KEY="skipMissingSource";
  
  /**
   * Default constructor.
   */
  public File2XmlEventReader()
  {
     
  }
  
  /**
   * Reads the filename of an xml file from a parameter map and puts the XmlEventReader of this file into the returned map.
   * Algorithm:
   * parameterMap->value of key 'src' is filename of xml file -> the event reader of this file is put into the result map under the key 'html' 
   * @param map must be a map
   * key:   HTML.Attribute.SRC.toString  -> the filename
   * optional SKIP_MISSING_SOURCE_KEY -> if true, then missing source files are ignored, no exception ist thrown
   * @return a map with the entry
   * key = HTML.Tag.HTML
   * value = XmlEventReader
   */
  @SuppressWarnings({ "unchecked" })
@Override
  public Map get(Object parameterMap)
  {
    try
    {
      Map map = (Map) parameterMap;
      String filename = (String) map.get(HTML.Attribute.SRC.toString());
      if (filename == null)
        throw new InvalidParameterException("Parameter "+ HTML.Attribute.SRC + "not found in " + map);

      HashMap result = new HashMap(); 
      
      File file = new File(filename);
      if ( file.exists())
      {  
        result.put(HTML.Tag.HTML, XMLFactoryMethods.getXMLEventReader(new FileInputStream(file)));
      }
      else
      {
        if (map.containsKey(SKIP_MISSING_SOURCE_KEY) && "true".equals(map.get(SKIP_MISSING_SOURCE_KEY).toString().toLowerCase()))
        {
          //all is ok, nothing needs to be done
        }
        else  
          throw new FileNotFoundException(file.getCanonicalPath());
      }
      return result;
    }
    catch (Exception ex)
    {
      throw new RuntimeException(ex);
    }
  }

 
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy