View Javadoc
1   package org.apache.maven.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  import java.nio.charset.StandardCharsets;
26  
27  /**
28   * Wraps an ordinary {@link CharSequence} as a source.
29   *
30   * @author Benjamin Bentmann
31   */
32  public class StringSource
33      implements Source
34  {
35      private final String content;
36  
37      private final String location;
38  
39      private final int hashCode;
40  
41      /**
42       * Creates a new source backed by the specified string.
43       *
44       * @param content The String representation, may be empty or {@code null}.
45       */
46      public StringSource( CharSequence content )
47      {
48          this( content, null );
49      }
50  
51      /**
52       * Creates a new source backed by the specified string.
53       *
54       * @param content The String representation, may be empty or {@code null}.
55       * @param location The location to report for this use, may be {@code null}.
56       */
57      public StringSource( CharSequence content, String location )
58      {
59          this.content = ( content != null ) ? content.toString() : "";
60          this.location = ( location != null ) ? location : "(memory)";
61          this.hashCode = this.content.hashCode();
62      }
63  
64      @Override
65      public InputStream getInputStream()
66          throws IOException
67      {
68          return new ByteArrayInputStream( content.getBytes( StandardCharsets.UTF_8 ) );
69      }
70  
71      @Override
72      public String getLocation()
73      {
74          return location;
75      }
76  
77      /**
78       * Gets the content of this source.
79       *
80       * @return The underlying character stream, never {@code null}.
81       */
82      public String getContent()
83      {
84          return content;
85      }
86  
87      @Override
88      public String toString()
89      {
90          return getLocation();
91      }
92  
93      @Override
94      public int hashCode()
95      {
96          return hashCode;
97      }
98  
99      @Override
100     public boolean equals( Object obj )
101     {
102         if ( this == obj )
103         {
104             return true;
105         }
106 
107         if ( obj == null )
108         {
109             return false;
110         }
111 
112         if ( !StringSource.class.equals( obj.getClass() ) )
113         {
114             return false;
115         }
116 
117         StringSource other = (StringSource) obj;
118         return this.content.equals( other.content );
119     }
120 }