1 23 24 29 30 package com.sun.enterprise.tools.upgrade.transform; 31 32 import com.sun.enterprise.tools.upgrade.transform.elements.*; 33 import java.io.File ; 34 import java.io.IOException ; 35 36 import javax.xml.parsers.DocumentBuilder ; 37 import javax.xml.parsers.DocumentBuilderFactory ; 38 import javax.xml.parsers.FactoryConfigurationError ; 39 import javax.xml.parsers.ParserConfigurationException ; 40 41 import org.xml.sax.SAXException ; 42 import org.xml.sax.SAXParseException ; 43 44 import org.w3c.dom.Document ; 45 import org.w3c.dom.DOMException ; 46 import org.w3c.dom.Element ; 47 import org.w3c.dom.Node ; 48 49 import javax.xml.transform.Transformer ; 51 import javax.xml.transform.TransformerFactory ; 52 import javax.xml.transform.TransformerException ; 53 import javax.xml.transform.TransformerConfigurationException ; 54 55 import javax.xml.transform.dom.DOMSource ; 56 import javax.xml.transform.stream.StreamResult ; 57 import javax.xml.transform.OutputKeys ; 58 59 import java.io.*; 60 import com.sun.enterprise.tools.upgrade.common.*; 61 import com.sun.enterprise.util.i18n.StringManager; 62 import com.sun.enterprise.tools.upgrade.logging.*; 63 import java.util.logging.*; 64 import java.util.Vector ; 65 import java.util.Enumeration ; 66 67 68 72 public class TransformManager implements BaseModule{ 73 74 private static TransformManager transManager; 75 private Document sourceDocument; 76 private Document resultDocument; 77 private StringManager stringManager = StringManager.getManager("com.sun.enterprise.tools.upgrade.transform"); 78 private Logger logger = CommonInfoModel.getDefaultLogger(); 79 private Vector recoveryList = new Vector (); 80 81 82 public TransformManager() { 83 } 84 public static TransformManager getTransformManager(){ 85 if(transManager == null) 86 transManager = new TransformManager(); 87 return transManager; 88 } 89 90 public void transform(Document source, Document result){ 91 this.sourceDocument = source; 92 this.resultDocument = result; 93 try{ 94 Element docEle = sourceDocument.getDocumentElement(); 95 BaseElement baseElement = ElementToObjectMapper.getMapper().getElementObject(docEle.getTagName()); 96 baseElement.transform(docEle, source.getDocumentElement(), result.getDocumentElement()); 97 }catch(Exception ex){ 98 logger.log(Level.SEVERE, stringManager.getString("upgrade.transform.startFailureMessage",ex.getMessage()),ex); 100 } 101 } 102 public boolean upgrade(CommonInfoModel commonInfo) { 103 logger.log(Level.INFO, stringManager.getString("upgrade.transform.startMessage")); 104 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 105 107 factory.setNamespaceAware(true); 108 if(commonInfo.getSourceDomainRootFlag()) { 109 factory.setAttribute("http://apache.org/xml/features/nonvalidating/load-external-dtd",Boolean.FALSE); 110 } 111 112 try { 113 backup(commonInfo.getTargetConfigXMLFile(), commonInfo); 114 DocumentBuilder builder = factory.newDocumentBuilder(); 115 DocumentBuilder builderDomainXml = factory.newDocumentBuilder(); 116 builderDomainXml.setEntityResolver((org.xml.sax.helpers.DefaultHandler )Class.forName 117 ("com.sun.enterprise.config.serverbeans.ServerValidationHandler").newInstance()); 118 BaseElement.setCommonInfoModel(commonInfo); 119 Document resultDoc = builderDomainXml.parse( new File (commonInfo.getTargetConfigXMLFile()) ); 120 if(commonInfo.getSourceVersionAndEdition().equals(UpgradeConstants.VERSION_AS7X_SE) || 122 commonInfo.getSourceVersionAndEdition().equals(UpgradeConstants.VERSION_AS7X_EE)){ 123 Document sourceDoc = builder.parse( new File (commonInfo.getSourceConfigXMLFile())); 124 this.transform(sourceDoc, resultDoc); 125 126 }else{ 127 if(!commonInfo.getSourceVersion().equals(UpgradeConstants.VERSION_7X)){ 128 builder.setEntityResolver((org.xml.sax.helpers.DefaultHandler )Class.forName 129 ("com.sun.enterprise.config.serverbeans.ServerValidationHandler").newInstance()); 130 } 131 Document sourceDoc = builder.parse( new File (commonInfo.getSourceConfigXMLFile())); 132 if((sourceDoc.getDocumentElement() != null) && (resultDoc.getDocumentElement() != null)) 133 this.transform(sourceDoc, resultDoc); 134 else 135 return false; 136 } 137 138 140 TransformerFactory tFactory = TransformerFactory.newInstance(); 142 Transformer transformer = tFactory.newTransformer(); 143 146 if (resultDoc.getDoctype() != null){ 147 String systemValue = resultDoc.getDoctype().getSystemId(); 148 transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, systemValue); 149 String pubValue = resultDoc.getDoctype().getPublicId(); 150 transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, pubValue); 151 } 152 153 DOMSource source = new DOMSource (resultDoc); 154 StreamResult result = new StreamResult (new FileOutputStream(commonInfo.getTargetConfigXMLFile())); 155 transformer.transform(source, result); 156 result.getOutputStream().close(); 157 158 }catch (Exception ex){ 159 UpdateProgressManager.getProgressManager().setContinueUpgrade(false); 160 logger.log(Level.SEVERE, stringManager.getString("upgrade.transform.startFailureMessage",ex.getMessage()),ex); 161 logger.log(Level.SEVERE, stringManager.getString("upgrade.transform.startFailureCheckAccessMessage")); 162 return false; 164 } 165 return true; 166 } 167 168 private void backup(String filePath, CommonInfoModel commonInfo) throws IOException { 169 String backupFilePath = filePath + ".bak"; 170 UpgradeUtils.getUpgradeUtils(commonInfo).copyFile(filePath, backupFilePath); 171 recoveryList.add(filePath); 172 } 173 174 public void recovery(CommonInfoModel commonInfo) { 175 Enumeration e = recoveryList.elements(); 176 while(e.hasMoreElements()){ 177 String recoverPath = (String )e.nextElement(); 178 String backupPath = recoverPath + ".bak"; 179 try { 180 UpgradeUtils.getUpgradeUtils(commonInfo).copyFile(backupPath, recoverPath); 181 new File (backupPath).delete(); 182 } catch (IOException ioe) { 183 logger.log(Level.SEVERE, stringManager.getString("upgrade.realm.recoveryFailureMessage",ioe.getMessage()),new Object []{recoverPath,ioe}); 184 } 185 } 186 } 187 188 191 public static void main(String [] args) { 192 System.setProperty("com.sun.aas.installRoot", "C:\\Softwares\\Sun\\j2eesdk1.4_beta3"); 193 DocumentBuilderFactory factory = 194 DocumentBuilderFactory.newInstance(); 195 factory.setNamespaceAware(true); 197 try { 198 DocumentBuilder builder = factory.newDocumentBuilder(); 199 Document sourceDoc = builder.parse( new File ("C:\\temp\\server.xml") ); 200 Document resultDoc = builder.parse( new File ("C:\\temp\\domain.xml") ); 201 TransformManager transMan = TransformManager.getTransformManager(); 202 transMan.transform(sourceDoc, resultDoc); 203 204 206 TransformerFactory tFactory = 208 TransformerFactory.newInstance(); 209 Transformer transformer = tFactory.newTransformer(); 210 211 DOMSource source = new DOMSource (resultDoc); 212 StreamResult result = new StreamResult (new FileOutputStream("c:\\temp\\domainModified.xml")); 213 transformer.transform(source, result); 214 result.getOutputStream().close(); 215 216 } catch (SAXParseException spe) { 217 spe.printStackTrace(); 218 } catch (SAXException sxe) { 219 sxe.printStackTrace(); 220 221 } catch (ParserConfigurationException pce) { 222 pce.printStackTrace(); 223 } catch (IOException ioe) { 224 ioe.printStackTrace(); 225 } catch (Exception ex){ 226 ex.printStackTrace(); 227 } 228 } 229 230 public String getName() { 231 return stringManager.getString("upgrade.transform.moduleName"); 232 } 233 234 } 235 | Popular Tags |