001    /******************************************************************************
002     * Copyright (C) MActor Developers. All rights reserved.                        *
003     * ---------------------------------------------------------------------------*
004     * This file is part of MActor.                                               *
005     *                                                                            *
006     * MActor is free software; you can redistribute it and/or modify             *
007     * it under the terms of the GNU General Public License as published by       *
008     * the Free Software Foundation; either version 2 of the License, or          *
009     * (at your option) any later version.                                        *
010     *                                                                            *
011     * MActor is distributed in the hope that it will be useful,                  *
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
014     * GNU General Public License for more details.                               *
015     *                                                                            *
016     * You should have received a copy of the GNU General Public License          *
017     * along with MActor; if not, write to the Free Software                      *
018     * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA *
019     ******************************************************************************/
020    package org.mactor.ui.gui.project;
021    
022    import java.io.File;
023    import java.io.FileReader;
024    import java.io.IOException;
025    import java.util.Map;
026    import java.util.zip.CRC32;
027    
028    import org.dom4j.Document;
029    import org.dom4j.DocumentException;
030    import org.dom4j.Element;
031    import org.dom4j.io.SAXReader;
032    import org.mactor.framework.MactorException;
033    import org.mactor.framework.spec.ProjectContext;
034    
035    public class XmlFileProjectTreeNode extends XmlProjectTreeNode {
036            File sourceFile;
037            boolean isConfigFile;
038            long checksum;
039            private XmlFileProjectTreeNode(ProjectNodeType nodeType, Element modelElement, Map<String, ProjectNodeType> nodeTypeDictionary) {
040                    super(nodeType, modelElement, nodeTypeDictionary);
041                    checksum = computeChecksum(modelElement.getDocument());
042            }
043            public static XmlProjectTreeNode buildFromFile(File sourceFile, Map<String, ProjectNodeType> nodeTypeDictionary, boolean isConfigFile) throws MactorException {
044                    try {
045                            return buildFromDocument(sourceFile, new SAXReader().read(new FileReader(sourceFile)), nodeTypeDictionary, isConfigFile);
046                    } catch (IOException ioe) {
047                            throw new MactorException(ioe);
048                    } catch (DocumentException de) {
049                            throw new MactorException(de);
050                    }
051            }
052            public static XmlProjectTreeNode buildFromDocument(File sourceFile, Document doc, Map<String, ProjectNodeType> nodeTypeDictionary, boolean isConfigFile) {
053                    Element element = doc.getRootElement();
054                    ProjectNodeType type = nodeTypeDictionary.get(element.getName());
055                    if (type == null)
056                            return null;
057                    XmlFileProjectTreeNode node = new XmlFileProjectTreeNode(type, element, nodeTypeDictionary);
058                    node.sourceFile = sourceFile;
059                    node.isConfigFile = isConfigFile;
060                    return node;
061            }
062            @Override
063            protected void model_delete() throws MactorException {
064                    ProjectContext.getGlobalInstance().deleteFile(sourceFile);
065                    if (ProjectNodeType.MBC_MESSAGE_BROKERS.equals(getNodeType()))
066                            ProjectContext.getGlobalInstance().setDirty(true);
067            }
068            @Override
069            protected boolean model_rename(String newName) throws MactorException {
070                    if (!newName.endsWith(".xml"))
071                            newName = newName + ".xml";
072                    this.sourceFile = ProjectContext.getGlobalInstance().renameFile(sourceFile, newName);
073                    return true;
074            }
075            @Override
076            protected String model_getName() {
077                    return sourceFile.getName();
078            }
079            @Override
080            protected ProjectTreeNode copy() throws MactorException {
081                    try {
082                            File newFile = ProjectContext.getGlobalInstance().duplicateFile(sourceFile);
083                            XmlProjectTreeNode newNode = buildFromDocument(newFile, new SAXReader().read(new FileReader(newFile)), super.nodeTypeDictionary, isConfigFile);
084                            if (ProjectNodeType.MBC_MESSAGE_BROKERS.equals(getNodeType()))
085                                    ProjectContext.getGlobalInstance().setDirty(true);
086                            return newNode;
087                    } catch (IOException ioe) {
088                            throw new MactorException(ioe);
089                    } catch (DocumentException de) {
090                            throw new MactorException(de);
091                    }
092            }
093            @Override
094            protected void model_save() throws MactorException {
095                    long newChecksum = computeChecksum(getModelElement().getDocument());
096                    if (newChecksum != checksum) {
097                            checksum = newChecksum;
098                            ProjectContext.getGlobalInstance().writeDocumentToFile(sourceFile, getModelElement().getDocument());
099                            if (ProjectNodeType.MBC_MESSAGE_BROKERS.equals(getNodeType()))
100                                    ProjectContext.getGlobalInstance().setDirty(true);
101                    }
102            }
103            @Override
104            protected void model_detatch() throws MactorException {
105                    ProjectContext.getGlobalInstance().deleteFile(sourceFile);
106                    if (ProjectNodeType.MBC_MESSAGE_BROKERS.equals(getNodeType()))
107                            ProjectContext.getGlobalInstance().setDirty(true);
108            }
109            private static long computeChecksum(Document doc) {
110                    CRC32 c = new CRC32();
111                    c.update(doc.asXML().getBytes());
112                    return c.getValue();
113            }
114    }