1 56 57 package org.objectstyle.cayenne.conf; 58 59 import java.io.PrintWriter ; 60 import java.util.Iterator ; 61 62 import org.objectstyle.cayenne.conn.DataSourceInfo; 63 import org.objectstyle.cayenne.project.Project; 64 import org.objectstyle.cayenne.util.Util; 65 66 71 public class ConfigSaver { 72 protected ConfigSaverDelegate delegate; 73 74 77 public ConfigSaver() { 78 super(); 79 } 80 81 84 public ConfigSaver(ConfigSaverDelegate delegate) { 85 this.delegate = delegate; 86 } 87 88 92 public void storeDomains(PrintWriter pw) { 93 pw.println("<?xml version=\"1.0\" encoding=\"utf-8\"?>"); 94 pw.println("<domains project-version=\"" + delegate.projectVersion() + "\">"); 95 96 Iterator it = delegate.domainNames(); 97 while (it.hasNext()) { 98 storeDomain(pw, (String ) it.next()); 99 } 100 101 Iterator views = delegate.viewNames(); 102 while (views.hasNext()) { 103 storeDataView(pw, (String ) views.next()); 104 } 105 pw.println("</domains>"); 106 } 107 108 protected void storeDataView(PrintWriter pw, String dataViewName) { 109 String location = delegate.viewLocation(dataViewName); 110 pw.print("<view name=\"" + dataViewName.trim()); 111 pw.print("\" location=\"" + location.trim()); 112 pw.println("\"/>"); 113 } 114 115 protected void storeDomain(PrintWriter pw, String domainName) { 116 pw.println("<domain name=\"" + domainName.trim() + "\">"); 117 118 Iterator properties = delegate.propertyNames(domainName); 120 boolean breakNeeded = properties.hasNext(); 121 while (properties.hasNext()) { 122 String name = (String ) properties.next(); 123 if (name == null) { 124 continue; 125 } 126 127 String value = delegate.propertyValue(domainName, name); 128 if (value == null) { 129 continue; 130 } 131 132 pw.print("\t<property name=\"" + Util.encodeXmlAttribute(name.trim())); 133 pw.println("\" value=\"" + Util.encodeXmlAttribute(value.trim()) + "\"/>"); 134 } 135 136 Iterator maps = delegate.mapNames(domainName); 138 if (maps.hasNext()) { 139 if (breakNeeded) { 140 pw.println(); 141 } 142 143 breakNeeded = true; 144 } 145 146 while (maps.hasNext()) { 147 String mapName = (String ) maps.next(); 148 String mapLocation = delegate.mapLocation(domainName, mapName); 149 150 pw.print("\t<map name=\"" + mapName.trim()); 151 pw.print("\" location=\"" + mapLocation.trim()); 152 pw.println("\"/>"); 153 } 154 155 Iterator nodes = delegate.nodeNames(domainName); 157 if (nodes.hasNext() && breakNeeded) { 158 pw.println(); 159 } 160 while (nodes.hasNext()) { 161 String nodeName = (String ) nodes.next(); 162 String datasource = delegate.nodeDataSourceName(domainName, nodeName); 163 String adapter = delegate.nodeAdapterName(domainName, nodeName); 164 String factory = delegate.nodeFactoryName(domainName, nodeName); 165 Iterator mapNames = delegate.linkedMapNames(domainName, nodeName); 166 167 pw.println("\t<node name=\"" + nodeName.trim() + "\""); 168 169 if (datasource != null) { 170 datasource = datasource.trim(); 171 pw.print("\t\t datasource=\"" + datasource + "\""); 172 } 173 pw.println(""); 174 175 if (adapter != null) { 176 pw.println("\t\t adapter=\"" + adapter + "\""); 177 } 178 179 if (factory != null) { 180 pw.print("\t\t factory=\"" + factory.trim() + "\""); 181 } 182 pw.println(">"); 183 184 while (mapNames.hasNext()) { 185 String mapName = (String ) mapNames.next(); 186 pw.println("\t\t\t<map-ref name=\"" + mapName.trim() + "\"/>"); 187 } 188 pw.println("\t </node>"); 189 } 190 pw.println("</domain>"); 191 } 192 193 197 public void storeDataNode(PrintWriter out, DataSourceInfo info) { 198 out.println("<?xml version=\"1.0\" encoding=\"utf-8\"?>"); 199 out.print("<driver project-version=\"" + Project.CURRENT_PROJECT_VERSION + "\""); 200 if (info.getJdbcDriver() != null) { 201 out.print(" class=\"" + info.getJdbcDriver() + "\""); 202 } 203 out.println(">"); 204 205 if (info.getDataSourceUrl() != null) { 206 String encoded = Util.encodeXmlAttribute(info.getDataSourceUrl()); 207 out.println("\t<url value=\"" + encoded + "\"/>"); 208 } 209 210 out.println( 211 "\t<connectionPool min=\"" 212 + info.getMinConnections() 213 + "\" max=\"" 214 + info.getMaxConnections() 215 + "\" />"); 216 217 if (info.getUserName() != null || info.getPassword() != null) { 218 out.print("\t<login"); 219 if (info.getUserName() != null) { 220 String encoded = Util.encodeXmlAttribute(info.getUserName()); 221 out.print(" userName=\"" + encoded + "\""); 222 } 223 if (info.getPassword() != null) { 224 String encoded = Util.encodeXmlAttribute(info.getPassword()); 225 out.print(" password=\"" + encoded + "\""); 226 } 227 out.println("/>"); 228 } 229 230 out.println("</driver>"); 231 } 232 } 233 | Popular Tags |