001: /*
002: * Copyright (C) 2001, 2002 Robert MacGrogan
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: *
019: * $Archive: SourceJammer$
020: * $FileName: XMLUtilFS.java$
021: * $FileID: 4426$
022: *
023: * Last change:
024: * $AuthorName: Rob MacGrogan$
025: * $Date: 4/23/03 5:17 PM$
026: * $Comment: $
027: */
028:
029: package org.sourcejammer.project.model.filesys;
030:
031: import org.w3c.dom.Document;
032: import org.w3c.dom.Element;
033: import org.sourcejammer.xml.XMLNodeDoesNotExistException;
034: import org.sourcejammer.util.AppConfig;
035: import org.sourcejammer.xml.XMLUtil;
036: import org.sourcejammer.project.controller.ControllerNode;
037: import org.sourcejammer.project.NodeList;
038: import org.sourcejammer.project.NodeIterator;
039: import org.sourcejammer.project.NodeExistsException;
040: import org.sourcejammer.util.ConfigurationException;
041: import org.sourcejammer.project.view.NodeInfo;
042: import org.sourcejammer.project.view.NodeName;
043: import org.sourcejammer.project.controller.Label;
044: import org.sourcejammer.project.controller.LabelVersionMappingBean;
045: import org.sourcejammer.util.SJDate;
046: import java.util.Vector;
047: import java.util.Enumeration;
048: import java.util.Iterator;
049: import java.util.Date;
050: import java.io.IOException;
051:
052: import javax.xml.parsers.DocumentBuilder;
053: import javax.xml.parsers.DocumentBuilderFactory;
054: import javax.xml.parsers.ParserConfigurationException;
055:
056: import org.sourcejammer.project.controller.NoSuchVersionException;
057: import org.sourcejammer.server.security.SecurityException;
058: import org.sourcejammer.util.SourceJammerVersionException;
059: import org.sourcejammer.project.controller.ProjectChild;
060: import org.sourcejammer.project.NodeDoesNotExistException;
061: import org.sourcejammer.project.model.FileAccessException;
062: import org.sourcejammer.server.ServerConfig;
063:
064: /**
065: * Title: $FileName: XMLUtilFS.java$
066: * @version $VerNum: 5$
067: * @author $AuthorName: Rob MacGrogan$<br><br>
068: *
069: * $Description: $<br>
070: * $KeyWordsOff: $<br><br>
071: *
072: * Static utility methods for turning FileSys node objects into
073: * XML doc objects.
074: */
075: public class XMLUtilFS {
076:
077: public static final class NodeNames {
078: public static final String NODE_ROOT = "SJFileSysNode";
079: public static final String NODE_NAME = "NodeName";
080: public static final String INFO = "Info";
081: public static final String NODE_TYPE = "NodeType";
082: public static final String CREATED_DATE = "CreatedDate";
083: public static final String FILE_NAME = "FileName";
084: public static final String COMMENT = "Comment";
085: public static final String LATEST_VERSION_FILENAME = "LatestVersionFileName";
086: public static final String IS_CHECKED_OUT = "IsCheckedOut";
087: public static final String CHECK_OUT_DIRECTORY = "CheckOutDirectory";
088: public static final String CHECKED_OUT_TO_USER = "CheckedOutToUser";
089: public static final String CHECK_OUT_DATE = "CheckOutDate";
090: public static final String CHILDREN = "Children";
091: public static final String REMOVED_CHILDREN = "RemovedChildren";
092: public static final String SJ_VERSION = "SJVersion";
093: public static final String CHILD = "Child";
094: public static final String COMMENT_FILE_NAME = "CommentFileName";
095: public static final String FILE_TYPE = "FileType";
096: public static final String SOURCE_SIZE = "SourceSize";
097: public static final String CREATED_USER = "CreatedUser";
098: public static final String UNIQUE_ID = "UniqueID";
099: public static final String PARENT_ID = "ParentID";
100: public static final String PARENTS = "Parents";
101: public static final String PARENT = "Parent";
102:
103: public static final String HISTORY_STORAGE_TYPE = "HistoryStorageType";
104: public static final String IS_LIMIT_HISTORY_SIZE = "IsLimitHistorySize";
105: public static final String MAX_HISTORY_SIZE = "MaxHistorySize";
106:
107: //Label nodes
108: public static final String LABEL_LIST = "LabelList";
109: public static final String LABEL = "Label";
110: public static final String MAPPED_VERSION_LIST = "MappedVersionList";
111: public static final String MAPPED_VERSION = "MappedVersion";
112: public static final String SOURCEJAMMER_PATH = "SJPath";
113: public static final String VERSION = "Version";
114: public static final String ROOT_LABEL_PROJECT_PATH = "LabelRootProjectSJPath";
115: public static final String ROOT_PROJECT_UNIQUE_ID = "RootProjectUniqueID";
116: public static final String VERSION_UNIQUE_ID = "VersionUniqueID";
117: public static final String FILE_UNIQUE_ID = "FileUniqueID";
118: }
119:
120: public static final class Attributes {
121: public static final String UNIQUE_ID = "id";
122: }
123:
124: private XMLUtilFS() {
125: }
126:
127: /**
128: * Parses a label xml docuemnt into an org.sourcejammer.project.controller.Label.
129: */
130: public static Label xmlToLabel(Document doc)
131: throws XMLNodeDoesNotExistException,
132: SourceJammerVersionException {
133: Label oLabel = new Label();
134:
135: Element elmRoot = doc.getDocumentElement();
136: oLabel.setNodeName(XMLUtil.getValue(NodeNames.NODE_NAME,
137: elmRoot));
138: String sRootProjUniqueID = XMLUtil.getValue(
139: NodeNames.ROOT_PROJECT_UNIQUE_ID, elmRoot);
140: long lRootProjUniqueID = -1;
141: if (sRootProjUniqueID == null) {
142: throw new SourceJammerVersionException(
143: "Previous version of label xml found.");
144: }
145:
146: oLabel
147: .setRootProjectUniqueID(Long
148: .parseLong(sRootProjUniqueID));
149:
150: Element elmMappedVersions = XMLUtil.getChildElement(
151: NodeNames.MAPPED_VERSION_LIST, elmRoot);
152: org.w3c.dom.NodeList oChildren = elmMappedVersions
153: .getChildNodes();
154: for (int iCounter = 0; iCounter < oChildren.getLength(); iCounter++) {
155: try {
156: org.w3c.dom.Node oNode = oChildren.item(iCounter);
157: if (oNode instanceof Element) {
158: Element elmMappedVersion = (Element) oNode;
159:
160: LabelVersionMappingBean bn = new LabelVersionMappingBean();
161: bn.setFullPathToFile(XMLUtil.getValue(
162: NodeNames.SOURCEJAMMER_PATH,
163: elmMappedVersion));
164: String sVersionNumber = XMLUtil.getValue(
165: NodeNames.VERSION, elmMappedVersion);
166: int iVersionNumber = Integer
167: .parseInt(sVersionNumber.trim());
168: bn.setVersionNumber(iVersionNumber);
169: String sVerUniqueID = XMLUtil.getValue(
170: NodeNames.VERSION_UNIQUE_ID,
171: elmMappedVersion);
172: long lVerUniqueID = Long.parseLong(sVerUniqueID);
173: bn.setVersionUniqueID(lVerUniqueID);
174: String sFileUniqueID = XMLUtil.getValue(
175: NodeNames.FILE_UNIQUE_ID, elmMappedVersion);
176: long lFileUniqueID = Long.parseLong(sFileUniqueID);
177: bn.setFileUniqueID(lFileUniqueID);
178:
179: oLabel.addLabelVersionMappingBean(bn);
180: }
181: } catch (NumberFormatException ex) {
182: throw new ConfigurationException(
183: "Version number cannot be parsed to an int");
184: }
185: }//end for
186:
187: return oLabel;
188:
189: }
190:
191: public static Document labelToXML(Label label) {
192: Document docLabel = XMLUtil.newDocument();
193: Element elmRoot = XMLUtil.addRootElement(NodeNames.LABEL,
194: docLabel);
195: XMLUtil.addNewChildElement(NodeNames.SJ_VERSION, ServerConfig
196: .getInstance().getServerVersion(), elmRoot);
197: XMLUtil.addNewChildElement(NodeNames.NODE_NAME, label
198: .getNodeName(), elmRoot);
199: XMLUtil.addNewChildElement(NodeNames.ROOT_PROJECT_UNIQUE_ID,
200: Long.toString(label.getRootProjectUniqueID()), elmRoot);
201:
202: Element elmMappedVersionList = XMLUtil.addNewChildElement(
203: NodeNames.MAPPED_VERSION_LIST, elmRoot);
204:
205: Iterator iter = label.getVersions();
206: int iCount = 0;
207: while (iter.hasNext()) {
208: iCount++;
209:
210: LabelVersionMappingBean bnVersion = (LabelVersionMappingBean) iter
211: .next();
212: Element elmVersion = XMLUtil.addNewChildElement(
213: NodeNames.MAPPED_VERSION, elmMappedVersionList);
214:
215: XMLUtil.addNewChildElement(NodeNames.SOURCEJAMMER_PATH,
216: bnVersion.getFullPathToFile(), elmVersion);
217: XMLUtil
218: .addNewChildElement(NodeNames.VERSION, Integer
219: .toString(bnVersion.getVersionNumber()),
220: elmVersion);
221: XMLUtil.addNewChildElement(NodeNames.VERSION_UNIQUE_ID,
222: Long.toString(bnVersion.getVersionUniqueID()),
223: elmVersion);
224: XMLUtil.addNewChildElement(NodeNames.FILE_UNIQUE_ID, Long
225: .toString(bnVersion.getFileUniqueID()), elmVersion);
226: }//end while
227: return docLabel;
228: }
229:
230: public static String xmlToNodeName(Document doc)
231: throws XMLNodeDoesNotExistException {
232: Element elmInfo = XMLUtil.getChildElement(NodeNames.INFO, doc
233: .getDocumentElement());
234: String sNodeName = elmInfo.getAttribute(NodeNames.NODE_NAME);
235: return sNodeName;
236: }
237:
238: /**
239: * Builds ProjectNode. ProjectNode should be locked and key passed in.
240: */
241: public static ProjectNodeFS xmlToProject(Document doc,
242: ProjectNodeFS ndProject, long lKey, NodeLibraryFS lib)
243: throws XMLNodeDoesNotExistException, SecurityException,
244: NodeDoesNotExistException, FileAccessException {
245:
246: ProjectNodeDeserializer deser = new ProjectNodeDeserializer(doc);
247: deser.setKey(lKey);
248: deser.setProject(ndProject);
249: deser.setLibrary(lib);
250:
251: return (ProjectNodeFS) deser.deserialize();
252: }
253:
254: /**
255: * Build a FileNodeFS from the file node's XML doc. File node should already be
256: * locked and key should be passed in.
257: */
258: public static FileNodeFS xmlToFileNode(Document doc,
259: FileNodeFS ndFile, long lKey)
260: throws XMLNodeDoesNotExistException, SecurityException {
261:
262: FileNodeDeserializer deserializer = new FileNodeDeserializer(
263: doc);
264: deserializer.setFile(ndFile);
265: deserializer.setKey(lKey);
266: return (FileNodeFS) deserializer.deserialize();
267: }
268:
269: /**
270: * Turn a ProjectNodeFS object into an XML Doc for storage
271: * on file system.
272: */
273: public static Document projectNodeToXML(ProjectNodeFS project) {
274: ProjectNodeSerializer serializer = new ProjectNodeSerializer(
275: project);
276: return serializer.serialize();
277: }
278:
279: /**
280: * Turn a FileNodeFS object into an XML Doc for storage
281: * on file system.
282: */
283: public static Document fileNodeToXML(FileNodeFS file) {
284: FileNodeSerializer serialzer = new FileNodeSerializer(file);
285: return serialzer.serialize();
286: }
287:
288: }
|