FileVaultProperties.java

  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. import java.io.ByteArrayOutputStream;
  22. import java.io.IOException;
  23. import java.io.InputStream;
  24. import java.io.OutputStream;
  25. import java.io.OutputStreamWriter;
  26. import java.nio.charset.StandardCharsets;
  27. import java.util.Properties;

  28. import org.apache.commons.lang3.StringUtils;

  29. /**
  30.  * Read and write properties.xml for FileVault package.
  31.  */
  32. final class FileVaultProperties {

  33.   private final Properties props;

  34.   /**
  35.    * Read properties from input stream.
  36.    * @param is Input stream
  37.    * @throws IOException I/O exception
  38.    */
  39.   FileVaultProperties(InputStream is) throws IOException {
  40.     props = new Properties();
  41.     props.loadFromXML(is);
  42.   }

  43.   public Properties getProperties() {
  44.     return this.props;
  45.   }

  46.   /**
  47.    * Store properties content to output stream.
  48.    * Ensures consistent line endings are used on all operating systems.
  49.    * @param os Output stream
  50.    * @throws IOException I/O exception
  51.    */
  52.   public void storeToXml(OutputStream os) throws IOException {
  53.     // write properties XML to string
  54.     ByteArrayOutputStream bos = new ByteArrayOutputStream();
  55.     props.storeToXML(bos, null);
  56.     String xmlOutput = bos.toString(StandardCharsets.UTF_8.name());

  57.     // normalize line endings with unix line ending
  58.     xmlOutput = StringUtils.replace(xmlOutput, System.lineSeparator(), "\n");

  59.     // output normalized XML
  60.     OutputStreamWriter writer = new OutputStreamWriter(os, StandardCharsets.UTF_8);
  61.     writer.write(xmlOutput);
  62.     writer.flush();
  63.   }

  64. }