KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > bridge > SVGColorProfileElementBridge


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.bridge;
19
20 import java.awt.color.ICC_Profile JavaDoc;
21 import java.io.IOException JavaDoc;
22
23 import org.apache.batik.dom.svg.SVGOMDocument;
24 import org.apache.batik.dom.util.XLinkSupport;
25 import org.apache.batik.ext.awt.color.ICCColorSpaceExt;
26 import org.apache.batik.ext.awt.color.NamedProfileCache;
27 import org.apache.batik.util.ParsedURL;
28 import org.w3c.dom.Document JavaDoc;
29 import org.w3c.dom.Element JavaDoc;
30 import org.w3c.dom.Node JavaDoc;
31 import org.w3c.dom.NodeList JavaDoc;
32
33 /**
34  * This class bridges an SVG <tt>color-profile</tt> element with an
35  * <tt>ICC_ColorSpace</tt> object.
36  *
37  * @author <a HREF="mailto:vincent.hardy@eng.sun.com">Vincent Hardy</a>
38  * @version $Id: SVGColorProfileElementBridge.java,v 1.13 2004/08/18 07:12:33 vhardy Exp $ */

39 public class SVGColorProfileElementBridge extends AbstractSVGBridge
40     implements ErrorConstants {
41
42     /**
43      * Profile cache
44      */

45     public NamedProfileCache cache = new NamedProfileCache();
46
47     /**
48      * Returns 'colorProfile'.
49      */

50     public String JavaDoc getLocalName() {
51         return SVG_COLOR_PROFILE_TAG;
52     }
53
54     /**
55      * Creates an ICC_ColorSpace according to the specified parameters.
56      *
57      * @param ctx the bridge context to use
58      * @param paintedElement element on which the color is painted
59      * @param iccProfileName name of the profile that should be loaded
60      * that could be a color-profile element or an @color-profile
61      * CSS rule
62      */

63     public ICCColorSpaceExt createICCColorSpaceExt(BridgeContext ctx,
64                                                    Element JavaDoc paintedElement,
65                                                    String JavaDoc iccProfileName) {
66         // Check if there is one if the cache.
67
ICCColorSpaceExt cs = cache.request(iccProfileName.toLowerCase());
68         if (cs != null){
69             return cs;
70         }
71
72         // There was no cached copies for the profile. Load it now.
73
// Search for a color-profile element with specific name
74
Document doc = paintedElement.getOwnerDocument();
75         NodeList JavaDoc list = doc.getElementsByTagNameNS(SVG_NAMESPACE_URI,
76                                                    SVG_COLOR_PROFILE_TAG);
77
78         int n = list.getLength();
79         Element JavaDoc profile = null;
80         for(int i=0; i<n; i++){
81             Node JavaDoc node = list.item(i);
82             if(node.getNodeType() == Node.ELEMENT_NODE){
83                 Element JavaDoc profileNode = (Element JavaDoc)node;
84                 String JavaDoc nameAttr
85                     = profileNode.getAttributeNS(null, SVG_NAME_ATTRIBUTE);
86
87                 if(iccProfileName.equalsIgnoreCase(nameAttr)){
88                     profile = profileNode;
89                 }
90             }
91         }
92
93         if(profile == null)
94             return null;
95
96         // Now that we have a profile element,
97
// try to load the corresponding ICC profile xlink:href
98
String JavaDoc href = XLinkSupport.getXLinkHref(profile);
99         ICC_Profile JavaDoc p = null;
100         if (href != null) {
101             String JavaDoc baseURI= ((SVGOMDocument)doc).getURL();
102             ParsedURL purl = new ParsedURL(baseURI, href);
103             if (!purl.complete())
104                 throw new BridgeException(paintedElement, ERR_URI_MALFORMED,
105                                           new Object JavaDoc[] {href});
106             try{
107                 ParsedURL pDocURL = null;
108                 if (baseURI != null) {
109                     pDocURL = new ParsedURL(baseURI);
110                 }
111
112                ctx.getUserAgent().checkLoadExternalResource(purl,
113                                                             pDocURL);
114
115                 p = ICC_Profile.getInstance(purl.openStream());
116             } catch(IOException JavaDoc e) {
117                 throw new BridgeException(paintedElement, ERR_URI_IO,
118                                           new Object JavaDoc[] {href});
119                 // ??? IS THAT AN ERROR FOR THE SVG SPEC ???
120
} catch(SecurityException JavaDoc e) {
121                 throw new BridgeException(paintedElement, ERR_URI_UNSECURE,
122                                           new Object JavaDoc[] {href});
123             }
124         }
125         if (p == null) {
126             return null;
127         }
128
129         // Extract the rendering intent from profile element
130
int intent = convertIntent(profile);
131         cs = new ICCColorSpaceExt(p, intent);
132
133         // Add profile to cache
134
cache.put(iccProfileName.toLowerCase(), cs);
135         return cs;
136     }
137
138     private static int convertIntent(Element JavaDoc profile) {
139
140         String JavaDoc intent
141             = profile.getAttributeNS(null, SVG_RENDERING_INTENT_ATTRIBUTE);
142
143         if (intent.length() == 0) {
144             return ICCColorSpaceExt.AUTO;
145         }
146         if (SVG_PERCEPTUAL_VALUE.equals(intent)) {
147             return ICCColorSpaceExt.PERCEPTUAL;
148         }
149         if (SVG_AUTO_VALUE.equals(intent)) {
150             return ICCColorSpaceExt.AUTO;
151         }
152         if (SVG_RELATIVE_COLORIMETRIC_VALUE.equals(intent)) {
153             return ICCColorSpaceExt.RELATIVE_COLORIMETRIC;
154         }
155         if (SVG_ABSOLUTE_COLORIMETRIC_VALUE.equals(intent)) {
156             return ICCColorSpaceExt.ABSOLUTE_COLORIMETRIC;
157         }
158         if (SVG_SATURATION_VALUE.equals(intent)) {
159             return ICCColorSpaceExt.SATURATION;
160         }
161         throw new BridgeException
162             (profile, ERR_ATTRIBUTE_VALUE_MALFORMED,
163              new Object JavaDoc[] {SVG_RENDERING_INTENT_ATTRIBUTE, intent});
164     }
165 }
166
Popular Tags