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 org.apache.maven.doxia.UnsupportedFormatException;
23  import org.apache.maven.doxia.parser.Parser;
24  import org.apache.maven.doxia.sink.SinkFactory;
25  import org.codehaus.plexus.PlexusContainer;
26  import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
27  
28  import java.util.Objects;
29  
30  /**
31   * Utility class to play with Doxia objects.
32   *
33   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
34   */
35  public class ConverterUtil
36  {
37      /**
38       * @param plexus not null
39       * @param format not null
40       * @param supportedFormats not null
41       * @return an instance of <code>Parser</code> depending on the format.
42       * @throws ComponentLookupException if could not find the Parser for the given format.
43       * @throws UnsupportedFormatException if the found parser is not instantiated.
44       * @throws IllegalArgumentException if any parameter is null
45       */
46      public static Parser getParser( PlexusContainer plexus, String format, String[] supportedFormats )
47          throws ComponentLookupException, UnsupportedFormatException
48      {
49          Objects.requireNonNull( plexus, "plexus is required" );
50          Objects.requireNonNull( format, "format is required" );
51          Objects.requireNonNull( supportedFormats, "supportedFormats is required" );
52  
53          Parser parser = null;
54  
55          for ( String supportedFormat : supportedFormats )
56          {
57              if ( format.equalsIgnoreCase( supportedFormat ) )
58              {
59                  parser = (Parser) plexus.lookup( Parser.ROLE, format );
60              }
61          }
62  
63          if ( parser == null )
64          {
65              throw new UnsupportedFormatException( format, supportedFormats );
66          }
67  
68          return parser;
69      }
70  
71      /**
72       * @param plexus not null
73       * @param format not null
74       * @param supportedFormats not null
75       * @return an instance of <code>SinkFactory</code> depending on the given format.
76       * @throws ComponentLookupException if could not find the SinkFactory for the given format.
77       * @throws UnsupportedFormatException if the found sink is not instantiated.
78       * @throws IllegalArgumentException if any parameter is null
79       */
80      public static SinkFactory getSinkFactory( PlexusContainer plexus, String format, String[] supportedFormats )
81          throws ComponentLookupException, UnsupportedFormatException
82      {
83          Objects.requireNonNull( plexus, "plexus is required" );
84          Objects.requireNonNull( format, "format is required" );
85          Objects.requireNonNull( supportedFormats, "supportedFormats is required" );
86  
87          SinkFactory factory = null;
88  
89          for ( String supportedFormat : supportedFormats )
90          {
91              if ( format.equalsIgnoreCase( supportedFormat ) )
92              {
93                  factory = (SinkFactory) plexus.lookup( SinkFactory.ROLE, format );
94              }
95          }
96  
97          if ( factory == null )
98          {
99              throw new UnsupportedFormatException( format, supportedFormats );
100         }
101 
102         return factory;
103     }
104 }