<commit_before>/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.
 */
package org.apache.forrest.dispatcher.impl.helper;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.stream.XMLEventWriter;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamWriter;
import javax.xml.stream.util.XMLEventAllocator;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamSource;

import org.apache.forrest.dispatcher.impl.XSLContract;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class XSLContractHelper extends StAX {

  private Transformer transformer = null;

  /**
   * This method will prepare the transformation from the data and the xsl
   * stylesheet from the contract.
   * 
   * @param xslSource
   *          the xsl stylesheet
   * @param allowXmlProperties
   *          whether or not we want to allow xml properties
   * @throws TransformerConfigurationException
   * @throws ParserConfigurationException 
   * @throws IOException 
   * @throws SAXException 
   */
  public void prepareTransformation(Source xslSource, boolean allowXmlProperties, HashMap<String, ?> params)
      throws TransformerConfigurationException, ParserConfigurationException, SAXException, IOException {
    TransformerFactory transFact = TransformerFactory.newInstance();
    // prepare transformation
    transformer = transFact.newTransformer(xslSource);
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    // do we allow xml properties?
    if(!allowXmlProperties){
      for (Iterator<String> iter = params.keySet().iterator(); iter.hasNext();) {
        String key = iter.next();
        String value = (String) params.get(key);
        transformer.setParameter(key,value);
      }
    }else{
      DocumentBuilder builder = DocumentBuilderFactory.newInstance()
      .newDocumentBuilder();
      for (Iterator<String> iter = params.keySet().iterator(); iter.hasNext();) {
        String key = iter.next();
        InputSource source = (InputSource) params.get(key);
        transformer.setParameter(key, builder.parse(source));
      }
    }
  }

  /**
   * Will create a dummy xml document with only one element.
   * 
   * @return ByteArrayOutputStream of the dummy xml document.
   * @throws XMLStreamException
   */
  public ByteArrayOutputStream createEmptyXmlOutput() throws XMLStreamException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    XMLStreamWriter writer = getStreamWriter(out);
    writer.writeStartDocument("UTF-8", "1.0");
    writer.writeStartElement("foo");
    writer.writeEndDocument();
    writer.flush();
    writer.close();
    return out;
  }

  /**
   * Will create a dummy xml document with only one element contacting
   * {@link #createEmptyXmlOutput()} and then switching the outputStream to an<commit_msg>Using the enhanced properties treatment. Looking if the value is of type InputSource. If so we will request a DOM represent of the source. If not we will pass a simple String.<commit_after>/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.
 */
package org.apache.forrest.dispatcher.impl.helper;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.stream.XMLEventWriter;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamWriter;
import javax.xml.stream.util.XMLEventAllocator;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamSource;

import org.apache.forrest.dispatcher.impl.XSLContract;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class XSLContractHelper extends StAX {

  private Transformer transformer = null;

  /**
   * This method will prepare the transformation from the data and the xsl
   * stylesheet from the contract.
   * 
   * @param xslSource
   *          the xsl stylesheet
   * @param allowXmlProperties
   *          whether or not we want to allow xml properties
   * @throws TransformerConfigurationException
   * @throws ParserConfigurationException 
   * @throws IOException 
   * @throws SAXException 
   */
  public void prepareTransformation(Source xslSource, boolean allowXmlProperties, HashMap<String, ?> params)
      throws TransformerConfigurationException, ParserConfigurationException, SAXException, IOException {
    TransformerFactory transFact = TransformerFactory.newInstance();
    // prepare transformation
    transformer = transFact.newTransformer(xslSource);
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    // do we allow xml properties?
    if(allowXmlProperties){
      DocumentBuilder builder = DocumentBuilderFactory.newInstance()
      .newDocumentBuilder();
      for (Iterator<String> iter = params.keySet().iterator(); iter.hasNext();) {
        String key = iter.next();
        Class<InputSource> source = InputSource.class;
        Object value = params.get(key);
        if (source.isInstance(value)){
          transformer.setParameter(key, builder.parse((InputSource)value));
        }else{
          transformer.setParameter(key,value);
        }
      }
    }else{
      for (Iterator<String> iter = params.keySet().iterator(); iter.hasNext();) {
        String key = iter.next();
        String value = (String) params.get(key);
        transformer.setParameter(key,value);
      }
      
    }
  }

  /**
   * Will create a dummy xml document with only one element.
   * 
   * @return ByteArrayOutputStream of the dummy xml document.
   * @throws XMLStreamException
   */
  public ByteArrayOutputStream createEmptyXmlOutput() throws XMLStreamException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    XMLStreamWriter writer = getStreamWriter(out);
    writer.writeStartDocument("UTF-8", "1.0");
    writer.writeStartElement("foo");
    writer.writeEndDocument();
    writer.flush();
    writer.close();
    return out;
  }

  /**
   * Will create a dummy xml document with only one element contacting
   * {@link #createEmptyXmlOutput()} and then switching the outputStream to an
{'fraction_non_alphanumeric': 0.08834000890075656, 'fraction_numerical': 0.001557632398753894, 'mean_word_length': 4.160160734787601, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 50}, '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': '5664691', 'n_tokens_mistral': 2341, 'n_tokens_neox': 2318, 'n_words': 710}