KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > test > svg > SVGReferenceRenderingAccuracyTest


1 /*
2
3    Copyright 2001-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 package org.apache.batik.test.svg;
19
20 import java.io.File JavaDoc;
21 import java.net.MalformedURLException JavaDoc;
22 import java.net.URL JavaDoc;
23
24 /**
25  * Base class for tests which take an additional parameter in addition
26  * to the SVG file.
27  *
28  * @author <a HREF="mailto:vincent.hardy@sun.com">Vincent Hardy</a>
29  * @version $Id: SVGReferenceRenderingAccuracyTest.java,v 1.6 2004/08/18 07:17:03 vhardy Exp $
30  */

31 public class SVGReferenceRenderingAccuracyTest
32     extends ParametrizedRenderingAccuracyTest {
33     protected String JavaDoc alias;
34    
35     /**
36      * For this type of test, the id should be made as
37      * follows:<br />
38      * <fileName>#reference-alias <br />
39      * For example: <br />
40      * samples/anne.svg#svgView(viewBox(0,0,100,100))-viewBox1
41      */

42     public void setId(String JavaDoc id){
43         this.id = id;
44
45         String JavaDoc svgFile = id;
46
47         int n = svgFile.lastIndexOf('#');
48         if(n == -1 || n+1 >= svgFile.length() ){
49             throw new IllegalArgumentException JavaDoc(id);
50         }
51         
52         parameter = svgFile.substring(n+1, svgFile.length());
53         svgFile = svgFile.substring(0, n);
54         
55         n = parameter.lastIndexOf('-');
56         if(n == -1 || n+1 >= parameter.length()){
57             throw new IllegalArgumentException JavaDoc(id);
58         }
59
60         alias = parameter.substring(n+1, parameter.length());
61         parameter = parameter.substring(0, n);
62
63         String JavaDoc[] dirNfile = breakSVGFile(svgFile);
64
65         setConfig(buildSVGURL(dirNfile[0], dirNfile[1]),
66                   buildRefImgURL(dirNfile[0], dirNfile[1]));
67
68         setVariationURL(buildVariationURL(dirNfile[0], dirNfile[1]));
69         setSaveVariation(new File JavaDoc(buildSaveVariationFile(dirNfile[0], dirNfile[1])));
70         setCandidateReference(new File JavaDoc(buildCandidateReferenceFile(dirNfile[0], dirNfile[1])));
71     }
72
73     /**
74      * Resolves the input string as follows.
75      *
76      * + First, the string is interpreted as a file description minus
77      * any url fragment it may have (stuff after a '#'). If the
78      * file's parent directory exists, then the file name is turned
79      * into a URL and the fragment if any is appended.
80      * + Otherwise, the string is supposed to be a URL. If it
81      * is an invalid URL, an IllegalArgumentException is thrown.
82      */

83     protected URL JavaDoc resolveURL(String JavaDoc url){
84         // We must strip the # off if there is one otherwise File thinks
85
// we want to reference a file that has a '#' in it's name...
86
String JavaDoc fragment = null;
87         String JavaDoc file = url;
88         int n = file.lastIndexOf('#');
89         if (n != -1) {
90             fragment = file.substring(n); // include the #.
91
file = file.substring(0,n);
92         }
93
94         // Is url a file?
95
File JavaDoc f = (new File JavaDoc(file)).getAbsoluteFile();
96         if(f.getParentFile().exists()){
97             try{
98                 if (fragment == null) {
99                     return f.toURL(); // No fragment.
100
} else {
101                     // Construct URL that includes fragment...
102
return new URL JavaDoc(f.toURL(), fragment);
103                 }
104             }catch(MalformedURLException JavaDoc e){
105                 throw new IllegalArgumentException JavaDoc();
106             }
107         }
108         
109         // url is not a file. It must be a regular URL...
110
try{
111             return new URL JavaDoc(url);
112         }catch(MalformedURLException JavaDoc e){
113             throw new IllegalArgumentException JavaDoc(url);
114         }
115     }
116
117     /**
118      * Gives a chance to the subclass to prepend a prefix to the
119      * svgFile name.
120      * The svgURL is built as:
121      * getSVGURLPrefix() + svgDir + svgFile + SVG_EXTENSION + "#" + parameter
122      */

123     protected String JavaDoc buildSVGURL(String JavaDoc svgDir, String JavaDoc svgFile){
124         return getSVGURLPrefix() + svgDir +
125             svgFile + SVG_EXTENSION + "#" + parameter;
126     }
127
128     /**
129      * Gives a chance to the subclass to control the construction
130      * of the reference PNG file from the svgFile name
131      * The refImgURL is built as:
132      * getRefImagePrefix() + svgDir + getRefImageSuffix() + svgFile
133      */

134     protected String JavaDoc buildRefImgURL(String JavaDoc svgDir, String JavaDoc svgFile){
135         return getRefImagePrefix() + svgDir + getRefImageSuffix() + svgFile + alias + PNG_EXTENSION;
136     }
137
138     /**
139      * Gives a chance to the subclass to control the construction
140      * of the variation URL, which is built as:
141      * getVariationPrefix() + svgDir + getVariationSuffix() + svgFile + parameter + PNG_EXTENSION
142      */

143     public String JavaDoc buildVariationURL(String JavaDoc svgDir, String JavaDoc svgFile){
144         return getVariationPrefix() + svgDir + getVariationSuffix() + svgFile + alias + PNG_EXTENSION;
145     }
146
147     /**
148      * Gives a chance to the subclass to control the construction
149      * of the saveVariation URL, which is built as:
150      * getSaveVariationPrefix() + svgDir + getSaveVariationSuffix() + svgFile + parameter + PNG_EXTENSION
151      */

152     public String JavaDoc buildSaveVariationFile(String JavaDoc svgDir, String JavaDoc svgFile){
153         return getSaveVariationPrefix() + svgDir + getSaveVariationSuffix() + svgFile + alias + PNG_EXTENSION;
154     }
155
156     /**
157      * Gives a chance to the subclass to control the construction
158      * of the candidateReference URL, which is built as:
159      * getSaveVariationPrefix() + svgDir + getSaveVariationSuffix() + svgFile + parameter + PNG_EXTENSION
160      */

161     public String JavaDoc buildCandidateReferenceFile(String JavaDoc svgDir, String JavaDoc svgFile){
162         return getCandidateReferencePrefix() + svgDir + getCandidateReferenceSuffix() + svgFile + alias + PNG_EXTENSION;
163     }
164
165 }
166
Popular Tags