<commit_before>/*
 * $Id$
 *
 * Copyright (c) 2008 by <NAME>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License (LGPL) as published by the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, copies are available
 * at http://www.opensource.org.
 */

package VASSAL.tools.image.svg;

import java.awt.Dimension;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import org.apache.batik.dom.GenericDOMImplementation;
import org.apache.batik.dom.svg.SAXSVGDocumentFactory;
import org.apache.batik.dom.util.DOMUtilities;
import org.apache.batik.dom.util.SAXDocumentFactory;
import org.apache.batik.dom.util.XLinkSupport;
import org.apache.batik.dom.util.XMLSupport;
import org.apache.batik.util.XMLResourceDescriptor;

import org.w3c.dom.Document;
import org.w3c.dom.DOMException;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import VASSAL.tools.image.ImageIOException;
import VASSAL.tools.image.ImageNotFoundException;
import VASSAL.tools.io.IOUtils;

/**
 * Utility methods for manipulating SVG images.
 *
 * @author <NAME>
 * @since 3.1.0
 */
public class SVGImageUtils {
  protected static final SAXSVGDocumentFactory factory =
    new SAXSVGDocumentFactory(XMLResourceDescriptor.getXMLParserClassName());

  private SVGImageUtils() { }

  /**
   * Returns the default dimensions of the SVG image.
   *
   * @return the image dimensions
   * @throws IOException if the image cannot be read
   */
  public static Dimension getImageSize(InputStream in) throws IOException {
    return getImageSize("", in); 
  }

  /**
   * Returns the default dimensions of the SVG image.
   *
   * @return the image dimensions
   * @throws IOException if the image cannot be read
   */
  public static Dimension getImageSize(String name, InputStream in)
                                                          throws IOException {
    // get the SVG
    final Document doc;
    try {
      doc = factory.createDocument(null, in);
      in.close();
    }
    catch (DOMException e) {
      throw new ImageIOException(name, e);
    }
    catch (FileNotFoundException e) {
      throw new ImageNotFoundException(name, e);
    }
    catch (IOException e) {
      throw new ImageIOException(name, e);
    }
    finally {
      IOUtils.closeQuietly(in);
    }

    // get the default image width and height
    final Element root = doc.getDocumentElement();
    try {
      final int width = (int) (Float.parseFloat(
        root.getAttributeNS(null, "width").replaceFirst("px", ""))+0.5);
      final int height = (int) (Float.parseFloat(
        root.getAttributeNS(null, "height").replaceFirst("px", ""))+0.5);

      return new Dimension(width, height);
    }
    catch (NumberFormatException e) {
      throw new ImageIOException(name, e);
    }
  }

  /**
   * Conducts a recursive depth-first search for external references
   * in the given SVG file.
   *
   * @param path the path of the file to check for external references
   */
  public static List<String> getExternalReferences(String path)
                                                           throws IOException {
    final ArrayList<String> reflist = new ArrayList<String>();
    reflist.add(path);
    return getExternalReferences(path, reflist);
  }

