1 16 package scriptella.configuration; 17 18 import scriptella.spi.ParametersCallback; 19 20 import java.net.URL ; 21 import java.util.HashSet ; 22 import java.util.List ; 23 import java.util.Map ; 24 import java.util.Set ; 25 26 27 33 public class ConfigurationEl extends XmlConfigurableBase { 34 private List <ConnectionEl> connections; 35 private List <ScriptingElement> scriptingElements; 36 private PropertiesMerger propertiesMerger; 37 private URL documentUrl; 38 39 public ConfigurationEl(XmlElement element, PropertiesMerger merger) { 40 propertiesMerger = merger; 41 configure(element); 42 } 43 44 public List <ConnectionEl> getConnections() { 45 return connections; 46 } 47 48 public void setConnections(final List <ConnectionEl> connections) { 49 this.connections = connections; 50 } 51 52 public List <ScriptingElement> getScriptingElements() { 53 return scriptingElements; 54 } 55 56 public void setScriptingElements(final List <ScriptingElement> scriptingElements) { 57 this.scriptingElements = scriptingElements; 58 } 59 60 63 public ParametersCallback getProperties() { 64 return propertiesMerger; 65 } 66 67 public URL getDocumentUrl() { 68 return documentUrl; 69 } 70 71 public void setDocumentUrl(final URL documentUrl) { 72 this.documentUrl = documentUrl; 73 } 74 75 public void configure(final XmlElement element) { 76 documentUrl = element.getDocumentUrl(); 77 78 Map <String ,?> xmlProps = new PropertiesEl(element.getChild("properties")).getMap(); 79 propertiesMerger.addProperties(xmlProps); 81 82 setConnections(load(element.getChildren("connection"), 83 ConnectionEl.class)); 84 if (connections.isEmpty()) { 85 throw new ConfigurationException("At least one connection element must be declared", element); 86 } 87 scriptingElements = QueryEl.loadScriptingElements(element, null); 88 validateScriptingElements(element); 89 } 90 91 void validateScriptingElements(final XmlElement element) { 92 Set <String > allowedConIds = new HashSet <String >(); 94 for (ConnectionEl connectionEl : connections) { 95 final String cid = connectionEl.getId(); 96 if (!allowedConIds.add(cid)) { 97 throw new ConfigurationException("Connection ID must be unique for ETL file", element); 98 } 99 if (cid==null && connections.size()>1) { 100 throw new ConfigurationException("Connection ID is required if more than one connection specified in ETL script.", element); 101 } 102 } 103 104 validateScriptingElements(allowedConIds, element, scriptingElements); 105 } 106 107 void validateScriptingElements(final Set <String > allowedConIds, final XmlElement element, final List <ScriptingElement> elements) { 108 for (ScriptingElement se : elements) { 109 final int allowedConSize = allowedConIds.size(); 111 final String seConnectionId = se.getConnectionId(); 112 if (allowedConSize == 1 && seConnectionId != null && 113 !allowedConIds.contains(seConnectionId)) { 114 throw new ConfigurationException("Element " + se.getLocation() + " has invalid connection-id"); 115 } else if (allowedConSize > 1 && seConnectionId == null && scriptingElements==elements) { 116 throw new ConfigurationException("connection-id is a required attribute for element " + se.getLocation()); 118 } else 119 if (allowedConSize > 1 && seConnectionId != null && !allowedConIds.contains(seConnectionId)) 120 { 121 throw new ConfigurationException("Element " + se.getLocation() + " has invalid connection-id"); 122 } 123 if (se instanceof QueryEl) { 124 validateScriptingElements(allowedConIds, element, ((QueryEl) se).getChildScriptinglElements()); 125 } 126 } 127 } 128 129 public String toString() { 130 return "ConfigurationEl{" + "connections=" + connections + 131 ", scriptingElements=" + scriptingElements + ", properties=" + propertiesMerger + 132 ", documentUrl=" + documentUrl + "}"; 133 } 134 } 135 | Popular Tags |