1 52 53 package freemarker.core; 54 55 import java.io.IOException ; 56 57 import freemarker.template.Template; 58 import freemarker.template.TemplateException; 59 60 61 65 public final class LibraryLoad extends TemplateElement { 66 67 private Expression templateName; 68 private String namespace; 69 private final String templatePath; 70 71 76 LibraryLoad(Template template, 77 Expression templateName, 78 String namespace) 79 { 80 this.namespace = namespace; 81 String templatePath1 = template.getName(); 82 int lastSlash = templatePath1.lastIndexOf('/'); 83 templatePath = lastSlash == -1 ? "" : templatePath1.substring(0, lastSlash + 1); 84 this.templateName = templateName; 85 } 86 87 void accept(Environment env) throws TemplateException, IOException { 88 String templateNameString = templateName.getStringValue(env); 89 if( templateNameString == null ) { 90 String msg = "Error " + getStartLocation() 91 + "The expression " + templateName + " is undefined."; 92 throw new InvalidReferenceException(msg, env); 93 } 94 Template importedTemplate; 95 try { 96 if(!env.isClassicCompatible()) { 97 if (templateNameString.indexOf("://") >0) { 98 ; 99 } 100 else if(templateNameString.length() > 0 && templateNameString.charAt(0) == '/') { 101 int protIndex = templatePath.indexOf("://"); 102 if (protIndex >0) { 103 templateNameString = templatePath.substring(0, protIndex + 2) + templateNameString; 104 } else { 105 templateNameString = templateNameString.substring(1); 106 } 107 } 108 else { 109 templateNameString = templatePath + templateNameString; 110 } 111 } 112 importedTemplate = env.getTemplateForImporting(templateNameString); 113 } 114 catch (ParseException pe) { 115 String msg = "Error parsing imported template " 116 + templateNameString; 117 throw new TemplateException(msg, pe, env); 118 } 119 catch (IOException ioe) { 120 String msg = "Error reading imported file " 121 + templateNameString; 122 throw new TemplateException(msg, ioe, env); 123 } 124 env.importLib(importedTemplate, namespace); 125 } 126 127 public String getCanonicalForm() { 128 StringBuffer buf = new StringBuffer ("<#import "); 129 buf.append(templateName); 130 buf.append(" as "); 131 buf.append(namespace); 132 buf.append("/>"); 133 return buf.toString(); 134 } 135 136 public String getDescription() { 137 return "import " + templateName + " as " + namespace; 138 } 139 140 public String getTemplateName() { 141 return templateName.toString(); 142 } 143 } 144 | Popular Tags |