InternalLoader.swift 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. *
  3. * Copyright 2017, Google Inc.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of Google Inc. nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. import Stencil
  34. import Foundation
  35. // A class for loading Stencil templates from compiled-in representations
  36. public class InternalLoader: Loader {
  37. private var templates: [String:String]
  38. public init() {
  39. self.templates = loadTemplates()
  40. }
  41. public func loadTemplate(name: String, environment: Environment) throws -> Template {
  42. if let encoding = templates[name],
  43. let data = Data(base64Encoded: encoding, options:[]),
  44. let template = String(data:data, encoding:.utf8) {
  45. return environment.templateClass.init(templateString: template,
  46. environment: environment,
  47. name: name)
  48. } else {
  49. throw TemplateDoesNotExist(templateNames: [name], loader: self)
  50. }
  51. }
  52. public func loadTemplate(names: [String], environment: Environment) throws -> Template {
  53. for name in names {
  54. if let encoding = templates[name],
  55. let data = Data(base64Encoded: encoding, options:[]),
  56. let template = String(data:data, encoding:.utf8) {
  57. return environment.templateClass.init(templateString: template,
  58. environment: environment,
  59. name: name)
  60. }
  61. }
  62. throw TemplateDoesNotExist(templateNames: names, loader: self)
  63. }
  64. }