561177b7f88493d9ced3ca82db9d5387f23f6e56
[jalview.git] / src / org / biojava / dasobert / das / DAS_Stylesheet_Handler.java
1 /*
2  *                  BioJava development code
3  *
4  * This code may be freely distributed and modified under the
5  * terms of the GNU Lesser General Public Licence.  This should
6  * be distributed with the code.  If you do not have a copy,
7  * see:
8  *
9  *      http://www.gnu.org/copyleft/lesser.html
10  *
11  * Copyright for this code is held jointly by the individual
12  * authors.  These should be listed in @author doc comments.
13  *
14  * For more information on the BioJava project and its aims,
15  * or to join the biojava-l mailing list, visit the home page
16  * at:
17  *
18  *      http://www.biojava.org/
19  * 
20  * Created on Aug 3, 2005
21  *
22  */
23 package org.biojava.dasobert.das;
24
25
26 import org.xml.sax.helpers.DefaultHandler;
27 import org.xml.sax.Attributes            ;
28 import java.util.*;
29 import java.util.logging.Logger;
30 import java.awt.Color;
31
32 /** a class to parse the XML response of a DAS - stylesheet request.
33  * @author Andreas Prlic
34  *
35  */
36 public class DAS_Stylesheet_Handler extends DefaultHandler {
37     
38     List typeGlyphMaps;
39     Map  currentType;
40     String chars ;
41     boolean threeDstyle;
42     List threeDGlyphMaps;
43     static Logger logger      = Logger.getLogger("org.biojava.spice");
44     
45     /**
46      * 
47      */
48     public DAS_Stylesheet_Handler() {
49         super();
50         typeGlyphMaps = new ArrayList();
51         currentType = new HashMap();
52         threeDGlyphMaps = new ArrayList();
53         threeDstyle = false;
54     }
55     
56     
57     public Map[] getTypeStyles(){
58         return (Map[]) typeGlyphMaps.toArray(new Map[typeGlyphMaps.size()]);
59     }
60     
61     public Map[] get3DStyles(){
62         return (Map[]) threeDGlyphMaps.toArray(new Map[threeDGlyphMaps.size()]);
63     }
64     
65     public void startElement (String uri, String name, String qName, Attributes atts){
66         chars = "";
67         
68         if ( qName.equals("CATEGORY")){
69             String id = atts.getValue("id");
70             if ( id.equals("3D")){
71                 // here follow the 3D styles...
72                 threeDstyle = true;
73             } else {
74                 threeDstyle = false;
75                 
76             }
77         }
78         
79         if ( qName.equals("TYPE")){
80             // this glyph matches to features of type >id<.
81             String id = atts.getValue("id");
82             currentType = new HashMap(); 
83             currentType.put("type",id);
84         } 
85         
86         else if ( qName.equals("ARROW")){
87             currentType.put("style","arrow");
88         } else if ( qName.equals("ANCHORED_ARROW")){
89             currentType.put("style","anchored_arrow");
90         } else if ( qName.equals("BOX")){
91             currentType.put("style","box");
92         } else if ( qName.equals("CROSS")){
93             currentType.put("style","cross");
94         } else if ( qName.equals("EX")){
95             currentType.put("style","EX");
96         } else if ( qName.equals("HELIX")){
97             currentType.put("style","helix");
98         } else if ( qName.equals("LINE")){
99             currentType.put("style","LINE");
100         }  else if ( qName.equals("SPAN")){
101             currentType.put("style","span");
102         } else if ( qName.equals("TRIANGLE")){
103             currentType.put("style","triangle");
104         }
105         
106     }
107     
108     /**  convert the color provided by the stylesheet to a java Color 
109      * 
110      * @param chars
111      * @return
112      */
113     private Color getColorFromString(String chars){
114         
115         
116         if (chars.equals("rotate")) {
117             // use the default SPICE colors ...
118             return null;
119         }
120         
121         try {
122             Color col = Color.decode(chars);
123             return col;
124         } catch ( Exception e) {
125             logger.finest("could not decode color from stylesheet " + e.getMessage());
126         }
127         
128         
129         // map the string to a build in color...
130         // thanks to the Xpm.java script provided by Phil Brown (under LGPL)
131         // AP downloaded it from http://www.bolthole.com/java/Xpm.java
132         
133         // the DAS spec stylesheet only requires 16 VGA colors to be supported, but here we do more... :-)
134         
135         int[] rgb = Xpm.NameToRGB3(chars);
136         if ( rgb != null) {
137             Color col = new Color(rgb[0],rgb[1],rgb[2]);
138             return col;
139         }
140         return null ;
141     }
142     
143     public void endElement(String uri, String name, String qName) {
144         if ( qName.equals("HEIGHT")){
145             currentType.put("height",chars);
146         } else if ( qName.equals("COLOR")){
147             //System.out.println("got color " + chars);
148             Color col = getColorFromString(chars);
149             if ( col != null ){
150                 currentType.put("color",col);
151             } else {
152                 if ( chars.equals("cpk")){
153                     currentType.put("cpkcolor","true");
154                 }
155             }
156             
157         } else if ( qName.equals("OUTLINECOLOR")){
158             currentType.put("outlinecolor",chars);
159         } else if ( qName.equals("BACKGROUND")){
160             currentType.put("background",chars);
161         } else if ( qName.equals("BUMP")){
162             if ( chars.equals("no"))
163                 currentType.put("bump","no");
164             else 
165                 currentType.put("bump","yes");
166         
167             // 3D stuff
168         }  else if ( qName.equals("WIREFRAME")){
169             currentType.put("display","wireframe");
170         } else if ( qName.equals("SPACEFILL")){
171             currentType.put("display","spacefill");
172         } else if ( qName.equals("BACKBONE")){
173             currentType.put("display","backbone");
174         } else if ( qName.equals("CARTOON")){
175             currentType.put("display","cartoon");
176         } else if ( qName.equals("RIBBONS")){
177             currentType.put("display","ribbons");
178         } else if ( qName.equals("WIDTH")){
179             currentType.put("width",chars);
180         }
181         
182         else if ( qName.equals("TYPE")){           
183             if ( threeDstyle){
184              threeDGlyphMaps.add(currentType);   
185             } else {
186                 typeGlyphMaps.add(currentType);
187             }
188         }
189     }
190     
191     public void characters (char ch[], int start, int length){
192         
193      
194             for (int i = start; i < start + length; i++) {
195                chars += ch[i];
196             }
197         
198         
199     }
200     
201 }
202
203
204
205