ProjectNodeDeserializer.java :  » Source-Control » sourcejammer » org » sourcejammer » project » model » filesys » Java Open Source

Java Open Source » Source Control » sourcejammer 
sourcejammer » org » sourcejammer » project » model » filesys » ProjectNodeDeserializer.java
/*
 *  Copyright (C) 2001, 2002 Robert MacGrogan
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library 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
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 *
 * $Archive: SourceJammer$
 * $FileName: ProjectNodeDeserializer.java$
 * $FileID: 4568$
 *
 * Last change:
 * $AuthorName: Rob MacGrogan$
 * $Date: 4/23/03 5:17 PM$
 * $Comment: $
 */
package org.sourcejammer.project.model.filesys;

import java.util.Date;
import java.util.Vector;

import org.sourcejammer.project.NodeDoesNotExistException;
import org.sourcejammer.project.NodeExistsException;
import org.sourcejammer.project.controller.ProjectChild;
import org.sourcejammer.project.model.FileAccessException;
import org.sourcejammer.server.security.SecurityException;
import org.sourcejammer.util.ConfigurationException;
import org.sourcejammer.xml.XMLNodeDoesNotExistException;
import org.sourcejammer.xml.XMLUtil;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

/**
 * Title: $FileName: ProjectNodeDeserializer.java$
 * @version $VerNum: 3$
 * @author $AuthorName: Rob MacGrogan$<br><br>
 * 
 * $Description: Deserializes XML to ProjectNodeFS.$<br>
 * $KeyWordsOff: $<br><br>
 */
public class ProjectNodeDeserializer extends NodeDeserializer {

  private Document doc = null;
  private ProjectNodeFS project = null;
  private long key = -1;
  private NodeLibraryFS library = null;
  
  public ProjectNodeDeserializer(Document doc){
    this.doc = doc;
  }

  private void setBasicProjectInfo(ProjectNodeFS proj, long lKey, Element elmInfo){
    String sNodeName = elmInfo.getAttribute(NodeSerializer.NodeNames.NODE_NAME);
    proj.setNodeName(sNodeName, lKey);

    String sFileName = elmInfo.getAttribute(NodeSerializer.NodeNames.FILE_NAME);
    proj.setFileName(sFileName, lKey);

    String sUniqueID = elmInfo.getAttribute(NodeSerializer.NodeNames.UNIQUE_ID);
    if (sUniqueID == null){
      //Needs to be converted. Filename is unique ID.
      sUniqueID = sFileName;
    }
    long lUniqueID = Long .parseLong(sUniqueID);
    proj.setUniqueID(lUniqueID, lKey);

    String sParentID = elmInfo.getAttribute(NodeSerializer.NodeNames.PARENT_ID);

    long lParentID = Long.parseLong(sParentID);
    proj.setParentID(lParentID, lKey);

    long lCreatedDate = Long.parseLong(elmInfo.getAttribute(NodeSerializer.NodeNames.CREATED_DATE));
    proj.setCreatedDate(new Date(lCreatedDate), lKey);
  }

  private void setChildren(ProjectNodeFS proj, long lKey, Element elmRoot) 
          throws XMLNodeDoesNotExistException, FileAccessException, 
                  NodeDoesNotExistException{
    //Get Children
    Element elmChildren = XMLUtil.getChildElement(NodeSerializer.NodeNames.CHILDREN, elmRoot);
    org.w3c.dom.NodeList oList = elmChildren.getChildNodes();
    for (int iCounter = 0; iCounter < oList.getLength(); iCounter++){
      org.w3c.dom.Node oNode = oList.item(iCounter);
      Element elmChild = (Element)oNode;
      int iNodeType = Integer.parseInt(elmChild.getAttribute(NodeSerializer.NodeNames.NODE_TYPE));
      try {
        String sChildID = elmChild.getAttribute(NodeSerializer.NodeNames.UNIQUE_ID);
        if (sChildID == null){
          //Needs to be converted. Filename is unique ID.
          String sChildFileName = elmChild.getAttribute(NodeSerializer.NodeNames.FILE_NAME);
          sChildID = sChildFileName;
        }
        long lChildID = Long.parseLong(sChildID);
        ProjectChild child = new ProjectChild(lChildID, iNodeType, null);
        String sChildName = library.getProjectChildName(child);
        child = new ProjectChild(lChildID, iNodeType, sChildName);
        proj.addChildNode(lChildID, iNodeType, sChildName, lKey);
      }
      catch (NodeExistsException ex){
        throw new ConfigurationException("More than one child node of project has same name in XML on filesys.", ex);
      }
    }//end for
  }

  private void setRemovedChildren(ProjectNodeFS proj, long lKey, Element elmRoot) 
          throws XMLNodeDoesNotExistException, NodeDoesNotExistException, 
                 FileAccessException{
    //Build vector of removed children.
    Vector vecRemoved = new Vector();
    Element elmRemovedChildren = XMLUtil.getChildElement(NodeSerializer.NodeNames.REMOVED_CHILDREN, doc.getDocumentElement());
    org.w3c.dom.NodeList oRemovedList = elmRemovedChildren.getChildNodes();
    for (int iCounter = 0; iCounter < oRemovedList.getLength(); iCounter++){
      org.w3c.dom.Node oNode = oRemovedList.item(iCounter);
      Element elmRemoved = (Element)oNode;
      int iNodeType = Integer.parseInt(elmRemoved.getAttribute(NodeSerializer.NodeNames.NODE_TYPE));
      String sChildID = elmRemoved.getAttribute(NodeSerializer.NodeNames.UNIQUE_ID);
      if (sChildID == null){
        //Needs to be converted. Filename is unique ID.
        String sChildFileName = elmRemoved.getAttribute(NodeSerializer.NodeNames.FILE_NAME);
        sChildID = sChildFileName;
      }
      long lChildID = Long.parseLong(sChildID);
      ProjectChild removedChild = new ProjectChild(lChildID, iNodeType, null);
      String sChildName = library.getProjectChildName(removedChild);
      removedChild= new ProjectChild(lChildID, iNodeType, sChildName);
      vecRemoved.add(removedChild);
    }//end for
    proj.setRemovedChildren(vecRemoved);
  }

  /**
   * @see org.sourcejammer.project.model.filesys.NodeDeserializer#deserialize()
   */
  public NodeFS deserialize() throws SecurityException, XMLNodeDoesNotExistException {
    try {
      boolean bLocalLock = false;
      if (project == null){
        project = new ProjectNodeFS();
        key = new java.util.Date().getTime();
        project.lock(key);
        bLocalLock = true;
      }
      
      Element elmInfo = XMLUtil.getChildElement(NodeSerializer.NodeNames.INFO, doc.getDocumentElement());
      setBasicProjectInfo(project, key, elmInfo);
      setChildren(project, key, doc.getDocumentElement());
      setRemovedChildren(project, key, doc.getDocumentElement());
      
      if (bLocalLock){
        project.unlock(key);
      }
      project.updateLightweightViewString();
    }
    catch (FileAccessException e) {
      throw new ConfigurationException(e.getMessage(), e);
    }
    catch (NodeDoesNotExistException e) {
      throw new ConfigurationException(e.getMessage(), e);
    }
    return project;
  }

  /**
   * Sets the key.
   * @param key The key to set
   */
  public void setKey(long key) {
    this.key = key;
  }

  /**
   * Sets the project.
   * @param project The project to set
   */
  public void setProject(ProjectNodeFS project) {
    this.project = project;
  }

  /**
   * Sets the library.
   * @param library The library to set
   */
  public void setLibrary(NodeLibraryFS library) {
    this.library = library;
  }

}
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.