1 package org.apache.maven.doxia.module.fo;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.io.IOException;
24 import java.util.List;
25
26 import javax.swing.text.MutableAttributeSet;
27 import javax.swing.text.SimpleAttributeSet;
28
29 import org.apache.commons.configuration.ConfigurationException;
30 import org.apache.commons.configuration.XMLConfiguration;
31 import org.apache.maven.doxia.sink.impl.SinkUtils;
32 import org.codehaus.plexus.util.ReaderFactory;
33
34
35
36
37
38
39
40
41 public class FoConfiguration
42 {
43
44 private MutableAttributeSet attributeSet;
45
46
47 private final XMLConfiguration config;
48
49
50 private List<?> sets;
51
52
53
54
55 public FoConfiguration()
56 {
57 this.config = new XMLConfiguration();
58
59
60 config.setDelimiterParsingDisabled( true );
61
62 loadDefaultConfig();
63 }
64
65
66
67
68
69
70
71
72
73
74
75 public void load( File configFile )
76 throws IOException
77 {
78 config.clear();
79
80 try
81 {
82 config.load( configFile );
83 }
84 catch ( ConfigurationException cex )
85 {
86 IOException ioe = new IOException();
87 ioe.initCause( cex );
88 throw ioe;
89 }
90
91 loadDefaultConfig();
92 }
93
94
95
96
97
98
99
100
101
102
103
104
105 public String getAttributeString( String attributeId )
106 {
107 if ( attributeId == null )
108 {
109 return "";
110 }
111
112 reset();
113 addAttributes( attributeId );
114
115 return SinkUtils.getAttributeString( attributeSet );
116 }
117
118
119
120
121
122
123
124
125
126
127
128 public MutableAttributeSet getAttributeSet( String attributeId )
129 {
130 if ( attributeId == null || attributeId.length() == 0 )
131 {
132 return null;
133 }
134
135 reset();
136 addAttributes( attributeId );
137
138 if ( attributeSet.getAttributeCount() == 0 )
139 {
140 return null;
141 }
142
143 return attributeSet;
144 }
145
146
147
148
149
150
151
152
153 private void addAttributes( String attributeId )
154 {
155 int index = sets.indexOf( attributeId );
156 String keybase = "xsl:attribute-set(" + String.valueOf( index ) + ")";
157
158 Object prop = config.getProperty( keybase + ".xsl:attribute" );
159
160 if ( prop instanceof List<?> )
161 {
162 List<?> values = (List<?>) prop;
163 List<?> keys = config.getList( keybase + ".xsl:attribute[@name]" );
164
165 for ( int i = 0; i < values.size(); i++ )
166 {
167 attributeSet.addAttribute( keys.get( i ), values.get( i ) );
168 }
169 }
170 else if ( prop instanceof String )
171 {
172 String value = config.getString( keybase + ".xsl:attribute" );
173 String key = config.getString( keybase + ".xsl:attribute[@name]" );
174 attributeSet.addAttribute( key, value );
175 }
176
177 String extend = config.getString( keybase + "[@use-attribute-sets]" );
178
179 if ( extend != null )
180 {
181 addAttributes( extend );
182 }
183 }
184
185
186 private void loadDefaultConfig()
187 {
188 try
189 {
190 config.load( ReaderFactory.newXmlReader( getClass().getResourceAsStream( "/fo-styles.xslt" ) ) );
191 }
192 catch ( ConfigurationException cex )
193 {
194
195 throw new RuntimeException( cex );
196 }
197 catch ( IOException e )
198 {
199
200 throw new RuntimeException( e );
201 }
202
203 this.sets = config.getList( "xsl:attribute-set[@name]" );
204 reset();
205 }
206
207
208
209
210 private void reset()
211 {
212 this.attributeSet = new SimpleAttributeSet();
213 }
214
215 }