1 if (Garmin == undefined) var Garmin = {};
  2 /**
  3  * Copyright � 2007 Garmin Ltd. or its subsidiaries.
  4  *
  5  * Licensed under the Apache License, Version 2.0 (the 'License')
  6  * you may not use this file except in compliance with the License.
  7  * You may obtain a copy of the License at
  8  *
  9  *    http://www.apache.org/licenses/LICENSE-2.0
 10  *
 11  * Unless required by applicable law or agreed to in writing, software
 12  * distributed under the License is distributed on an 'AS IS' BASIS,
 13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  * See the License for the specific language governing permissions and
 15  * limitations under the License.
 16  * 
 17  * @fileoverview Garmin.DirectoryFactory - A factory for producing directory objects.
 18  * 
 19  * @author Diana Chow diana.chow at garmin.com
 20  * @version 1.0
 21  */
 22 /**A factory that can produce a Garmin.Directory object given directory xml.  Currently
 23  * this is for FIT support, but this may be extended later for other types.
 24  * @class Garmin.DirectoryFactory
 25  * @constructor 
 26  * @requires Garmin.File, Garmin.FileId
 27  */
 28 Garmin.DirectoryFactory = function(){};
 29 Garmin.DirectoryFactory = {
 30 	
 31 	parseString: function(xmlString) {
 32 		var dirDocument = Garmin.XmlConverter.toDocument(xmlString);		
 33 		return Garmin.DirectoryFactory.parseDocument(dirDocument);		
 34 	},
 35 	
 36 	/* Creates and returns a list of all FIT files from the document, regardless of type. */
 37 	parseDocument: function(dirDocument) {
 38 		
 39 		// Not parseable directory doc
 40 		if( dirDocument.getElementsByTagName(Garmin.DirectoryFactory.SCHEMA_TAGS.directoryListing).length == 0) {
 41 			throw new Error("ERROR: Unable to parse directory document.");
 42 		}
 43 		
 44 		var parsedDocument;
 45 		
 46 		// Files		
 47 		if( dirDocument.getElementsByTagName(Garmin.DirectoryFactory.SCHEMA_TAGS.file).length >= 0) {
 48 			// Complete file
 49 			parsedDocument = Garmin.DirectoryFactory._parseFiles(dirDocument);
 50 		} 
 51 		
 52 		return parsedDocument;
 53 	},
 54 	
 55 	produceString: function(directory) {
 56 		// TODO fill this guy out when we need it
 57 	},
 58 	
 59 	_parseFiles: function(dirDocument) {
 60 		var files = new Array();
 61 		var fileNodes;
 62 
 63 		// Grab the activity/course nodes, depending on document		
 64 		fileNodes = dirDocument.getElementsByTagName(Garmin.DirectoryFactory.SCHEMA_TAGS.file);
 65 		
 66 		// loop through all files in the document
 67 		for (var i = 0; i < fileNodes.length; i++) {
 68 			
 69 			// create new file object
 70 			var file = Garmin.DirectoryFactory._parseFile(fileNodes[i], Garmin.DirectoryFactory.SCHEMA_TAGS.file);
 71 			
 72 			// Add the populated file to the list of files.
 73 			files.push(file);
 74 		}
 75 		
 76 		return files;
 77 	},
 78 	
 79 	_parseFile: function(fileNode, fileType) {
 80 		// create new activity object
 81 		var file = new Garmin.File();
 82 		
 83 		// set is directory
 84 		file.setAttribute(Garmin.File.ATTRIBUTE_KEYS.isDirectory, fileNode.getAttribute(Garmin.File.ATTRIBUTE_KEYS.isDirectory));
 85 		
 86 		// set path
 87 		file.setAttribute(Garmin.File.ATTRIBUTE_KEYS.path, fileNode.getAttribute(Garmin.File.ATTRIBUTE_KEYS.path));
 88 		
 89 		// set type
 90 		file.setAttribute(Garmin.File.ATTRIBUTE_KEYS.type, fileNode.getAttribute(Garmin.File.ATTRIBUTE_KEYS.type));
 91 
 92 		// set creation time, optional.
 93 		var creationTimeNode = fileNode.getElementsByTagName(Garmin.File.ATTRIBUTE_KEYS.creationTime)[0];
 94 		if( creationTimeNode != null ) {
 95     		var creationTime = creationTimeNode.childNodes[0].nodeValue;
 96     		var creationTimeObj = (new Garmin.DateTimeFormat()).parseXsdDateTime(creationTime);
 97     		file.setAttribute(Garmin.File.ATTRIBUTE_KEYS.creationTime, creationTimeObj);
 98 		}
 99 
100         // set dom
101 		file.setAttribute(Garmin.File.ATTRIBUTE_KEYS.dom, fileNode);
102 		
103 		// set id - only one id per file	
104 		// TODO Will there be other id types in the future? probably... 	
105 		var fitIdNode = fileNode.getElementsByTagName(Garmin.DirectoryFactory.SCHEMA_TAGS.fitId)[0];
106 		var fitId = Garmin.DirectoryFactory._parseFitId(fitIdNode);
107 		file.setId(fitId);
108 		
109 		return file;
110 	},
111 	
112 	_parseFitId: function(fitIdNode) {
113 	    var fitId = new Garmin.FileId();
114 	     
115 		// set id
116 		var id = Garmin.DirectoryFactory._tagValue(fitIdNode, Garmin.DirectoryFactory.SCHEMA_TAGS.id);
117 		if( id != null ) {
118 		  fitId.setValue(Garmin.DirectoryFactory.SCHEMA_TAGS.id, id);
119 		}
120         // set filetype
121 		var fileType = Garmin.DirectoryFactory._tagValue(fitIdNode, Garmin.DirectoryFactory.SCHEMA_TAGS.fileType);
122 		if( fileType != null ) {
123 		  fitId.setValue(Garmin.DirectoryFactory.SCHEMA_TAGS.fileType, fileType);
124 		}
125         // set manufacturer
126 		var manufacturer = Garmin.DirectoryFactory._tagValue(fitIdNode, Garmin.DirectoryFactory.SCHEMA_TAGS.manufacturer);
127 		if( manufacturer != null ) {
128 		  fitId.setValue(Garmin.DirectoryFactory.SCHEMA_TAGS.manufacturer, manufacturer);
129 		}
130         // set product
131 		var product = Garmin.DirectoryFactory._tagValue(fitIdNode, Garmin.DirectoryFactory.SCHEMA_TAGS.product);
132 		if( product != null ) {
133 		  fitId.setValue(Garmin.DirectoryFactory.SCHEMA_TAGS.product, product);
134 		}
135         // set serial number
136 		var serialNumber= Garmin.DirectoryFactory._tagValue(fitIdNode, Garmin.DirectoryFactory.SCHEMA_TAGS.serialNumber);
137 		if( serialNumber != null ) {
138 		  fitId.setValue(Garmin.DirectoryFactory.SCHEMA_TAGS.serialNumber, serialNumber);
139 		}
140 		
141 		return fitId;
142 	},
143 	
144 	/**
145 	 * Takes in a list of any file type and returns a list of only activity file types.
146 	 * @param files {Array} list of Garmin.File objects of any type produced from the factory 
147 	 */
148 	getActivityFiles: function(files) {
149 	    var activityFiles = new Array();
150 	    
151 	    for(var i=0; i < files.length; i++) {
152 	        if( files[i].getIdValue(Garmin.FileId.KEYS.fileType) == Garmin.FileId.FILE_TYPE_MAP.activities){
153 	            activityFiles.push(files[i]);
154 	        }
155 	    }
156 	    
157 	    return activityFiles;
158 	},
159 	
160 	/**
161 	 * Gets the first tag's value under the parent.
162 	 */
163 	_tagValue: function(parentNode, tagName) {
164 		var subNode = parentNode.getElementsByTagName(tagName);
165 		return subNode.length > 0 ? subNode[0].childNodes[0].nodeValue : null;
166 	},	
167 	
168     toString: function() {
169         return "[DirectoryFactory]";
170     }	
171 };
172 
173 Garmin.DirectoryFactory.SCHEMA_TAGS = {
174 	directoryListing:			"DirectoryListing",
175 	file:                       "File",
176 	unitId:                     "UnitId",
177 	fitId:                      "FitId",
178 	path:                       "Path",
179 	type:                       "Type",
180 	id:                         "Id",
181 	fileType:                   "FileType",
182     manufacturer:               "Manufacturer",
183     product:                    "Product",
184     serialNumber:               "SerialNumber",
185     isDirectory:                "IsDirectory"
186 };