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

Java Open Source » Source Control » sourcejammer 
sourcejammer » org » sourcejammer » project » model » filesys » FileNodeSerializer.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: FileNodeSerializer.java$
 * $FileID: 4564$
 *
 * Last change:
 * $AuthorName: Rob MacGrogan$
 * $Date: 4/23/03 5:17 PM$
 * $Comment: $
 */
 
package org.sourcejammer.project.model.filesys;

import java.util.Enumeration;

import org.sourcejammer.project.NodeIterator;
import org.sourcejammer.project.controller.NoSuchVersionException;
import org.sourcejammer.project.view.NodeInfo;
import org.sourcejammer.util.AppConfig;
import org.sourcejammer.xml.XMLUtil;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

/**
 * Title: $FileName: FileNodeSerializer.java$
 * @version $VerNum: 4$
 * @author $AuthorName: Rob MacGrogan$<br><br>
 * 
 * $Description: Serializes FileNode objects to XML.$<br>
 * $KeyWordsOff: $<br><br>
 * 
 * Given a FileNodeFS, generates the serialized XML.
 */
public class FileNodeSerializer extends NodeSerializer{

  private FileNodeFS node = null;

  /**
   * Default constructor. Pass in node to be serialized and call serialize.
   */
  public FileNodeSerializer(FileNodeFS node){
    this.node = node;
  }

  /**
   * Adds all basic info to info section of XML doc.
   */
  public void addBasicFileInfo(FileNodeFS file, Element elmInfo){

    elmInfo.setAttribute( NodeNames.NODE_TYPE,
                          Integer.toString(AppConfig.NodeTypes.FILE));
    elmInfo.setAttribute( NodeNames.FILE_TYPE,
             Integer.toString(file.getFileType()));

    elmInfo.setAttribute( NodeNames.HISTORY_STORAGE_TYPE,
             Integer.toString(file.getHistoryStorageType()));


    if (node.getDescrption() != null){
      XMLUtil.addNewChildElement( NodeNames.COMMENT,
                                  file.getDescrption(),
                                  elmInfo);
    }

    String sIsLimitHistorySize;
    if(file.isLimitHistorySize()){
      elmInfo.setAttribute( NodeNames.IS_LIMIT_HISTORY_SIZE, "true");
    }

    elmInfo.setAttribute( NodeNames.MAX_HISTORY_SIZE,
                                Integer.toString(node.getMaxHistorySize()));

    elmInfo.setAttribute(NodeNames.MASTER_FILE_ID, 
                         Long.toString(file.getMasterFileID()));

    try {
      NodeInfo ndLatestVer = file.getLatestVersionNode();
      elmInfo.setAttribute( NodeNames.LATEST_VERSION_FILENAME,
                                       Long.toString(ndLatestVer.getUniqueID()));
    }
    catch (NoSuchVersionException ex){
      //This should never happen. And if it does, it may not be a big deal.
    }

  }

  /**
   * Adds check out info to XML doc.
   */
  private void addCheckOutInfo(FileNodeFS file, Element elmInfo){
    String sIsCheckedOut;
    if (file.isCheckedOut()){
      elmInfo.setAttribute( NodeNames.IS_CHECKED_OUT, "true");
      elmInfo.setAttribute( NodeNames.CHECKED_OUT_TO_USER, file.getCheckedOutToUser());
      elmInfo.setAttribute( NodeNames.CHECK_OUT_DIRECTORY, file.getCheckedOutToPath());
      long lCheckOutDate = file.getCheckedOutDate().getTime();
      elmInfo.setAttribute( NodeNames.CHECK_OUT_DATE, Long.toString(lCheckOutDate));
    }
  }

  /**
   * Adds list of versions to XML doc.
   */
  private void addVersionChildren(FileNodeFS file, Element elmRoot){
    Element elmChildren = XMLUtil.addNewChildElement(NodeNames.CHILDREN,
                                                     elmRoot );
    //Add Child Versions
    NodeIterator itrChildNodes = file.getVersions();
    int iChildCounter = 0;
    while(itrChildNodes.hasMoreNodes()){
      iChildCounter++;
      NodeInfo ndChild = (NodeInfo)itrChildNodes.getNextNode();
      childToElement(Integer.toString(iChildCounter),
                            elmChildren,
                            ndChild);
    }//end while has more child nodes
  }
  
  /**
   * Adds list of parent ids to XML doc.
   */
  private void addParentList(FileNodeFS file, Element elmRoot){
    Element elmParents = XMLUtil.addNewChildElement(NodeNames.PARENTS,
                                                     elmRoot );
    Enumeration enmParents = file.getParents();
    while(enmParents.hasMoreElements()){
      Long lng = (Long)enmParents.nextElement();
      long id = lng.longValue();
      Element elmChild = XMLUtil.addNewChildElement(NodeNames.PARENT, elmParents);

      elmChild.setAttribute(NodeNames.PARENT_ID, Long.toString(id));
    }
  }

  /**
   * Adds list of removed parent ids to XML doc.
   */
  private void addRemovedParentList(FileNodeFS file, Element elmRoot){
    Element elmParents = XMLUtil.addNewChildElement(NodeNames.REMOVED_PARENTS,
                                                     elmRoot );
    Enumeration enmParents = file.getRemovedParents();
    while(enmParents.hasMoreElements()){
      Long lng = (Long)enmParents.nextElement();
      long id = lng.longValue();
      Element elmChild = XMLUtil.addNewChildElement(NodeNames.PARENT, elmParents);

      elmChild.setAttribute(NodeNames.PARENT_ID, Long.toString(id));
    }
  }
  
  /**
   * Turn a FileNodeFS object into an XML Doc for storage
   * on file system.
   */
  public Document serialize(){

    NodeXMLBean oBean = buildBasicNodeDocument(node);
    Document docFile = oBean.getDocument();
    Element elmInfo = oBean.getInfoElement();

    addBasicFileInfo(node, elmInfo);
    addCheckOutInfo(node, elmInfo);

    Element elmRoot = oBean.getRootElement();
    addVersionChildren(node, elmRoot);
    addParentList(node, elmRoot);
    addRemovedParentList(node, elmRoot);
    
    return docFile;
  }

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