<commit_before>package org.helioviewer.jhv.viewmodel.metadata;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Optional;

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

import org.helioviewer.jhv.base.logging.Log;
import org.w3c.dom.CharacterData;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.google.common.primitives.Doubles;
import com.google.common.primitives.Ints;

public class XMLMetaDataContainer implements MetaDataContainer {

    private NodeList nodeList;

    public void parseXML(String xml) throws Exception {
        try (InputStream in = new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8))) {
            DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
            nodeList = builder.parse(in).getElementsByTagName("meta");
        } catch (Exception e) {
            throw new Exception("Failed parsing XML data", e);
        }
    }

    public void destroyXML() {
        nodeList = null;
    }

    private String getValueFromXML(String key) {
        NodeList value = ((Element) nodeList.item(0)).getElementsByTagName(key);
        Element line = (Element) value.item(0);
        if (line == null)
            return null;

        Node child = line.getFirstChild();
        if (child instanceof CharacterData)
            return ((CharacterData) child).getData();
        return null;
    }

    @Override
    public String get(String key) {<commit_msg>Reduce<commit_after>package org.helioviewer.jhv.viewmodel.metadata;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Optional;

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

import org.helioviewer.jhv.base.logging.Log;
import org.w3c.dom.CharacterData;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

import com.google.common.primitives.Doubles;
import com.google.common.primitives.Ints;

public class XMLMetaDataContainer implements MetaDataContainer {

    Element meta;

    public void parseXML(String xml) throws Exception {
        try (InputStream in = new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8))) {
            DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
            meta = (Element) builder.parse(in).getElementsByTagName("meta").item(0);
        } catch (Exception e) {
            throw new Exception("XML metadata parse failure: ", e);
        }

        if (meta == null)
            throw new Exception("XML metadata without meta tag");
    }

    public void destroyXML() {
        meta = null;
    }

    private String getValueFromXML(String key) {
        Element line = (Element) meta.getElementsByTagName(key).item(0);
        if (line == null)
            return null;

        Node child = line.getFirstChild();
        if (child instanceof CharacterData)
            return ((CharacterData) child).getData();
        return null;
    }

    @Override
    public String get(String key) {
{'fraction_non_alphanumeric': 0.10090880601692259, 'fraction_numerical': 0.00407395800689439, 'mean_word_length': 3.3135135135135134, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, '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': '4045346', 'n_tokens_mistral': 916, 'n_tokens_neox': 895, 'n_words': 206}