Package misc

Source Code of misc.CreationTreeXML

package misc;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;

import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Templates;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;

import application.Application;

import com.sun.org.apache.xerces.internal.parsers.DOMParser;

import controls.ParametersPanel;

//todo : think again aout the name, is it a facility?
public class CreationTreeXML {

  private Document document = null;
 
  public CreationTreeXML() {
    super();
  }
 
  private String getLocalLanguage() {
 
    String libraryXslPath = "./library/library.xsl";
    String libraryXmlPath = "./library/library.xml";
    String localXslPath = "./library/library_"+Application.messages.getLocale().getLanguage()+".xsl";
    String localXmlPath = "./library/library_"+Application.messages.getLocale().getLanguage()+".xml";
   
    try {

            // Open xsl File
            File xslFile = new File(libraryXslPath);
            InputStream xslIs = new FileInputStream(xslFile);
           
            // Create the byte array to hold the data
            long length = xslFile.length();
            byte[] bytes = new byte[(int)length];
       
            // Read in the bytes
            int offset = 0;
            int numRead = 0;
            while (offset < bytes.length && (numRead = xslIs.read(bytes, offset, bytes.length-offset)) >= 0)
                offset += numRead;
       
            // Ensure all the bytes have been read in
            if (offset < bytes.length)
                throw new IOException("Could not completely read file "+xslFile.getName());
       
            // Close the input stream and return bytes
            xslIs.close();
           
           
            // Transforms actual xsl to localized one
            String xslStream = new String(bytes);
            String localXslStream = xslStream.replaceAll("gtlregexlanguage", Application.messages.getLocale().getLanguage())
           
            // Write new xsl file
            FileWriter localXslFile = new FileWriter(localXslPath);
            localXslFile.write(localXslStream);
            localXslFile.flush();
            localXslFile.close();
           
            // Create transformer factory
            TransformerFactory factory = TransformerFactory.newInstance();
            Templates template = factory.newTemplates(new StreamSource(new FileInputStream(localXslPath)));

            // Use the template to create a transformer
            Transformer xformer = template.newTransformer();

            // Prepare the input and output files
            Source source = new StreamSource(new FileInputStream(libraryXmlPath));
            Result result = new StreamResult(new FileOutputStream(localXmlPath));

            // Apply the xsl file to the source file and write the result to the output file
            xformer.transform(source, result);
           
        } catch (Exception e) {
          e.printStackTrace();
        }

    return localXmlPath;
  }
 
  public CustomTreeNode getTreeFromXml()
  {
    String xmlPath = getLocalLanguage();
   
    CustomTreeNode rootNode = new CustomTreeNode("Creation","Creation root");
   
    if(readFromXml(xmlPath))
    {
      if (document.hasChildNodes())
      {
        Node libraryNode = this.getXmlChildNode(document,"library");

        if(libraryNode.hasChildNodes())
        {
          Node libraryChild = libraryNode.getFirstChild();
          do
          {
            if(libraryChild.getLocalName() == "category") {
              //we know for sure we are processing a category
              Node categoryXmlNode = libraryChild ;//just for better comprehension
              //first we need the name and tooltip of this category
              CustomTreeNode categoryTreeNode = this.getTreeCategoryNode(categoryXmlNode);
              rootNode.add(categoryTreeNode);
             
              //then we may add the items
              Node itemsXmlNode = this.getXmlChildNode(categoryXmlNode,"items");
              if(itemsXmlNode == categoryXmlNode)
                break;
              appendTreeItems(itemsXmlNode,categoryTreeNode);
            }
          }while((libraryChild = libraryChild.getNextSibling()) != null);
         
        }
      }
    }
    return rootNode;
  }
  //append all the items of a category to the categorytreenode
  //return the number of items added
  private int appendTreeItems(Node itemsXmlNode,CustomTreeNode categoryTreeNode)
  {
    int count = 0;
    Node itemXmlNode = itemsXmlNode.getFirstChild();
    //enumerate the items
    do
    {
      if(itemXmlNode.getLocalName() == "item")
      {
        //it's an item for sure
        CreationTreeNode itemTreeNode = getTreeItemNode(itemXmlNode);
        if(itemTreeNode != null)
          categoryTreeNode.add(itemTreeNode);
      }
    }while((itemXmlNode = itemXmlNode.getNextSibling()) != null);
    return count;
  }
 
