1 2 24 package org.enhydra.tool.archive.xml; 25 26 import org.w3c.dom.Document ; 28 import org.w3c.dom.Element ; 29 import org.w3c.dom.Node ; 30 import org.w3c.dom.NodeList ; 31 import org.enhydra.xml.io.DOMParser; 32 import org.xml.sax.InputSource ; 33 import org.xml.sax.SAXException ; 34 35 import org.enhydra.tool.archive.ArchiveException; 37 import org.enhydra.tool.archive.WarBuilder; 39 import org.enhydra.tool.archive.Descriptor; 40 import org.enhydra.tool.archive.Module; 41 import org.enhydra.tool.archive.WebApplication; 42 import org.enhydra.tool.common.PathHandle; 43 44 import java.io.InputStreamReader ; 46 import java.io.BufferedReader ; 47 import java.io.InputStream ; 48 import java.io.IOException ; 49 50 public class EarDescriptorHandler extends AbstractDescriptorHandler { 52 private final String ALT_DD = "alt-dd"; 53 private final String APP = "application"; 54 private final String DESC_NAME = "description"; 55 private final String DISPLAY_NAME = "display-name"; 56 private final String EJB = "ejb"; 57 private final String MODULE = "module"; 58 private final String WEB = "web"; 59 private final String WEB_URI = "web-uri"; 60 private final String CONTEXT_ROOT = "context-root"; 61 private final String PUBLIC_ID = 62 "-//Sun Microsystems, Inc.//DTD J2EE Application 1.2//EN"; 63 private final String SYSTEM_ID = 64 "http://java.sun.com/j2ee/dtds/application_1_2.dtd"; 65 66 private String appDesc = "Application description"; private String appName = new String (); 69 private Module[] mods = new Module[0]; 70 private InputStream [] mergeDescriptors = new InputStream [0]; 71 private boolean altDD = true; 72 73 public String getAppName() { 76 return appName; 77 } 78 79 public void setAppName(String n) { 80 appName = n; 81 } 82 83 public String getDescription() { 84 return appDesc; 85 } 86 87 public void setDescription(String d) { 88 appDesc = d; 89 } 90 91 public Module[] getAllModules() { 92 return mods; 93 } 94 95 public void setAllModules(Module[] m) { 96 mods = m; 97 } 98 99 public InputStream [] getMergeDescriptors() { 100 return mergeDescriptors; 101 } 102 103 public void setMergeDescriptors(InputStream [] m) { 104 mergeDescriptors = m; 105 } 106 107 public boolean isAlt() { 108 return altDD; 109 } 110 111 public void setAlt(boolean a) { 112 altDD = a; 113 } 114 115 public static void main(String [] args) { 116 EarDescriptorHandler prep = new EarDescriptorHandler(); 117 118 prep.setSource("d:/test/simple/test2.xml"); 119 prep.setOutStream(System.out); 120 try { 121 prep.prep(); 122 } catch (Exception e) { 123 e.printStackTrace(); 124 } 125 } 126 127 protected Document getDocument() throws ArchiveException { 128 DOMParser parser = new DOMParser(); 129 Document d = null; 130 NodeList list = null; 131 132 d = parser.newDocument(); 133 d.appendChild(d.createElement(APP)); 134 for (int i = 0; i < mergeDescriptors.length; i++) { 135 Document mergeDoc = null; 136 InputSource inputSource = null; 137 DocTypeFilterReader reader = null; 138 139 reader = 140 new DocTypeFilterReader(new BufferedReader (new InputStreamReader (mergeDescriptors[i]))); 141 inputSource = new InputSource (reader); 142 try { 143 mergeDoc = parser.parse(inputSource); 144 } catch (IOException e) { 145 throw new ArchiveException(e, 146 "Unable to parse merge descriptor"); 147 } catch (SAXException e) { 148 throw new ArchiveException(e, 149 "Unable to parse merge descriptor"); 150 } 151 list = mergeDoc.getChildNodes(); 152 for (int j = 0; j < list.getLength(); j++) { 153 Node child = null; 154 155 child = list.item(j); 156 if (child instanceof Element ) { 157 Element element = (Element ) child; 158 159 if (element.getNodeName().equals("module")) { 160 d.appendChild(element); 161 } 162 } 163 } 164 } 165 return d; 166 } 167 168 protected String getSystemID() { 170 return SYSTEM_ID; 171 } 172 173 protected String getPublicID() { 174 return PUBLIC_ID; 175 } 176 177 protected void prepElements() { 178 prepDisplayName(); 179 prepDescription(); 180 prepModules(); 181 } 182 183 private void prepDescription() { 184 Element element = null; 185 186 element = lookup(getDoc().getDocumentElement(), DESC_NAME); 187 if (element == null) { 188 element = getDoc().createElement(DESC_NAME); 189 element.appendChild(getDoc().createTextNode(getDescription())); 190 getDoc().getDocumentElement().appendChild(element); 191 } 192 } 193 194 private void prepDisplayName() { 195 Element element = null; 196 197 element = lookup(getDoc().getDocumentElement(), DISPLAY_NAME); 198 if (element == null) { 199 element = getDoc().createElement(DISPLAY_NAME); 200 element.appendChild(getDoc().createTextNode(getAppName())); 201 getDoc().getDocumentElement().appendChild(element); 202 } 203 } 204 205 private void prepModules() { 206 PathHandle ph = null; 207 208 for (int i = 0; i < mods.length; i++) { 209 Element modElement = null; 210 211 if (mods[i] instanceof WebApplication) { 212 modElement = appendWar((WebApplication) mods[i]); 213 } else if (mods[i].getName().toLowerCase().endsWith(".jar")) { 214 modElement = appendEjbJar(mods[i]); 215 } 216 if ((modElement != null) && isAlt()) { 217 appendAlt(modElement, mods[i]); 218 } 219 } 220 } 221 222 private Element appendWar(WebApplication webApp) { 223 Element mod = null; 224 Element web = null; 225 Element uri = null; 226 Element ctx = null; 227 228 mod = getDoc().createElement(MODULE); 229 web = getDoc().createElement(WEB); 230 uri = getDoc().createElement(WEB_URI); 231 ctx = getDoc().createElement(CONTEXT_ROOT); 232 mod.appendChild(web); 233 web.appendChild(uri); 234 web.appendChild(ctx); 235 uri.appendChild(getDoc().createTextNode(webApp.getName())); 236 ctx.appendChild(getDoc().createTextNode(webApp.getContextRoot())); 237 getDoc().getDocumentElement().appendChild(mod); 238 return mod; 239 } 240 241 private void appendAlt(Element modElement, Module mod) { 242 Element alt = null; 243 StringBuffer buf = new StringBuffer (); 244 245 buf.append(mod.getArchive()); 246 buf.append('/'); 247 if (mod instanceof WebApplication) { 248 buf.append(Descriptor.getArchivePath(Descriptor.WEB)); 249 } else { 250 buf.append(Descriptor.getArchivePath(Descriptor.EJB)); 251 } 252 alt = getDoc().createElement(ALT_DD); 253 alt.appendChild(getDoc().createTextNode(buf.toString())); 254 modElement.appendChild(alt); 255 } 256 257 private Element appendEjbJar(Module ejbMod) { 258 Element mod = null; 259 Element ejb = null; 260 261 mod = getDoc().createElement(MODULE); 262 ejb = getDoc().createElement(EJB); 263 mod.appendChild(ejb); 264 ejb.appendChild(getDoc().createTextNode(ejbMod.getName())); 265 getDoc().getDocumentElement().appendChild(mod); 266 return mod; 267 } 268 269 } 270 | Popular Tags |