<commit_before>/*
 * Copyright 2009 <NAME> 
 * http://www.varaneckas.com
 *
 * Licensed 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 com.googlecode.xmlzen.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.nio.charset.Charset;

import com.googlecode.xmlzen.XmlZenException;

/**
 * XmlUtils - FIXME add documentation 
 * 
 * @author <NAME> &lt;<EMAIL>&gt;
 * @version $Id$
 */
public abstract class XmlUtils {

	/**
	 * Tries to read the <?xml ... encoding="???"?> header. 
	 * 
	 * Possible outcomes are listed here: 
	 * http://java.sun.com/j2se/1.5.0/docs/guide/intl/encoding.doc.html 
	 * 
	 * In case header is not found, Charset.defaultCharset().name() is returned
	 * 
	 * @param file
	 * @return
	 */
	public static String guessCharset(final File file) {
		InputStream in = null;
		try {
			in = new FileInputStream(file);
			//<?xml version="1.0" encoding="Windows-1257"?> ~50 bytes
			//100 bytes should be more than enough to read such a header
			byte[] head = new byte[100]; 
			in.read(head);
			return guessCharset(new String(head));
		} catch (final Exception e) {
			throw new XmlZenException("Failed guessing charset for file: " 
					+ file, e);
		} finally {
			FileUtils.close(in);
		}
	}

	public static String guessCharset(final String xml) {
		int offEnc = xml.indexOf("encoding");
		if (offEnc > 0) {
			//find the quote
			int offQuote = xml.indexOf('"', offEnc + 8);
			if (offQuote < 1) {
				offQuote = xml.indexOf('\'', offEnc + 8);
			}
			if (offQuote < 1) {
				return Charset.defaultCharset().name();
			}
			//find the ending quote offset and increase start offset
			int offQuoteEnd = xml.indexOf(xml.charAt(offQuote++), offQuote + 1);
			if (offQuoteEnd < 1) {
				//sounds like an incomplete header
				return Charset.defaultCharset().name();
			}
			//we'we located a header
			return xml.substring(offQuote, offQuoteEnd);
		} else {
			return Charset.defaultCharset().name();
		}
	}
	
	public static String getTagValue(String xml, String tag) {
	    return getTagValue(xml, tag, null, null, true);<commit_msg>SVN Id keywords, some comments<commit_after>/*
 * Copyright 2009 <NAME> 
 * http://www.varaneckas.com
 *
 * Licensed 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 com.googlecode.xmlzen.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.nio.charset.Charset;

import com.googlecode.xmlzen.XmlZenException;

/**
 * XmlUtils - FIXME add documentation 
 * 
 * @author <NAME> &lt;<EMAIL>&gt;
 * @version $Id$
 */
public abstract class XmlUtils {

	/**
	 * Tries to read the <?xml ... encoding="???"?> header. 
	 * <p>
	 * Possible outcomes are listed here: 
	 * http://java.sun.com/j2se/1.5.0/docs/guide/intl/encoding.doc.html</p>
	 * <p>
	 * In case header is not found, Charset.defaultCharset().name() is returned</p>
	 * 
	 * @param file XML File
	 * @return Possible charset name
	 */
	public static String guessCharset(final File file) {
		InputStream in = null;
		try {
			in = new FileInputStream(file);
			//<?xml version="1.0" encoding="Windows-1257"?> ~50 bytes
			//100 bytes should be more than enough to read such a header
			byte[] head = new byte[100]; 
			in.read(head);
			return guessCharset(new String(head));
		} catch (final Exception e) {
			throw new XmlZenException("Failed guessing charset for file: " 
					+ file, e);
		} finally {
			FileUtils.close(in);
		}
	}

	/**
	 * Tries to read the <?xml ... encoding="???"?> header. 
     * 
     * Possible outcomes are listed here: 
     * http://java.sun.com/j2se/1.5.0/docs/guide/intl/encoding.doc.html 
     * 
     * In case header is not found, Charset.defaultCharset().name() is returned
     * 
	 * @param xml XML String
	 * @return Possible charset name
	 */
	public static String guessCharset(final String xml) {
		int offEnc = xml.indexOf("encoding");
		if (offEnc > 0) {
			//find the quote
			int offQuote = xml.indexOf('"', offEnc + 8);
			if (offQuote < 1) {
				offQuote = xml.indexOf('\'', offEnc + 8);
			}
			if (offQuote < 1) {
				return Charset.defaultCharset().name();
			}
			//find the ending quote offset and increase start offset
			int offQuoteEnd = xml.indexOf(xml.charAt(offQuote++), offQuote + 1);
			if (offQuoteEnd < 1) {
				//sounds like an incomplete header
				return Charset.defaultCharset().name();
			}
			//we'we located a header
			return xml.substring(offQuote, offQuoteEnd);
		} else {
			return Charset.defaultCharset().name();
		}
	}
	
	public static String getTagValue(String xml, String tag) {
	    return getTagValue(xml, tag, null, null, true);
{'fraction_non_alphanumeric': 0.12617735916118714, 'fraction_numerical': 0.012440021325750844, 'mean_word_length': 3.276595744680851, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 2, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 28}, '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': '1083494', 'n_tokens_mistral': 1983, 'n_tokens_neox': 1718, 'n_words': 570}