View Javadoc

1   package org.apache.maven.doxia.module.fo;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * A utility class to construct FO configuration parameters.
37   *
38   * @author ltheussl
39   * @version $Id: FoConfiguration.java 1091053 2011-04-11 12:55:07Z ltheussl $
40   * @since 1.1
41   */
42  public class FoConfiguration
43  {
44      /** Holds the single attributes. */
45      private MutableAttributeSet attributeSet;
46  
47      /** The configuration instance. */
48      private final XMLConfiguration config;
49  
50      /** The list of attribute sets. */
51      private List<?> sets;
52  
53      /**
54       * Constructor.
55       */
56      public FoConfiguration()
57      {
58          this.config = new XMLConfiguration();
59  
60          // necessary because some attributes contain commas:
61          config.setDelimiterParsingDisabled( true );
62  
63          loadDefaultConfig();
64      }
65  
66      /**
67       * Load configuration parameters from a File.
68       *
69       * @param configFile the configuration file.
70       *
71       * @throws java.io.IOException if the File cannot be read
72       *  or some error occurs when initializing the configuration parameters.
73       *
74       * @since 1.1.1
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(); // this adds default values that are missing from above
93      }
94  
95      /**
96       * Builds a list of attributes.
97       *
98       * @param attributeId A unique id to identify the set of attributes.
99       * This should correspond to the name of an attribute-set
100      * defined in the configuration file.
101      * @return A string that contains a list of attributes with
102      * the values configured for the current builder. Returns the
103      * empty string if attributeId is null or if attributeId
104      * is not a valid identifier.
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      * Builds a set of attributes.
121      *
122      * @param attributeId A unique id to identify the set of attributes.
123      * This should correspond to the name of an attribute-set
124      * defined in the configuration file.
125      * @return A MutableAttributeSet that contains the attributes with
126      * the values configured for the current builder. Returns null
127      * if attributeId is null or empty, or if attributeId is not a valid identifier.
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      * Adds an attribute to the current StringBuffer.
149      *
150      * @param attributeId A unique id to identify the set of attributes.
151      * This should correspond to the name of an attribute-set
152      * defined in the configuration file.
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     /** Load the default fo configuration file. */
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             // this should not happen
196             throw new RuntimeException( cex );
197         }
198         catch ( IOException e )
199         {
200             // this should not happen
201             throw new RuntimeException( e );
202         }
203 
204         this.sets = config.getList( "xsl:attribute-set[@name]" );
205         reset();
206     }
207 
208     /**
209      * (Re-)initialize the AttributeSet.
210      */
211     private void reset()
212     {
213         this.attributeSet = new SimpleAttributeSet();
214     }
215 
216 }