<commit_before>package org.apache.commons.graph.export;

/*
 * 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.
 */

import static java.lang.String.format;

import java.io.Writer;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamWriter;

import org.apache.commons.graph.Edge;
import org.apache.commons.graph.Graph;
import org.apache.commons.graph.Labeled;
import org.apache.commons.graph.Vertex;
import org.apache.commons.graph.Weighted;

final class GraphMLExporter<V extends Vertex, E extends Edge, G extends Graph<V, E>>
    extends AbstractExporter<V, E, G>
{

    private static final String GRAPHML = "graphml";

    private static final String XMLNS = "xmlns";

    private static final String GRAPHML_XMLNS = "http://graphml.graphdrawing.org/xmlns";

    private static final String EDGEDEFAULT = "edgedefault";

    private static final String DIRECTED = "directed";

    private static final String KEY = "key";

    private static final String FOR = "for";

    private static final String ID = "id";

    private static final String ATTR_NAME = "attr.name";

    private static final String ATTR_TYPE = "attr.type";

    private static final String GRAPH = "graph";

    private static final String NODE = "node";

    private static final String EDGE = "edge";

    private static final String SOURCE = "source";

    private static final String TARGET = "target";

    private static final String DATA = "data";

    private static final String LABEL = "label";

    private static final String STRING = "string";

    private static final String FLOAT = "float";

    private static final String DOUBLE = "double";

    private static final String LONG = "long";

    private static final String BOOLEAN = "boolean";

    private static final String INT = "int";

    private static final String WEIGHT = "weight";

    public GraphMLExporter( G graph, Writer writer, String name )
    {
        super( graph, writer, name );
    }

    @Override
    protected void internalExport()
        throws Exception
    {
        // scan graph and print TYPES description for vertex AND edges' properties
        Map<String, String> edgeKeyTypes = new HashMap<String, String>();

        for ( E edge : getGraph().getEdges() )
        {
            if ( edge instanceof Labeled )
            {
                if ( !edgeKeyTypes.containsKey( LABEL ) )
                {
                    edgeKeyTypes.put( LABEL, getStringType( ( (Labeled) edge ).getLabel() ) );
                }
            }

            if ( edge instanceof Weighted )
            {
                if ( !edgeKeyTypes.containsKey( WEIGHT ) )
                {
                    edgeKeyTypes.put( WEIGHT, getStringType( ( (Weighted<?>) edge ).getWeight() ) );
                }
            }
        }

        // scan vertices
        Map<String, String> vertexKeyTypes = new HashMap<String, String>();

        for ( V vertex : getGraph().getVertices() )
        {
            if ( vertex instanceof Labeled )
            {
                if ( !vertexKeyTypes.containsKey( LABEL ) )
                {
                    vertexKeyTypes.put( LABEL, getStringType( ( (Labeled) vertex ).getLabel() ) );
                }
            }
        }

        XMLStreamWriter xmlWriter = XMLOutputFactory.newInstance().createXMLStreamWriter( getWriter() );

        // start document tokens
        xmlWriter.writeStartDocument();

        xmlWriter.writeComment( format( "Graph generated by Apache Commons Graph on %s%n", new Date() ) );

        xmlWriter.writeStartElement( GRAPHML );
        xmlWriter.writeAttribute( XMLNS, GRAPHML_XMLNS );

        xmlWriter.writeStartElement( GRAPH );
        xmlWriter.writeAttribute( ID, getName() );
        xmlWriter.writeAttribute( EDGEDEFAULT, DIRECTED );

        // write EDGE key types
        for ( Map.Entry<String, String> entry : edgeKeyTypes.entrySet() )
        {
            xmlWriter.writeStartElement( KEY );
            xmlWriter.writeAttribute( ID, entry.getKey() );
            xmlWriter.writeAttribute( FOR, EDGE );
            xmlWriter.writeAttribute( ATTR_NAME, entry.getKey() );
            xmlWriter.writeAttribute( ATTR_TYPE, entry.getValue() );
            xmlWriter.writeEndElement();
        }

        // write VERTICES' key types
        for ( Map.Entry<String, String> entry : vertexKeyTypes.entrySet() )
        {<commit_msg>Removed unnecessary new line in graph ml exporter.<commit_after>package org.apache.commons.graph.export;

/*
 * 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.
 */

import static java.lang.String.format;

import java.io.Writer;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamWriter;

import org.apache.commons.graph.Edge;
import org.apache.commons.graph.Graph;
import org.apache.commons.graph.Labeled;
import org.apache.commons.graph.Vertex;
import org.apache.commons.graph.Weighted;

final class GraphMLExporter<V extends Vertex, E extends Edge, G extends Graph<V, E>>
    extends AbstractExporter<V, E, G>
{

    private static final String GRAPHML = "graphml";

    private static final String XMLNS = "xmlns";

    private static final String GRAPHML_XMLNS = "http://graphml.graphdrawing.org/xmlns";

    private static final String EDGEDEFAULT = "edgedefault";

    private static final String DIRECTED = "directed";

    private static final String KEY = "key";

    private static final String FOR = "for";

    private static final String ID = "id";

    private static final String ATTR_NAME = "attr.name";

    private static final String ATTR_TYPE = "attr.type";

    private static final String GRAPH = "graph";

    private static final String NODE = "node";

    private static final String EDGE = "edge";

    private static final String SOURCE = "source";

    private static final String TARGET = "target";

    private static final String DATA = "data";

    private static final String LABEL = "label";

    private static final String STRING = "string";

    private static final String FLOAT = "float";

    private static final String DOUBLE = "double";

    private static final String LONG = "long";

    private static final String BOOLEAN = "boolean";

    private static final String INT = "int";

    private static final String WEIGHT = "weight";

    public GraphMLExporter( G graph, Writer writer, String name )
    {
        super( graph, writer, name );
    }

    @Override
    protected void internalExport()
        throws Exception
    {
        // scan graph and print TYPES description for vertex AND edges' properties
        Map<String, String> edgeKeyTypes = new HashMap<String, String>();

        for ( E edge : getGraph().getEdges() )
        {
            if ( edge instanceof Labeled )
            {
                if ( !edgeKeyTypes.containsKey( LABEL ) )
                {
                    edgeKeyTypes.put( LABEL, getStringType( ( (Labeled) edge ).getLabel() ) );
                }
            }

            if ( edge instanceof Weighted )
            {
                if ( !edgeKeyTypes.containsKey( WEIGHT ) )
                {
                    edgeKeyTypes.put( WEIGHT, getStringType( ( (Weighted<?>) edge ).getWeight() ) );
                }
            }
        }

        // scan vertices
        Map<String, String> vertexKeyTypes = new HashMap<String, String>();

        for ( V vertex : getGraph().getVertices() )
        {
            if ( vertex instanceof Labeled )
            {
                if ( !vertexKeyTypes.containsKey( LABEL ) )
                {
                    vertexKeyTypes.put( LABEL, getStringType( ( (Labeled) vertex ).getLabel() ) );
                }
            }
        }

        XMLStreamWriter xmlWriter = XMLOutputFactory.newInstance().createXMLStreamWriter( getWriter() );

        // start document tokens
        xmlWriter.writeStartDocument();

        xmlWriter.writeComment( format( "Graph generated by Apache Commons Graph on %s", new Date() ) );

        xmlWriter.writeStartElement( GRAPHML );
        xmlWriter.writeAttribute( XMLNS, GRAPHML_XMLNS );

        xmlWriter.writeStartElement( GRAPH );
        xmlWriter.writeAttribute( ID, getName() );
        xmlWriter.writeAttribute( EDGEDEFAULT, DIRECTED );

        // write EDGE key types
        for ( Map.Entry<String, String> entry : edgeKeyTypes.entrySet() )
        {
            xmlWriter.writeStartElement( KEY );
            xmlWriter.writeAttribute( ID, entry.getKey() );
            xmlWriter.writeAttribute( FOR, EDGE );
            xmlWriter.writeAttribute( ATTR_NAME, entry.getKey() );
            xmlWriter.writeAttribute( ATTR_TYPE, entry.getValue() );
            xmlWriter.writeEndElement();
        }

        // write VERTICES' key types
        for ( Map.Entry<String, String> entry : vertexKeyTypes.entrySet() )
        {
{'fraction_non_alphanumeric': 0.0917851350062017, 'fraction_numerical': 0.0007632859459975193, 'mean_word_length': 2.523361344537815, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 36}, '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': '4230390', 'n_tokens_mistral': 2820, 'n_tokens_neox': 2697, 'n_words': 917}