View Javadoc

1   package org.apache.maven.doxia.util;
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.IOException;
23  
24  import org.apache.maven.doxia.UnsupportedFormatException;
25  import org.apache.maven.doxia.parser.Parser;
26  import org.apache.maven.doxia.sink.SinkFactory;
27  import org.codehaus.plexus.PlexusContainer;
28  import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
29  
30  /**
31   * Utility class to play with Doxia objects.
32   *
33   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
34   * @version $Id: ConverterUtil.java 786981 2009-06-21 10:01:58Z ltheussl $
35   */
36  public class ConverterUtil
37  {
38      /**
39       * @param plexus not null
40       * @param format not null
41       * @param supportedFormats not null
42       * @return an instance of <code>Parser</code> depending on the format.
43       * @throws ComponentLookupException if could not find the Parser for the given format.
44       * @throws UnsupportedFormatException if the found parser is not instantiated.
45       * @throws IllegalArgumentException if any parameter is null
46       */
47      public static Parser getParser( PlexusContainer plexus, String format, String[] supportedFormats )
48          throws ComponentLookupException, UnsupportedFormatException
49      {
50          if ( plexus == null )
51          {
52              throw new IllegalArgumentException( "plexus is required" );
53          }
54  
55          if ( format == null )
56          {
57              throw new IllegalArgumentException( "format is required" );
58          }
59  
60          if ( supportedFormats == null )
61          {
62              throw new IllegalArgumentException( "supportedFormats is required" );
63          }
64  
65          Parser parser = null;
66  
67          for ( int i = 0; i < supportedFormats.length; i++ )
68          {
69              if ( format.equalsIgnoreCase( supportedFormats[i] ) )
70              {
71                  parser = (Parser) plexus.lookup( Parser.ROLE, format );
72              }
73          }
74  
75          if ( parser == null )
76          {
77              throw new UnsupportedFormatException( format, supportedFormats );
78          }
79  
80          return parser;
81      }
82  
83      /**
84       * @param plexus not null
85       * @param format not null
86       * @param supportedFormats not null
87       * @return an instance of <code>SinkFactory</code> depending on the given format.
88       * @throws ComponentLookupException if could not find the SinkFactory for the given format.
89       * @throws UnsupportedFormatException if the found sink is not instantiated.
90       * @throws IOException if any.
91       * @throws IllegalArgumentException if any parameter is null
92       */
93      public static SinkFactory getSinkFactory( PlexusContainer plexus, String format, String[] supportedFormats )
94          throws ComponentLookupException, UnsupportedFormatException
95      {
96          if ( plexus == null )
97          {
98              throw new IllegalArgumentException( "plexus is required" );
99          }
100 
101         if ( format == null )
102         {
103             throw new IllegalArgumentException( "format is required" );
104         }
105 
106         if ( supportedFormats == null )
107         {
108             throw new IllegalArgumentException( "supportedFormats is required" );
109         }
110 
111         SinkFactory factory = null;
112 
113         for ( int i = 0; i < supportedFormats.length; i++ )
114         {
115             if ( format.equalsIgnoreCase( supportedFormats[i] ) )
116             {
117                 factory = (SinkFactory) plexus.lookup( SinkFactory.ROLE, format );
118             }
119         }
120 
121         if ( factory == null )
122         {
123             throw new UnsupportedFormatException( format, supportedFormats );
124         }
125 
126         return factory;
127     }
128 }