View Javadoc

1   package org.apache.maven.settings.building;
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.ByteArrayInputStream;
23  import java.io.IOException;
24  import java.io.InputStream;
25  
26  /**
27   * Wraps an ordinary {@link CharSequence} as a settings source.
28   * 
29   * @author Benjamin Bentmann
30   */
31  public class StringSettingsSource
32      implements SettingsSource
33  {
34  
35      private String settings;
36  
37      private String location;
38  
39      /**
40       * Creates a new settings source backed by the specified string.
41       * 
42       * @param settings The settings' string representation, may be empty or {@code null}.
43       */
44      public StringSettingsSource( CharSequence settings )
45      {
46          this( settings, null );
47      }
48  
49      /**
50       * Creates a new settings source backed by the specified string.
51       * 
52       * @param settings The settings' string representation, may be empty or {@code null}.
53       * @param location The location to report for this use, may be {@code null}.
54       */
55      public StringSettingsSource( CharSequence settings, String location )
56      {
57          this.settings = ( settings != null ) ? settings.toString() : "";
58          this.location = ( location != null ) ? location : "(memory)";
59      }
60  
61      public InputStream getInputStream()
62          throws IOException
63      {
64          return new ByteArrayInputStream( settings.getBytes( "UTF-8" ) );
65      }
66  
67      public String getLocation()
68      {
69          return location;
70      }
71  
72      /**
73       * Gets the character sequence of this settings source.
74       * 
75       * @return The underlying character stream, never {@code null}.
76       */
77      public String getSettings()
78      {
79          return settings;
80      }
81  
82      @Override
83      public String toString()
84      {
85          return getLocation();
86      }
87  
88  }