1 /*
2 * #%L
3 * wcm.io
4 * %%
5 * Copyright (C) 2022 wcm.io
6 * %%
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * #L%
19 */
20 package io.wcm.devops.conga.plugins.aem.maven.allpackage;
21
22 import java.io.ByteArrayOutputStream;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.OutputStream;
26 import java.io.OutputStreamWriter;
27 import java.nio.charset.StandardCharsets;
28 import java.util.Properties;
29
30 import org.apache.commons.lang3.Strings;
31
32 /**
33 * Read and write properties.xml for FileVault package.
34 */
35 final class FileVaultProperties {
36
37 private final Properties props;
38
39 /**
40 * Read properties from input stream.
41 * @param is Input stream
42 * @throws IOException I/O exception
43 */
44 FileVaultProperties(InputStream is) throws IOException {
45 props = new Properties();
46 props.loadFromXML(is);
47 }
48
49 public Properties getProperties() {
50 return this.props;
51 }
52
53 /**
54 * Store properties content to output stream.
55 * Ensures consistent line endings are used on all operating systems.
56 * @param os Output stream
57 * @throws IOException I/O exception
58 */
59 public void storeToXml(OutputStream os) throws IOException {
60 // write properties XML to string
61 ByteArrayOutputStream bos = new ByteArrayOutputStream();
62 props.storeToXML(bos, null);
63 String xmlOutput = bos.toString(StandardCharsets.UTF_8.name());
64
65 // normalize line endings with unix line ending
66 xmlOutput = Strings.CS.replace(xmlOutput, System.lineSeparator(), "\n");
67
68 // output normalized XML
69 OutputStreamWriter writer = new OutputStreamWriter(os, StandardCharsets.UTF_8);
70 writer.write(xmlOutput);
71 writer.flush();
72 }
73
74 }