1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package org.apache.maven.building;
20
21 import java.io.ByteArrayInputStream;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.nio.charset.StandardCharsets;
25
26 /**
27 * Wraps an ordinary {@link CharSequence} as a source.
28 *
29 * @deprecated since 4.0.0, use {@link org.apache.maven.api.services} instead
30 */
31 @Deprecated(since = "4.0.0")
32 public class StringSource implements Source {
33 private final String content;
34
35 private final String location;
36
37 private final int hashCode;
38
39 /**
40 * Creates a new source backed by the specified string.
41 *
42 * @param content The String representation, may be empty or {@code null}.
43 */
44 public StringSource(CharSequence content) {
45 this(content, null);
46 }
47
48 /**
49 * Creates a new source backed by the specified string.
50 *
51 * @param content The String representation, may be empty or {@code null}.
52 * @param location The location to report for this use, may be {@code null}.
53 */
54 public StringSource(CharSequence content, String location) {
55 this.content = (content != null) ? content.toString() : "";
56 this.location = (location != null) ? location : "(memory)";
57 this.hashCode = this.content.hashCode();
58 }
59
60 @Override
61 public InputStream getInputStream() throws IOException {
62 return new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8));
63 }
64
65 @Override
66 public String getLocation() {
67 return location;
68 }
69
70 /**
71 * Gets the content of this source.
72 *
73 * @return The underlying character stream, never {@code null}.
74 */
75 public String getContent() {
76 return content;
77 }
78
79 @Override
80 public String toString() {
81 return getLocation();
82 }
83
84 @Override
85 public int hashCode() {
86 return hashCode;
87 }
88
89 @Override
90 public boolean equals(Object obj) {
91 if (this == obj) {
92 return true;
93 }
94
95 if (obj == null) {
96 return false;
97 }
98
99 if (!StringSource.class.equals(obj.getClass())) {
100 return false;
101 }
102
103 StringSource other = (StringSource) obj;
104 return this.content.equals(other.content);
105 }
106 }