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