JAL-2360 fix applet build by separating castor XML dependency from jalview.schemes...
[jalview.git] / src / jalview / schemes / ColourSchemeLoader.java
1 package jalview.schemes;
2
3 import jalview.binding.JalviewUserColours;
4
5 import java.awt.Color;
6 import java.io.File;
7 import java.io.FileInputStream;
8 import java.io.InputStreamReader;
9
10 import org.exolab.castor.xml.Unmarshaller;
11
12 public class ColourSchemeLoader
13 {
14
15   /**
16    * Loads a user defined colour scheme from file. The file should contain a
17    * definition of residue colours in XML format as defined in
18    * JalviewUserColours.xsd.
19    * 
20    * @param filePath
21    * 
22    * @return
23    */
24   public static UserColourScheme loadColourScheme(String filePath)
25   {
26     UserColourScheme ucs = null;
27     Color[] newColours = null;
28     File file = new File(filePath);
29     try
30     {
31       InputStreamReader in = new InputStreamReader(
32               new FileInputStream(file), "UTF-8");
33   
34       jalview.schemabinding.version2.JalviewUserColours jucs = new jalview.schemabinding.version2.JalviewUserColours();
35   
36       org.exolab.castor.xml.Unmarshaller unmar = new org.exolab.castor.xml.Unmarshaller(
37               jucs);
38       jucs = (jalview.schemabinding.version2.JalviewUserColours) unmar
39               .unmarshal(in);
40   
41       /*
42        * non-case-sensitive colours are for 20 amino acid codes,
43        * B, Z, X and Gap
44        * optionally, lower-case alternatives for all except Gap
45        */
46       newColours = new Color[24];
47       Color[] lowerCase = new Color[23];
48       boolean caseSensitive = false;
49   
50       String name;
51       int index;
52       for (int i = 0; i < jucs.getColourCount(); i++)
53       {
54         name = jucs.getColour(i).getName();
55         if (ResidueProperties.aa3Hash.containsKey(name))
56         {
57           index = ResidueProperties.aa3Hash.get(name).intValue();
58         }
59         else
60         {
61           index = ResidueProperties.aaIndex[name.charAt(0)];
62         }
63         if (index == -1)
64         {
65           continue;
66         }
67   
68         Color color = new Color(Integer.parseInt(jucs.getColour(i)
69                 .getRGB(), 16));
70         if (name.toLowerCase().equals(name))
71         {
72           caseSensitive = true;
73           lowerCase[index] = color;
74         }
75         else
76         {
77           newColours[index] = color;
78         }
79       }
80   
81       /*
82        * instantiate the colour scheme
83        */
84       ucs = new UserColourScheme(newColours);
85       ucs.setName(jucs.getSchemeName());
86       if (caseSensitive)
87       {
88         ucs.setLowerCaseColours(lowerCase);
89       }
90     } catch (Exception ex)
91     {
92       // Could be old Jalview Archive format
93       try
94       {
95         InputStreamReader in = new InputStreamReader(new FileInputStream(
96                 file), "UTF-8");
97   
98         jalview.binding.JalviewUserColours jucs = new jalview.binding.JalviewUserColours();
99   
100         jucs = JalviewUserColours.unmarshal(in);
101   
102         newColours = new Color[jucs.getColourCount()];
103   
104         for (int i = 0; i < 24; i++)
105         {
106           newColours[i] = new Color(Integer.parseInt(jucs.getColour(i)
107                   .getRGB(), 16));
108         }
109         ucs = new UserColourScheme(newColours);
110         ucs.setName(jucs.getSchemeName());
111       } catch (Exception ex2)
112       {
113         ex2.printStackTrace();
114       }
115   
116       if (newColours == null)
117       {
118         System.out.println("Error loading User ColourFile\n" + ex);
119       }
120     }
121   
122     return ucs;
123   }
124
125 }