summaryrefslogtreecommitdiffstats
path: root/src/main/java/com/kitfox/salamander/parser/SVGParser.java
blob: d588b08d5f3b7f61ccb263366747a7fa22f1e2d7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
 * SVGParser.java
 *
 * Created on April 11, 2007, 5:07 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.kitfox.salamander.parser;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;

/**
 *
 * @author kitfox
 */
public class SVGParser
{
    public static interface SVGParserListener
    {
        
    }
    
    /** Creates a new instance of SVGParser */
    public SVGParser()
    {
    }

    /**
     * Parse an SVG document.  The document may be either uncompressed XML (.svg) 
     * or a zipped document (.svgz).  This routine will automatically detect
     * zipped documents and unzip them.
     */
    public static void parse(File source, SVGParserListener listener)
    {
        try
        {
            XMLReader parser;
            parser = XMLReaderFactory.createXMLReader();
            SVGParserHandler handler = new SVGParserHandler(listener);
            parser.setContentHandler(handler);

            FileInputStream fis = new FileInputStream(source);
            BufferedInputStream bis = new BufferedInputStream(fis);
            bis.mark(4);
            //Check for gzip magic number
            DataInputStream din = new DataInputStream(bis);
            long magicNumber = din.readLong();
            bis.reset();
            
            InputStream svgStream;
            if ((int)magicNumber == 0x4b50)
            {
                //PK Zip file
                ZipInputStream zin = new ZipInputStream(bis);
                ZipEntry entry = zin.getNextEntry();
                byte[] buf = new byte[(int)entry.getSize()];
                for (int offset = 0; offset < buf.length; offset += zin.read(buf, offset, buf.length - offset));
                zin.closeEntry();
                zin.close();
                
                svgStream = new ByteArrayInputStream(buf);
            }
            else
            {
                //Treat input as uncompressed XML
                svgStream = bis;
            }
            
            InputSource is = new InputSource(svgStream);
            parser.parse(is);

        }
        catch (SAXException ex)
        {
            ex.printStackTrace();
        }
        catch (IOException ex)
        {
            ex.printStackTrace();
        }
    }
}