1 package org.apache.maven.plugins.shade.resource.properties.io;
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.BufferedWriter;
23 import java.io.IOException;
24 import java.io.Writer;
25
26 /**
27 * Simple buffered writer skipping its first write(String) call.
28 */
29 public class SkipPropertiesDateLineWriter extends BufferedWriter
30 {
31 private State currentState = State.MUST_SKIP_DATE_COMMENT;
32
33 public SkipPropertiesDateLineWriter( Writer out )
34 {
35 super( out );
36 }
37
38 @Override
39 public void write( String str ) throws IOException
40 {
41 if ( currentState.shouldSkip( str ) )
42 {
43 currentState = currentState.next();
44 return;
45 }
46 super.write( str );
47 }
48
49 private enum State
50 {
51 MUST_SKIP_DATE_COMMENT
52 {
53 @Override
54 boolean shouldSkip( String content )
55 {
56 return content.length() > 1 && content.startsWith( "#" ) && !content.startsWith( "# " );
57 }
58
59 @Override
60 State next()
61 {
62 return SKIPPED_DATE_COMMENT;
63 }
64 },
65 SKIPPED_DATE_COMMENT
66 {
67 @Override
68 boolean shouldSkip( String content )
69 {
70 return content.trim().isEmpty();
71 }
72
73 @Override
74 State next()
75 {
76 return DONE;
77 }
78 },
79 DONE
80 {
81 @Override
82 boolean shouldSkip( String content )
83 {
84 return false;
85 }
86
87 @Override
88 State next()
89 {
90 throw new UnsupportedOperationException( "done is a terminal state" );
91 }
92 };
93
94 abstract boolean shouldSkip( String content );
95 abstract State next();
96 }
97 }