/*
 * Copyright
 *   2008 axYus - www.axyus.com
 *   2008 C.Bosquet - charles.bosquet@axyus.com
 *
 * This file is part of XEMELIOS.
 *
 * XEMELIOS is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * XEMELIOS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with XEMELIOS; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package fr.gouv.finances.dgfip.xemelios.web.utils;

import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;

/**
 * @author CBO
 * Classe facilitant la production de xml simple
 * TODO: a réécrire completement
 * @deprecated
 */
@Deprecated
public class SimpleXmlWriter {

  private String encoding;
  private Writer writer;
  private int openElementCount = 0;

  public SimpleXmlWriter(Writer writer) {
      this.writer = writer;
  }

  public SimpleXmlWriter(OutputStream outputStream, String encoding) throws IOException {
    this.encoding = encoding;
    this.writer = new OutputStreamWriter(outputStream, encoding);
    this.writer.append("<?xml version=\"1.0\" encoding=\"" + encoding + "\"?>");
  }

  private void writeEndLineAndSpaces(boolean appendNEwLine) throws IOException {
    if(appendNEwLine) this.writer.append('\n');
    for (int i=0; i<openElementCount; i++) {
      this.writer.append(' ').append(' ');
    }
  }

  public SimpleXmlWriter startElement(String name, Attribute... attributes) throws IOException {
    writeEndLineAndSpaces(true);
    this.writer.append('<');
    this.writer.append(name);
    for (Attribute attribute : attributes) {
      this.writer.append(" ");
      this.writer.append(attribute.getName());
      this.writer.append("=");
      this.writer.append('"');
      this.writer.append(attribute.getValue() != null ? escapeChars(attribute.getValue()) : "");
      this.writer.append('"');
    }
    this.writer.append('>');
    openElementCount++;
    return this;
  }

  public SimpleXmlWriter writeElementContent(String content) throws IOException {
    this.writer.append(content != null ? escapeChars(content) : "");
    return this;
  }

  public SimpleXmlWriter endElement(String name) throws IOException {
    openElementCount--;
    writeEndLineAndSpaces(false);
    this.writer.append('<');
    this.writer.append('/');
    this.writer.append(name);
    this.writer.append('>');
    return this;
  }

  public SimpleXmlWriter writeEmptyElement(String name, Attribute... attributes) throws IOException {
    writeEndLineAndSpaces(true);
    this.writer.append('<');
    this.writer.append(name);
    for (Attribute attribute : attributes) {
      this.writer.append(" ");
      this.writer.append(attribute.getName());
      this.writer.append("=");
      this.writer.append('"');
      this.writer.append(attribute.getValue() != null ? escapeChars(attribute.getValue()) : "");
      this.writer.append('"');
    }
    this.writer.append("/>");
    return this;
  }

  public SimpleXmlWriter writeTextElement(String name, String text, Attribute... attributes) throws IOException {
//    writeEndLineAndSpaces();
    startElement(name, attributes);
    writeElementContent(text != null ? escapeChars(text) : "");
    return endElement(name);
  }

  public void flush() throws IOException {
    this.writer.flush();
  }

  static public class Attribute {

    private String name;
    private String value;

    public Attribute(String name, String value) {
      this.name = name;
      this.value = value;
    }

    public Attribute(String name, Long value) {
      this.name = name;
      this.value = value != null ? String.valueOf(value) : null;
    }

    public String getName() {
      return name;
    }

    public String getValue() {
      return value;
    }
  }

  public static String escapeChars(String source) {
    if (source == null) {
      return null;
    }
    StringBuffer result = new StringBuffer(source.length() * 2);
    for (int i = 0; i < source.length(); ++i) {
      char ch = source.charAt(i);
      switch (ch) {
        case '<':
          result.append("&lt;");
          break;
        case '>':
          result.append("&gt;");
          break;
        case '&':
          result.append("&amp;");
          break;
        case '"':
          result.append("&quot;");
          break;
        default:
          result.append(ch);
      }
    }
    return new String(result);
  }
}

