Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/apache/batik/apps/rasterizer/SVGConverterURLSource.java


1   /*
2   
3      Copyright 1999-2003  The Apache Software Foundation 
4   
5      Licensed under the Apache License, Version 2.0 (the "License");
6      you may not use this file except in compliance with the License.
7      You may obtain a copy of the License at
8   
9          http://www.apache.org/licenses/LICENSE-2.0
10  
11     Unless required by applicable law or agreed to in writing, software
12     distributed under the License is distributed on an "AS IS" BASIS,
13     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14     See the License for the specific language governing permissions and
15     limitations under the License.
16  
17  */
18  
19  package org.apache.batik.apps.rasterizer;
20  
21  import java.io.IOException;
22  import java.io.InputStream;
23  
24  import org.apache.batik.util.ParsedURL;
25  
26  /*
27   * @author <a href="mailto:vhardy@apache.org">Vincent Hardy</a>
28   * @version $Id: SVGConverterURLSource.java,v 1.6 2004/10/30 18:38:04 deweese Exp $
29   */
30  public class SVGConverterURLSource implements SVGConverterSource {
31      /** 
32       * SVG file extension 
33       */
34      protected static final String SVG_EXTENSION = ".svg";
35      protected static final String SVGZ_EXTENSION = ".svgz";
36  
37      //
38      // Reported when the URL for one of the sources is
39      // invalid. This will happen if the URL is malformed or
40      // if the URL file does not end with the ".svg" extension.
41      // This is needed to be able to create a file name for
42      // the ouptut automatically.
43      //
44      public static final String ERROR_INVALID_URL
45          = "SVGConverterURLSource.error.invalid.url";
46  
47      ParsedURL purl;
48      String name;
49  
50      public SVGConverterURLSource(String url) throws SVGConverterException{
51          this.purl = new ParsedURL(url);
52  
53          // Get the path portion
54          String path = this.purl.getPath();
55          if (path == null || 
56              !(path.toLowerCase().endsWith(SVG_EXTENSION) ||
57                path.toLowerCase().endsWith(SVGZ_EXTENSION))){
58              throw new SVGConverterException(ERROR_INVALID_URL,
59                                              new Object[]{url});
60          }
61  
62          int n = path.lastIndexOf("/");
63          if (n != -1){
64              // The following is safe because we know there is at least ".svg"
65              // after the slash.
66              path = path.substring(n+1);
67          }
68              
69          name = path;
70  
71          //
72          // The following will force creation of different output file names
73          // for urls with references (e.g., anne.svg#svgView(viewBox(0,0,4,5)))
74          //
75          String ref = this.purl.getRef();
76          if (ref != null && (ref.length()!=0)) {
77              name += "" + ref.hashCode();
78          }
79      }
80  
81      public String toString(){
82          return purl.toString();
83      }
84  
85      public String getURI(){
86          return toString();
87      }
88  
89      public boolean equals(Object o){
90          if (o == null || !(o instanceof SVGConverterURLSource)){
91              return false;
92          }
93  
94          return purl.equals(((SVGConverterURLSource)o).purl);
95      }
96  
97      public InputStream openStream() throws IOException {
98          return purl.openStream();
99      }
100 
101     public boolean isSameAs(String srcStr){
102         return toString().equals(srcStr);
103     }
104 
105     public boolean isReadable(){
106         return true;
107     }
108 
109     public String getName(){
110         return name;
111     }
112 }