1 16 17 package scriptella.configuration; 18 19 import org.w3c.dom.Element ; 20 import org.w3c.dom.Node ; 21 import scriptella.spi.DialectIdentifier; 22 import scriptella.spi.Resource; 23 import scriptella.util.StringUtils; 24 25 import java.util.ArrayList ; 26 import java.util.List ; 27 import java.util.regex.Pattern ; 28 29 44 public class DialectBasedContentEl extends XmlConfigurableBase { 45 private List <Dialect> dialects; 46 47 public DialectBasedContentEl() { 48 } 49 50 public DialectBasedContentEl(final XmlElement element) { 51 configure(element); 52 } 53 54 public void configure(final XmlElement element) { 55 Dialect defaultDialect = null; 56 dialects = new ArrayList <Dialect>(); 57 for (Node node = element.getElement().getFirstChild(); node != null; node = node.getNextSibling()) { 59 if (isDialectElement(node)) { 60 if (defaultDialect != null) { 61 dialects.add(defaultDialect); 62 defaultDialect = null; 63 } 64 Dialect d = new Dialect(); 65 d.configure(new XmlElement((Element ) node, element)); 66 dialects.add(d); 67 } else { 68 Resource resource = ContentEl.asResource(element, node); 70 if (resource != null) { 72 if (defaultDialect == null) { 74 defaultDialect = new Dialect(); 76 defaultDialect.configureDefault(element); 77 } 78 defaultDialect.contentEl.append(resource); 80 } 81 } 82 } 83 if (defaultDialect != null) { 84 dialects.add(defaultDialect); } 86 87 } 88 89 private static boolean isDialectElement(Node node) { 90 return node instanceof Element && "dialect".equals(node.getNodeName()); 91 } 92 93 99 public Resource getContent(final DialectIdentifier id) { 100 ContentEl result = null; 101 for (Dialect d : dialects) { 102 if (d.matches(id)) { 103 if (result == null) { 104 result = new ContentEl(); 105 result.setLocation(getLocation()); 106 } 107 result.merge(d.getContentEl()); 108 } 109 } 110 return result; 111 } 112 113 114 119 List <Dialect> getDialects() { 120 return dialects; 121 } 122 123 static class Dialect extends XmlConfigurableBase { 124 private Pattern name; 125 private Pattern version; 126 private ContentEl contentEl; 127 128 public Pattern getName() { 129 return name; 130 } 131 132 public void setName(final Pattern name) { 133 this.name = name; 134 } 135 136 public Pattern getVersion() { 137 return version; 138 } 139 140 public void setVersion(final Pattern version) { 141 this.version = version; 142 } 143 144 public ContentEl getContentEl() { 145 return contentEl; 146 } 147 148 public void configure(final XmlElement element) { 149 setPatternProperty(element, "name"); 150 setPatternProperty(element, "version"); 151 contentEl = new ContentEl(element); 152 setLocation(element); 153 } 154 155 160 public void configureDefault(final XmlElement parent) { 161 setLocation(parent); 162 contentEl = new ContentEl(); 163 } 164 165 166 boolean matches(final DialectIdentifier id) { 167 if (id == null) { return name == null && version == null; 170 } 171 172 String idName = StringUtils.nullsafeToString(id.getName()); 173 String idVersion = StringUtils.nullsafeToString(id.getVersion()); 174 175 return ((name == null) || 176 name.matcher(idName).matches()) && ((version == null) || 177 version.matcher(idVersion).matches()); 178 179 } 180 181 public String toString() { 182 return "Dialect{" + "name=" + name + ", version=" + 183 version + ", contentEl=" + contentEl + "}"; 184 } 185 } 186 187 } 188 | Popular Tags |