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  import org.apache.maven.doxia.sink.impl.SinkUtils;
32  import org.codehaus.plexus.util.ReaderFactory;
33  
34  /**
35   * A utility class to construct FO configuration parameters.
36   *
37   * @author ltheussl
38   * @version $Id: FoConfiguration.java 1726411 2016-01-23 16:34:09Z hboutemy $
39   * @since 1.1
40   */
41  public class FoConfiguration
42  {
43      /** Holds the single attributes. */
44      private MutableAttributeSet attributeSet;
45  
46      /** The configuration instance. */
47      private final XMLConfiguration config;
48  
49      /** The list of attribute sets. */
50      private List<?> sets;
51  
52      /**
53       * Constructor.
54       */
55      public FoConfiguration()
56      {
57          this.config = new XMLConfiguration();
58  
59          // necessary because some attributes contain commas:
60          config.setDelimiterParsingDisabled( true );
61  
62          loadDefaultConfig();
63      }
64  
65      /**
66       * Load configuration parameters from a File.
67       *
68       * @param configFile the configuration file.
69       *
70       * @throws java.io.IOException if the File cannot be read
71       *  or some error occurs when initializing the configuration parameters.
72       *
73       * @since 1.1.1
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(); // this adds default values that are missing from above
92      }
93  
94      /**
95       * Builds a list of attributes.
96       *
97       * @param attributeId A unique id to identify the set of attributes.
98       * This should correspond to the name of an attribute-set
99       * defined in the configuration file.
100      * @return A string that contains a list of attributes with
101      * the values configured for the current builder. Returns the
102      * empty string if attributeId is null or if attributeId
103      * is not a valid identifier.
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      * Builds a set of attributes.
120      *
121      * @param attributeId A unique id to identify the set of attributes.
122      * This should correspond to the name of an attribute-set
123      * defined in the configuration file.
124      * @return A MutableAttributeSet that contains the attributes with
125      * the values configured for the current builder. Returns null
126      * if attributeId is null or empty, or if attributeId is not a valid identifier.
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      * Adds an attribute to the current StringBuilder.
148      *
149      * @param attributeId A unique id to identify the set of attributes.
150      * This should correspond to the name of an attribute-set
151      * defined in the configuration file.
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     /** Load the default fo configuration file. */
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             // this should not happen
195             throw new RuntimeException( cex );
196         }
197         catch ( IOException e )
198         {
199             // this should not happen
200             throw new RuntimeException( e );
201         }
202 
203         this.sets = config.getList( "xsl:attribute-set[@name]" );
204         reset();
205     }
206 
207     /**
208      * (Re-)initialize the AttributeSet.
209      */
210     private void reset()
211     {
212         this.attributeSet = new SimpleAttributeSet();
213     }
214 
215 }