1 52 53 package freemarker.ext.xml; 54 import freemarker.template.TemplateMethodModel; 55 import java.lang.String ; 56 import java.util.List ; 57 import freemarker.template.TemplateScalarModel; 58 import freemarker.template.TemplateModelException; 59 import java.util.HashMap ; 60 61 65 class Namespaces 66 implements 67 TemplateMethodModel, 68 Cloneable 69 { 70 static final Factory FACTORY = new Factory() 71 { 72 public Namespaces create() 73 { 74 return new Namespaces(); 75 } 76 }; 77 78 private HashMap namespaces; 79 private boolean shared; 80 81 Namespaces() { 82 namespaces = new HashMap (); 83 namespaces.put("", ""); 84 namespaces.put("xml", "http://www.w3.org/XML/1998/namespace"); 85 shared = false; 86 } 87 88 public Object clone() { 89 try { 90 Namespaces clone = (Namespaces)super.clone(); 91 clone.namespaces = (HashMap )namespaces.clone(); 92 clone.shared = false; 93 return clone; 94 } 95 catch(CloneNotSupportedException e) { 96 throw new Error (); } 98 } 99 100 public String translateNamespacePrefixToUri(String prefix) { 101 synchronized(namespaces) { 102 return (String )namespaces.get(prefix); 103 } 104 } 105 106 public Object exec(List arguments) throws TemplateModelException { 107 if (arguments.size() != 2) { 108 throw new TemplateModelException("_registerNamespace(prefix, uri) requires two arguments"); 109 } 110 registerNamespace((String )arguments.get(0), (String )arguments.get(1)); 111 return TemplateScalarModel.EMPTY_STRING; 112 } 113 114 void registerNamespace(String prefix, String uri) { 115 synchronized(namespaces) { 116 namespaces.put(prefix, uri); 117 } 118 } 119 120 void markShared() { 121 if(!shared) { 122 shared = true; 123 } 124 } 125 126 boolean isShared() { 127 return shared; 128 } 129 130 interface Factory 131 { 132 Namespaces create(); 133 } 134 135 136 } | Popular Tags |