001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.axis2.tool.core;
020:
021: import org.apache.axis2.wsdl.codegen.writer.FileWriter;
022: import org.apache.axis2.wsdl.codegen.writer.ServiceXMLWriter;
023: import org.w3c.dom.Document;
024: import org.w3c.dom.Element;
025:
026: import javax.xml.parsers.DocumentBuilder;
027: import javax.xml.parsers.DocumentBuilderFactory;
028: import javax.xml.parsers.ParserConfigurationException;
029: import javax.xml.transform.Result;
030: import javax.xml.transform.Source;
031: import javax.xml.transform.Transformer;
032: import javax.xml.transform.TransformerFactory;
033: import javax.xml.transform.dom.DOMSource;
034: import javax.xml.transform.stream.StreamResult;
035: import java.io.ByteArrayOutputStream;
036: import java.io.File;
037: import java.io.IOException;
038: import java.util.ArrayList;
039:
040: public class ServiceFileCreator {
041:
042: public File createServiceFile(String serviceName,
043: String implementationClassName, ArrayList methodList)
044: throws Exception {
045:
046: String currentUserDir = System.getProperty("user.dir");
047: String fileName = "services.xml";
048:
049: FileWriter serviceXmlWriter = new ServiceXMLWriter(
050: currentUserDir);
051: writeFile(getServiceModel(serviceName, implementationClassName,
052: methodList), serviceXmlWriter, fileName);
053:
054: return new File(currentUserDir + File.separator + fileName);
055:
056: }
057:
058: private Document getServiceModel(String serviceName,
059: String className, ArrayList methods) {
060:
061: DocumentBuilder builder = null;
062: try {
063: builder = DocumentBuilderFactory.newInstance()
064: .newDocumentBuilder();
065: } catch (ParserConfigurationException e) {
066: throw new RuntimeException(e);
067: }
068: Document doc = builder.newDocument();
069:
070: Element rootElement = doc.createElement("interface");
071: rootElement.setAttribute("classpackage", "");
072: rootElement.setAttribute("name", className);
073: rootElement.setAttribute("servicename", serviceName);
074: Element methodElement = null;
075: int size = methods.size();
076: for (int i = 0; i < size; i++) {
077: methodElement = doc.createElement("method");
078: rootElement.setAttribute("name", methods.get(i).toString());
079: rootElement.appendChild(methodElement);
080: }
081: doc.appendChild(rootElement);
082: return doc;
083: }
084:
085: /**
086: * A resusable method for the implementation of interface and implementation writing
087: * @param model
088: * @param writer
089: * @throws IOException
090: * @throws Exception
091: */
092: private void writeFile(Document model, FileWriter writer,
093: String fileName) throws IOException, Exception {
094:
095: Source source = new DOMSource(model);
096: ByteArrayOutputStream memoryStream = new ByteArrayOutputStream();
097: Result result = new StreamResult(memoryStream);
098: Transformer xformer = TransformerFactory.newInstance()
099: .newTransformer();
100: xformer.transform(source, result);
101:
102: //TODO: Doesn't really output stuff from the memorystream to file...hmm.
103:
104: writer.loadTemplate();
105: writer.createOutFile(null, fileName);
106: }
107:
108: }
|