Package org.jboss.fresh.shell.commands

Source Code of org.jboss.fresh.shell.commands.XMLSerializerExe

package org.jboss.fresh.shell.commands;

import org.jboss.fresh.io.BufferObjectReader;
import org.jboss.fresh.io.BufferObjectWriter;
import org.jboss.fresh.io.BufferWriter;
import org.jboss.fresh.shell.AbstractExecutable;
import org.jboss.fresh.xml.XMLHelper;
import org.w3c.dom.Document;
import org.w3c.dom.DocumentFragment;
import org.w3c.dom.Element;

import java.io.IOException;
import java.io.PrintWriter;

/**
* @author boky
* @created 2003.3.31 13:45:50
* @version 1.0
*/
public class XMLSerializerExe extends AbstractExecutable {
  private static transient org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(XMLSerializerExe.class);
  private static transient org.apache.log4j.Logger log_xml = org.apache.log4j.Logger.getLogger(XMLSerializerExe.class.getName() + ".XML");


  public void process(String execName, String[] args) throws Exception {
    long benchmark = System.currentTimeMillis();

    log.debug("entering");
    PrintWriter err = null;

    if (helpRequested()) {
      printHelp(execName);
      log.debug("done");
      return;
    }

    if (!canThrowEx()) {
      err = new PrintWriter(new BufferWriter(getStdOut()));
    }

    boolean forceString = isSwitchActive(args, "S", "string");
    boolean forceDOM = isSwitchActive(args, "D", "DOM");

    if (forceString && forceDOM) {
      if (err == null) {
        throw new RuntimeException("Cannot specify both, --DOM and --string in the same command line!");
      } else {
        err.println("Cannot specify both, --DOM and --string in the same command line!");
        return;
      }

    }

    BufferObjectReader in = new BufferObjectReader(getStdIn());
    BufferObjectWriter out = new BufferObjectWriter(getStdOut());

    Object obj;
    while (!in.isFinished()) {
      obj = in.readObject();
      // process input stream
      if (obj == null) {
        if (forceString) {
          log.debug("Object is null. Outputing empty String.");
          out.writeObject("");
        } else {
          log.debug("Object is null. Outputing empty Document.");
          out.writeObject(XMLHelper.getDocument());
        }
      } else if (obj instanceof Document) {
        if (forceDOM) {
          log.debug("Object is Document. Forced output to DOM, so just returning it.");
          if (log_xml.isDebugEnabled()) {
            log_xml.debug("Document -> Document: " + XMLHelper.getXML((Document) obj));
          }
          out.writeObject(obj);
        } else {
          try {
            String s = XMLHelper.getXML((Document) obj);
            log.debug("Object is Document. Returning string.");
            log_xml.debug("Document -> String: " + s);
            out.writeObject(s);
          } catch (IOException e) {
            log.error("Could not parse input Document into string! Weird.", e);
            if (err == null) {
              throw new RuntimeException("Could not parse input Document into string! Weird.", e);
            } else {
              err.write("Could not parse input Document into string! Weird.");
              e.printStackTrace(err);
            }
          }
        }
      } else if (obj instanceof Element) {
        if (forceDOM) {
          log.debug("Object is Element. Forced output to DOM, so just returning it.");
          if (log_xml.isDebugEnabled()) {
            log_xml.debug("Element -> Element: " + XMLHelper.getXML((Element) obj));
          }
          out.writeObject(obj);
        } else {
          try {
            String s = XMLHelper.getXML((Element) obj);
            log.debug("Object is Element. Returning string.");
            log_xml.debug("Element -> String: " + s);
            out.writeObject(s);
          } catch (IOException e) {
            log.error("Could not parse input Element into string! Weird.", e);
            if (err == null) {
              throw new RuntimeException("Could not parse input Element into string! Weird.", e);
            } else {
              err.write("Could not parse input Element into string! Weird.");
              e.printStackTrace(err);
            }
          }
        }
      } else if (obj instanceof DocumentFragment) {
        if (forceDOM) {
          log.debug("Object is DocumentFragment. Forced output to DOM, so just returning it.");
          if (log_xml.isDebugEnabled()) {
            log_xml.debug("DocumentFragment -> DocumentFragment: " + XMLHelper.getXML((DocumentFragment) obj));
          }
          out.writeObject(obj);
        } else {
          try {
            String s = XMLHelper.getXML((DocumentFragment) obj);
            log.debug("Object is DocumentFragment. Returning string.");
            log_xml.debug("DocumentFragment -> String: " + s);
            out.writeObject(s);
          } catch (IOException e) {
            log.error("Could not parse input DocumentFragment into string! Weird.", e);
            if (err == null) {
              throw new RuntimeException("Could not parse input DocumentFragment into string! Weird.", e);
            } else {
              err.write("Could not parse input DocumentFragment into string! Weird.");
              e.printStackTrace(err);
            }
          }
        }
      } else {
        if (log.isDebugEnabled() && (!(obj instanceof String))) {
          log.debug("We got this:" + (obj == null ? "<null>" : obj.getClass().getName()), new Throwable());
        }
        String s;

        if (obj instanceof char[]) {
          s = new String((char[]) obj);
        } else if (obj instanceof byte[]) {
          s = new String((byte[]) obj, "UTF8");
        } else {
          s = obj.toString();
        }
        if (forceString) {
          log.debug("Object is String. Forced output to String, so just returning it.");
          log_xml.debug("String -> String: " + s);
          out.writeObject(s);
        } else {
          try {
            log.debug("Object is String. Parsing it to DOM Document.");
            log_xml.debug("String -> Document: " + s);
            out.writeObject(XMLHelper.getDocument(s));
          } catch (Exception e) {
            log.error("Could not parse input string into DOM! Probably not a valid XML.", e);
            if (err == null) {
              throw new RuntimeException("Could not parse input string into DOM! Probably not a valid XML.", e);
            } else {
              err.write("Could not parse input string into DOM! Probably not a valid XML.");
              e.printStackTrace(err);
            }
          }
        }
      }
    }
    log.debug("XMLSerializerExe spent " + (System.currentTimeMillis() - benchmark) + "ms for its execution.");
  }

  private void printHelp(String execName) {
    PrintWriter out = new PrintWriter(new BufferWriter(getStdOut()));
    out.println("Usage: " + execName + " [-ex] [--help] [--DOM | --string]");
    out.println();
    out.println("This executable will (de)serialize the XML. The following coversions are supported:");
    out.println("  STDIN                             STDOUT");
    out.println("  String                            org.w3c.dom.Document");
    out.println("  -                                 org.w3c.dom.Document (empty)");
    out.println("  org.w3c.dom.Document              String");
    out.println("  org.w3c.dom.DocumentFragment      String");
    out.println("  org.w3c.dom.Element               String");
    out.println();
    out.println("The executable will fail if non-valid XML is given. You can");
    out.println("(de)serialize multiple objects at a time.");
    out.println();
    out.println("Command line arguments:");
    out.println("  --help or -h               Displays this help.");
    out.println("  -ex                        Instead of outputting the error to the command");
    out.println("                             line, throw the exception. This parameter must");
    out.println("                             first, else it is parsed as -e -x.");
    out.println("  --DOM or -D                Force the output to XML DOM, no matter what the");
    out.println("                             input is.");
    out.println("  --string or -S             Force the output to string, no matter what the");
    out.println("                             input is.");
    out.close();
  }


}
TOP

Related Classes of org.jboss.fresh.shell.commands.XMLSerializerExe

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.