Java tutorial
/* * Copyright 2002-2007 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.suren.autotest.web.framework.code; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.HashSet; import java.util.Set; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.XPath; import org.dom4j.io.OutputFormat; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; import org.dom4j.xpath.DefaultXPath; import org.jaxen.SimpleNamespaceContext; import org.suren.autotest.web.framework.core.Callback; /** * ?xml?Page???? * @author suren * @date 20161225 ?7:44:36 * @see DefaultXmlCodeGenerator * @see DefaultXmlDataSourceGenerator * @see DefaultXmlSuiteRunnerGenerator */ public class DefaultXmlGenerator implements Generator { @Override public void generate(String srcCoding, final String outputDir) { //???Page Generator generator = new DefaultXmlCodeGenerator() { private String springConfigPath = "applicationContext.xml"; private Set<String> allPackage = new HashSet<String>(); @Override protected void create(AutoPage autoPage) { super.create(autoPage); allPackage.add(autoPage.getPackageName()); } @Override protected void done() { super.done(); if (allPackage.size() == 0) { return; } Document springConfigDoc = null; try (InputStream inputStream = getClassLoader().getResourceAsStream(springConfigPath)) { if (inputStream != null) { springConfigDoc = new SAXReader().read(inputStream); } } catch (IOException e) { e.printStackTrace(); } catch (DocumentException e) { e.printStackTrace(); } if (springConfigDoc == null) { springConfigDoc = DocumentHelper.createDocument(); String prefix = "p"; Element root = springConfigDoc.addElement(prefix + ":beans"); root.addNamespace(prefix, "http://www.springframework.org/schema/beans"); root.addNamespace("context", "http://www.springframework.org/schema/context"); root.addAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance"); root.addAttribute("xsi:schemaLocation", "http://www.springframework.org/schema/beans " + "http://www.springframework.org/schema/beans/spring-beans-3.0.xsd " + "http://www.springframework.org/schema/context " + "http://www.springframework.org/schema/context/spring-context-3.0.xsd "); } SimpleNamespaceContext simpleNamespaceContext = new SimpleNamespaceContext(); simpleNamespaceContext.addNamespace("p", "http://www.springframework.org/schema/beans"); simpleNamespaceContext.addNamespace("context", "http://www.springframework.org/schema/context"); XPath xpath = new DefaultXPath("/p:beans/context:component-scan"); xpath.setNamespaceContext(simpleNamespaceContext); //? Element componentScanEle = (Element) xpath.selectSingleNode(springConfigDoc); if (componentScanEle == null) { componentScanEle = springConfigDoc.getRootElement().addElement("context:component-scan"); } //pagepackage? String basePackage = componentScanEle.attributeValue("base-package", ""); basePackage = basePackage.trim(); if (!"".equals(basePackage) && !basePackage.endsWith(",")) { basePackage += ","; } basePackage.replaceAll(",\\s*", ","); for (String newPackage : allPackage) { if (!basePackage.contains(newPackage + ",")) { basePackage += (newPackage + ","); } } componentScanEle.addAttribute("base-package", basePackage); //??? File outputDirFile = new File(outputDir); if (!outputDirFile.isDirectory()) { outputDirFile.mkdirs(); } try (OutputStream dsOutput = new FileOutputStream(new File(outputDirFile, springConfigPath))) { OutputFormat outputFormat = OutputFormat.createPrettyPrint(); outputFormat.setIndentSize(4); XMLWriter xmlWriter = new XMLWriter(dsOutput, outputFormat); xmlWriter.write(springConfigDoc); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }; generator.generate(srcCoding, outputDir); //? generator = new DefaultXmlDataSourceGenerator(); generator.generate(srcCoding, outputDir); //? generator = new DefaultXmlSuiteRunnerGenerator(); generator.generate(srcCoding, outputDir); //?} } @Override public void generate(InputStream input, String outputDir, Callback callback) { // TODO Auto-generated method stub } }