AjaxFormsFormater.java :  » J2EE » Enhydra-Application-Framework » org » enhydra » util » ajaxforms » Java Open Source

Java Open Source » J2EE » Enhydra Application Framework 
Enhydra Application Framework » org » enhydra » util » ajaxforms » AjaxFormsFormater.java
package org.enhydra.util.ajaxforms;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.IllegalCharsetNameException;
import java.nio.charset.UnsupportedCharsetException;

import org.enhydra.util.DOMFormatter;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.Text;

public class AjaxFormsFormater implements DOMFormatter{

  private String encoding = null;
  
  public AjaxFormsFormater (String encoding){
    try {
      if (null!=Charset.forName(encoding))
          this.encoding = encoding;
    } catch (Exception ex){
      // keep default encoding
    }
    
  }
  
  public void setEncoding(String encoding){
    try {
      if (null!=Charset.forName(encoding))
          this.encoding = encoding;
    } catch (IllegalCharsetNameException ex){
      // keep default encoding
    } catch (UnsupportedCharsetException ex) {
      // keep default encoding
    }
  }
  
  public String getEncoding(){
    return this.encoding;
  }
  

  public byte[] toBytes(Node document) {
    removeEmpty(document);
    StringBuffer sb = new StringBuffer();
    try {
      serialize(document, sb);
    } catch (IOException e) {
      e.printStackTrace();
    }
    
    try {
      return sb.toString().getBytes(encoding);
    } catch (Exception e) {
      return sb.toString().getBytes();
    }

  }

  private void serialize(Node node, StringBuffer sb) throws IOException {
    short type = node.getNodeType();
    String name = node.getNodeName();

    boolean jsp = false;

    switch (type) {
    case Node.DOCUMENT_NODE:
      sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
      break;
    case Node.ELEMENT_NODE:
      if (!jsp || !name.equals("jsp:root")) {
        sb.append("<").append(name);
        NamedNodeMap atts = ((Element) node).getAttributes();

        for (int i = 0; i < atts.getLength(); i++) {
          serialize(atts.item(i), sb);
        }

        sb.append(">");

        if (jsp && name.indexOf(":") != -1) {
          String pre = name.split(":")[0];
          String uri = node.lookupNamespaceURI(pre);
          boolean tagDir = uri.startsWith("urn:jsptagdir:");

          if (tagDir) {
            uri = uri.substring(14);
          }

          String dec = "<%@ taglib " + (tagDir ? "tagdir" : "uri")
              + "=\"" + uri + "\" prefix=\"" + pre + "\" %>\n";

          if (sb.indexOf(dec) == -1) {
            sb.insert(0, dec);
          }
        }
      }

      break;
    case Node.ATTRIBUTE_NODE:
      boolean process = name.indexOf("xmlns") == -1;

      if (process) {
        sb.append(" ").append(name).append("=\"");
        sb.append(encoding(node.getNodeValue())).append("\"");
      }

      break;
    case Node.TEXT_NODE:
      sb.append(encoding(node.getNodeValue()));
      break;
    }

    if (type != Node.ATTRIBUTE_NODE) {
      int length = node.getChildNodes().getLength();

      for (int i = 0; i < length; i++) {
        serialize(node.getChildNodes().item(i), sb);
      }
    }

    if (type == Node.ELEMENT_NODE) {
      if (jsp || !name.equals("jsp:root")) {
        sb.append("</").append(name).append(">");
      }
    }
  }

  private void removeEmpty(Node node) {
    if (node instanceof Text) {
      String nodeValue = node.getNodeValue().trim();

      if (nodeValue == null || nodeValue.length() == 0
          || (nodeValue.length() == 1 && nodeValue.charAt(0) == 160)) {
        if (!node.getParentNode().getLocalName().equals("span")) {
          node.getParentNode().removeChild(node);
        }
      }
    } else {
      Node child = node.getFirstChild();
      Node old = child;

      while (child != null) {
        removeEmpty(child);

        if (child.getParentNode() != null) {
          old = child;
          child = child.getNextSibling();
        } else if (old.getParentNode() == null) {
          child = node.getFirstChild();
          old = child;
        } else {
          child = old.getNextSibling();
        }
      }
    }
  }

  private String encoding(String text) {
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < text.length(); i++) {
      int c = text.charAt(i);

      switch (c) {
      case '&':
        sb.append("&amp;");
        break;
      case '>':
        sb.append("&gt;");
        break;
      case '<':
        sb.append("&lt;");
        break;
      default:
        sb.append((char) c);
      }
    }

    return sb.toString();
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.