1 /**
  2  * Copyright (C) 2012-2013 KO GmbH <copyright@kogmbh.com>
  3  *
  4  * @licstart
  5  * This file is part of WebODF.
  6  *
  7  * WebODF is free software: you can redistribute it and/or modify it
  8  * under the terms of the GNU Affero General Public License (GNU AGPL)
  9  * as published by the Free Software Foundation, either version 3 of
 10  * the License, or (at your option) any later version.
 11  *
 12  * WebODF is distributed in the hope that it will be useful, but
 13  * WITHOUT ANY WARRANTY; without even the implied warranty of
 14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 15  * GNU Affero General Public License for more details.
 16  *
 17  * You should have received a copy of the GNU Affero General Public License
 18  * along with WebODF.  If not, see <http://www.gnu.org/licenses/>.
 19  * @licend
 20  *
 21  * @source: http://www.webodf.org/
 22  * @source: https://github.com/kogmbh/WebODF/
 23  */
 24 
 25 /*global odf*/
 26 
 27 /**
 28  * Singleton object which provides namespace ids and
 29  * some utility methods related to prefixes and namespaces
 30  * @const
 31  */
 32 odf.Namespaces = {
 33     namespaceMap: {
 34         db: "urn:oasis:names:tc:opendocument:xmlns:database:1.0",
 35         dc: "http://purl.org/dc/elements/1.1/",
 36         dr3d: "urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0",
 37         draw: "urn:oasis:names:tc:opendocument:xmlns:drawing:1.0",
 38         chart: "urn:oasis:names:tc:opendocument:xmlns:chart:1.0",
 39         fo: "urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0",
 40         form: "urn:oasis:names:tc:opendocument:xmlns:form:1.0",
 41         meta: "urn:oasis:names:tc:opendocument:xmlns:meta:1.0",
 42         number: "urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0",
 43         office: "urn:oasis:names:tc:opendocument:xmlns:office:1.0",
 44         presentation: "urn:oasis:names:tc:opendocument:xmlns:presentation:1.0",
 45         style: "urn:oasis:names:tc:opendocument:xmlns:style:1.0",
 46         svg: "urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0",
 47         table: "urn:oasis:names:tc:opendocument:xmlns:table:1.0",
 48         text: "urn:oasis:names:tc:opendocument:xmlns:text:1.0",
 49         xlink: 'http://www.w3.org/1999/xlink',
 50         xml: "http://www.w3.org/XML/1998/namespace"
 51     },
 52     /**@type{!Object.<string,string>}*/
 53     prefixMap: {},
 54     dbns: "urn:oasis:names:tc:opendocument:xmlns:database:1.0",
 55     dcns: "http://purl.org/dc/elements/1.1/",
 56     dr3dns: "urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0",
 57     drawns: "urn:oasis:names:tc:opendocument:xmlns:drawing:1.0",
 58     chartns: "urn:oasis:names:tc:opendocument:xmlns:chart:1.0",
 59     fons: "urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0",
 60     formns: "urn:oasis:names:tc:opendocument:xmlns:form:1.0",
 61     metans: "urn:oasis:names:tc:opendocument:xmlns:meta:1.0",
 62     numberns: "urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0",
 63     /**
 64      * @const
 65      * @type{string}
 66      */
 67     officens: "urn:oasis:names:tc:opendocument:xmlns:office:1.0",
 68     presentationns: "urn:oasis:names:tc:opendocument:xmlns:presentation:1.0",
 69     stylens: "urn:oasis:names:tc:opendocument:xmlns:style:1.0",
 70     svgns: "urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0",
 71     tablens: "urn:oasis:names:tc:opendocument:xmlns:table:1.0",
 72     textns: "urn:oasis:names:tc:opendocument:xmlns:text:1.0",
 73     xlinkns: 'http://www.w3.org/1999/xlink',
 74     xmlns: "http://www.w3.org/XML/1998/namespace"
 75 };
 76 
 77 (function () {
 78     "use strict";
 79     // map namespacemap to prefix map on startup
 80     var map = odf.Namespaces.namespaceMap,
 81         /**@type{!Object.<string,string>}*/
 82         pmap = odf.Namespaces.prefixMap,
 83         /**@type{string}*/
 84         prefix;
 85 
 86     for (prefix in map) {
 87         if (map.hasOwnProperty(prefix)) {
 88             pmap[map[prefix]] = prefix;
 89         }
 90     }
 91 }());
 92 
 93 /**
 94  * Calls the passed callback for all pairs of prefix and namespace
 95  * which are in the namespaceMap property
 96  * @param {function(string,string):undefined} cb
 97  * @return {undefined}
 98  */
 99 odf.Namespaces.forEachPrefix = function forEachPrefix(cb) {
100     "use strict";
101     var /**@type{!Object.<string,string>}*/
102         ns = odf.Namespaces.namespaceMap,
103         /**@type{string}*/
104         prefix;
105 
106     for (prefix in ns) {
107         if (ns.hasOwnProperty(prefix)) {
108             cb(prefix, ns[prefix]);
109         }
110     }
111 };
112 
113 /**
114  * Returns the namespace belonging to the prefix or null.
115  * @param {string} prefix
116  * @return {?string}
117  */
118 odf.Namespaces.lookupNamespaceURI = function lookupNamespaceURI(prefix) {
119     "use strict";
120     var /**@type{?string}*/
121         r = null;
122     if (odf.Namespaces.namespaceMap.hasOwnProperty(prefix)) {
123         r = /**@type{string}*/(odf.Namespaces.namespaceMap[prefix]);
124     }
125     return r;
126 };
127 
128 /**
129  * Returns the prefix belonging to the NamespaceURI or null.
130  * @param {string} namespaceURI
131  * @return {?string}
132  */
133 odf.Namespaces.lookupPrefix = function lookupPrefix(namespaceURI) {
134     "use strict";
135     var /**@type{!Object.<string,string>}*/
136         map = odf.Namespaces.prefixMap;
137     return map.hasOwnProperty(namespaceURI) ? map[namespaceURI] : null;
138 };
139 
140 // TODO: document where and why this is needed
141 odf.Namespaces.lookupNamespaceURI.lookupNamespaceURI = odf.Namespaces.lookupNamespaceURI;
142 
143