  /**
   * Conducts a recursive depth-first search for external references
   * in the SVG file named by path. This is a helper function for
   * {@link #getExternalReferences}.
   *
   * @param path the path of the file to check for external references
   * @param known the list of references already found
   */
  protected static List<String> getExternalReferences(
                          String path, List<String> known) throws IOException {

    final HashSet<String> follow = new HashSet<String>();
    final URL here = new URL("file", null, new File(path).getCanonicalPath());

    Document doc = null;
    try {
      doc = factory.createDocument(here.toString());
    }
    catch (DOMException e) {
      throw (IOException) new IOException().initCause(e);
    }

    final NodeList usenodes = doc.getElementsByTagName("use");
    for (int i = 0; i < usenodes.getLength(); ++i) {
      final Element e = (Element) usenodes.item(i);
      final URL url = new URL(new URL(e.getBaseURI()),
                              XLinkSupport.getXLinkHref(e));
      // balk (for now) unless file is available on our filesystem
      if (url.getProtocol().equals("file")) {
        final String refpath = url.getPath();
        if (!known.contains(refpath)) {
          follow.add(refpath);
          known.add(refpath);
        }
      }
      else {
        throw new IOException("unsupported protocol '" +
                              url.getProtocol() + "' in xlink:href");
      }
    }<commit_msg>Fixed Bug 3745:  NPE in SVGImageUtils.getImageSize in Batik<commit_after>/*
 * $Id$
 *
 * Copyright (c) 2008 by <NAME>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License (LGPL) as published by the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, copies are available
 * at http://www.opensource.org.
 */

package VASSAL.tools.image.svg;

import java.awt.Dimension;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import org.apache.batik.dom.GenericDOMImplementation;
import org.apache.batik.dom.svg.SAXSVGDocumentFactory;
import org.apache.batik.dom.util.DOMUtilities;
import org.apache.batik.dom.util.SAXDocumentFactory;
import org.apache.batik.dom.util.XLinkSupport;
import org.apache.batik.dom.util.XMLSupport;
import org.apache.batik.util.XMLResourceDescriptor;

import org.w3c.dom.Document;
import org.w3c.dom.DOMException;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import VASSAL.tools.image.ImageIOException;
import VASSAL.tools.image.ImageNotFoundException;
import VASSAL.tools.io.IOUtils;

/**
 * Utility methods for manipulating SVG images.
 *
 * @author <NAME>
 * @since 3.1.0
 */
public class SVGImageUtils {
  // NB: SAXSVGDocumentFactory isn't thread-safe, we have to synchronize on it.
  protected static final SAXSVGDocumentFactory factory =
    new SAXSVGDocumentFactory(XMLResourceDescriptor.getXMLParserClassName());

  private SVGImageUtils() { }

  /**
   * Returns the default dimensions of the SVG image.
   *
   * @return the image dimensions
   * @throws IOException if the image cannot be read
   */
  public static Dimension getImageSize(InputStream in) throws IOException {
    return getImageSize("", in); 
  }

  /**
   * Returns the default dimensions of the SVG image.
   *
   * @return the image dimensions
   * @throws IOException if the image cannot be read
   */
  public static Dimension getImageSize(String name, InputStream in)
                                                          throws IOException {
    // get the SVG
    final Document doc;
    try {
      synchronized (factory) {
        doc = factory.createDocument(null, in);
      }
      in.close();
    }
    catch (DOMException e) {
      throw new ImageIOException(name, e);
    }
    catch (FileNotFoundException e) {
      throw new ImageNotFoundException(name, e);
    }
    catch (IOException e) {
      throw new ImageIOException(name, e);
    }
    finally {
      IOUtils.closeQuietly(in);
    }

    // get the default image width and height
    final Element root = doc.getDocumentElement();
    try {
      final int width = (int) (Float.parseFloat(
        root.getAttributeNS(null, "width").replaceFirst("px", ""))+0.5);
      final int height = (int) (Float.parseFloat(
        root.getAttributeNS(null, "height").replaceFirst("px", ""))+0.5);

      return new Dimension(width, height);
    }
    catch (NumberFormatException e) {
      throw new ImageIOException(name, e);
    }
  }

  /**
   * Conducts a recursive depth-first search for external references
   * in the given SVG file.
   *
   * @param path the path of the file to check for external references
   */
  public static List<String> getExternalReferences(String path)
                                                           throws IOException {
    final ArrayList<String> reflist = new ArrayList<String>();
    reflist.add(path);
    return getExternalReferences(path, reflist);
  }

  /**
   * Conducts a recursive depth-first search for external references
   * in the SVG file named by path. This is a helper function for
   * {@link #getExternalReferences}.
   *
   * @param path the path of the file to check for external references
   * @param known the list of references already found
   */
  protected static List<String> getExternalReferences(
                          String path, List<String> known) throws IOException {

    final HashSet<String> follow = new HashSet<String>();
    final URL here = new URL("file", null, new File(path).getCanonicalPath());

    Document doc = null;
    try {
      synchronized (factory) {
        doc = factory.createDocument(here.toString());
      }
    }
    catch (DOMException e) {
      throw (IOException) new IOException().initCause(e);
    }

    final NodeList usenodes = doc.getElementsByTagName("use");
    for (int i = 0; i < usenodes.getLength(); ++i) {
      final Element e = (Element) usenodes.item(i);
      final URL url = new URL(new URL(e.getBaseURI()),
                              XLinkSupport.getXLinkHref(e));
      // balk (for now) unless file is available on our filesystem
      if (url.getProtocol().equals("file")) {
        final String refpath = url.getPath();
        if (!known.contains(refpath)) {
          follow.add(refpath);
          known.add(refpath);
        }
      }
      else {
        throw new IOException("unsupported protocol '" +
                              url.getProtocol() + "' in xlink:href");
      }
    }
{'fraction_non_alphanumeric': 0.09920275412212358, 'fraction_numerical': 0.0034426526544663887, 'mean_word_length': 2.8993288590604025, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5517571', 'n_tokens_mistral': 3140, 'n_tokens_neox': 3095, 'n_words': 983}