  private CreationTreeNode getTreeItemNode(Node itemXmlNode)
  {
    if(itemXmlNode == null)
      return null;
   
    if(itemXmlNode.getLocalName() != "item")
      return null;
    //first get the name and description of this item
    Node name = this.getXmlChildNode(itemXmlNode,"name");
    if(name==itemXmlNode)return null;
    String itemName = name.getTextContent();
    Node description = this.getXmlChildNode(itemXmlNode,"description");
    if(description==itemXmlNode)return null;
    String categoryDescription = description.getTextContent();
   
    //we may now create the asociated panel
    ParametersPanel panel = new ParametersPanel(categoryDescription);
    //then add parameters to this panel
    Node parametersNode = this.getXmlChildNode(itemXmlNode,"parameters");
    if(parametersNode==itemXmlNode)return null;
    Node parameter = parametersNode.getFirstChild();
    if(parameter != null)
    {
      //enumerate the parameters if there is some
      do
      {
        if(parameter.getLocalName() == "parameter")
        {
          String parameterType = null;
          String parameterDescription= null;
          String parameterName= null;
          NamedNodeMap attributes = parameter.getAttributes();
          if(attributes != null)
          {

            Node typeNode = null;
            if((typeNode=attributes.getNamedItem("type")) != null)
            {
              parameterType=typeNode.getTextContent();
            }
            Node nameNode = null;
            if((nameNode=attributes.getNamedItem("name")) != null)
            {
              parameterName=nameNode.getTextContent();
            }
          }
          parameterDescription = parameter.getTextContent();
          panel.addParameter(parameterType,parameterName,parameterDescription);
         
        }
 
      }while((parameter = parameter.getNextSibling()) != null);
    }
 
    //still need to add the pattern to the panel
    Node patternNode = this.getXmlChildNode(itemXmlNode,"pattern");
    if(patternNode==itemXmlNode)return null;
    Node patternItem = patternNode.getFirstChild();
    if(patternItem != null)
    {
      //enumerate the parameters if there is some
      do
      {
        if(patternItem.getLocalName() == null) // it's a part of the pattern text
        {
          panel.addPatternItem(PatternItem.Type.LITTERAL,patternItem.getTextContent());
        }
        if(patternItem.getLocalName() == "var") //it's a reference in the pattern
        {
          panel.addPatternItem(PatternItem.Type.REFERENCE,patternItem.getTextContent());
        }
      }while((patternItem = patternItem.getNextSibling()) != null);
    }
 
    CreationTreeNode itemTreeNode = new CreationTreeNode(itemName,categoryDescription,panel);
    return itemTreeNode;
  }
 
  private CustomTreeNode getTreeCategoryNode(Node category)
  {
    if(category == null)
      return null;
    if(category.getLocalName() != "category")
      return null;
    Node name = this.getXmlChildNode(category,"name");
    if(name==category)
      return null;
    String categoryName = name.getTextContent();
    Node description = this.getXmlChildNode(category,"description");
    String categoryDescription = description.getTextContent();
    CustomTreeNode categoryTreeNode = new CustomTreeNode(categoryName,categoryDescription);
    return categoryTreeNode;
  }
  private Node getXmlChildNode(Node father,String name)
  {
    if(father == null)
      return null;
    Node child= father.getFirstChild();
    do
    {
      if(child.getLocalName() == name)
        return child;
    }while((child = child.getNextSibling()) != null);
    return father;
 

  private boolean readFromXml(String xmlPath)
  {
      try
      {
        DOMParser parser = new DOMParser();
          parser.parse( xmlPath);
          this.document = parser.getDocument();         
      }  
      catch (IOException ie) {
            System.err.println(ie);
            return false;
         
      }  
      catch (SAXException se) {
            System.err.println(se);
            return false;
         
      }
      return true;
  }
}
TOP

Related Classes of misc.CreationTreeXML

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.