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