You are viewing a plain text version of this content. The canonical link for it is here.
Posted to axis-cvs@ws.apache.org by aj...@apache.org on 2006/05/31 13:48:40 UTC

svn commit: r410500 - in /webservices/axis2/trunk/java/modules/common/src/org/apache/axis2/util: FileWriter.java URLProcessor.java XSLTTemplateProcessor.java XSLTUtils.java

Author: ajith
Date: Wed May 31 04:48:40 2006
New Revision: 410500

URL: http://svn.apache.org/viewvc?rev=410500&view=rev
Log:
oops - Forgot to add the these classes

Added:
    webservices/axis2/trunk/java/modules/common/src/org/apache/axis2/util/FileWriter.java
    webservices/axis2/trunk/java/modules/common/src/org/apache/axis2/util/URLProcessor.java
    webservices/axis2/trunk/java/modules/common/src/org/apache/axis2/util/XSLTTemplateProcessor.java
    webservices/axis2/trunk/java/modules/common/src/org/apache/axis2/util/XSLTUtils.java

Added: webservices/axis2/trunk/java/modules/common/src/org/apache/axis2/util/FileWriter.java
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/common/src/org/apache/axis2/util/FileWriter.java?rev=410500&view=auto
==============================================================================
--- webservices/axis2/trunk/java/modules/common/src/org/apache/axis2/util/FileWriter.java (added)
+++ webservices/axis2/trunk/java/modules/common/src/org/apache/axis2/util/FileWriter.java Wed May 31 04:48:40 2006
@@ -0,0 +1,70 @@
+/*
+ * Copyright 2004,2005 The Apache Software Foundation.
+ *
+ * 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.apache.axis2.util;
+
+
+import java.io.File;
+import java.io.IOException;
+
+public class FileWriter {
+
+    /**
+     * Creates/ returns a file object
+     *
+     * @param rootLocation - Location to be written
+     * @param packageName  - package, can be '.' separated
+     * @param fileName     name of the file
+     * @param extension    type of the file, java, csharp, cpp etc
+     * @return the File that was created
+     * @throws IOException
+     * @throws Exception
+     */
+    public static File createClassFile(File rootLocation,
+                                       String packageName,
+                                       String fileName,
+                                       String extension) throws IOException,
+            Exception {
+        File returnFile = null;
+        File root = rootLocation;
+
+        if (packageName != null) {
+            String directoryNames[] = packageName.split("\\.");
+            File tempFile = null;
+            int length = directoryNames.length;
+            for (int i = 0; i < length; i++) {
+                tempFile = new File(root, directoryNames[i]);
+                root = tempFile;
+                if (!tempFile.exists()) {
+                    tempFile.mkdir();
+                }
+            }
+        }
+
+        if (!fileName.endsWith(extension)) {
+            fileName = fileName + extension;
+        }
+
+        returnFile = new File(root, fileName);
+
+        if (!returnFile.exists()) {
+            // returnFile.createNewFile();
+        }
+        return returnFile;
+    }
+
+
+}

Added: webservices/axis2/trunk/java/modules/common/src/org/apache/axis2/util/URLProcessor.java
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/common/src/org/apache/axis2/util/URLProcessor.java?rev=410500&view=auto
==============================================================================
--- webservices/axis2/trunk/java/modules/common/src/org/apache/axis2/util/URLProcessor.java (added)
+++ webservices/axis2/trunk/java/modules/common/src/org/apache/axis2/util/URLProcessor.java Wed May 31 04:48:40 2006
@@ -0,0 +1,136 @@
+/*
+ * Copyright 2004,2005 The Apache Software Foundation.
+ *
+ * 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.apache.axis2.util;
+
+import java.net.MalformedURLException;
+import java.util.StringTokenizer;
+
+public class URLProcessor {
+    public static final String DEFAULT_PACKAGE = "org.apache.axis2";
+
+    /**
+     * Method makePackageName.
+     *
+     * @param namespace
+     * @return Returns String.
+     */
+    public static String makePackageName(String namespace) {
+
+        String hostname = null;
+        String path = "";
+
+        // get the target namespace of the document
+        try {
+            java.net.URL u = new java.net.URL(namespace);
+
+            hostname = u.getHost();
+            path = u.getPath();
+        } catch (MalformedURLException e) {
+            if (namespace.indexOf(":") > -1) {
+                hostname = namespace.substring(namespace.indexOf(":") + 1);
+
+                if (hostname.indexOf("/") > -1) {
+                    hostname = hostname.substring(0, hostname.indexOf("/"));
+                }
+            } else {
+                hostname = namespace;
+            }
+        }
+
+        // if we didn't file a hostname, bail
+        if (hostname == null || hostname.length() == 0) {
+            return null;
+        }
+
+        // convert illegal java identifier
+        hostname = hostname.replace('-', '_');
+        path = path.replace('-', '_');
+
+        path = path.replace(':', '_');
+
+        // chomp off last forward slash in path, if necessary
+        if ((path.length() > 0) && (path.charAt(path.length() - 1) == '/')) {
+            path = path.substring(0, path.length() - 1);
+        }
+
+        // tokenize the hostname and reverse it
+        StringTokenizer st = new StringTokenizer(hostname, ".:");
+        String[] words = new String[st.countTokens()];
+
+        for (int i = 0; i < words.length; ++i) {
+            words[i] = st.nextToken();
+        }
+
+        StringBuffer sb = new StringBuffer(namespace.length());
+
+        for (int i = words.length - 1; i >= 0; --i) {
+            addWordToPackageBuffer(sb, words[i], (i == words.length - 1));
+        }
+
+        // tokenize the path
+        StringTokenizer st2 = new StringTokenizer(path, "/");
+
+        while (st2.hasMoreTokens()) {
+            addWordToPackageBuffer(sb, st2.nextToken(), false);
+        }
+
+        return sb.toString().toLowerCase();
+    }
+
+
+    /**
+     * Massages <tt>word</tt> into a form suitable for use in a Java package name.
+     * Appends it to the target string buffer with a <tt>.</tt> delimiter if
+     * <tt>word</tt> is not the first word in the package name.
+     *
+     * @param sb        the buffer to append to
+     * @param word      the word to append
+     * @param firstWord a flag indicating whether this is the first word
+     */
+    private static void addWordToPackageBuffer(StringBuffer sb, String word,
+                                               boolean firstWord) {
+
+        if (JavaUtils.isJavaKeyword(word)) {
+            word = JavaUtils.makeNonJavaKeyword(word);
+        }
+
+        // separate with dot after the first word
+        if (!firstWord) {
+            sb.append('.');
+        }
+
+        // prefix digits with underscores
+        if (Character.isDigit(word.charAt(0))) {
+            sb.append('_');
+        }
+
+        // replace periods with underscores
+        if (word.indexOf('.') != -1) {
+            char[] buf = word.toCharArray();
+
+            for (int i = 0; i < word.length(); i++) {
+                if (buf[i] == '.') {
+                    buf[i] = '_';
+                }
+            }
+
+            word = new String(buf);
+        }
+
+        sb.append(word);
+    }
+}

Added: webservices/axis2/trunk/java/modules/common/src/org/apache/axis2/util/XSLTTemplateProcessor.java
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/common/src/org/apache/axis2/util/XSLTTemplateProcessor.java?rev=410500&view=auto
==============================================================================
--- webservices/axis2/trunk/java/modules/common/src/org/apache/axis2/util/XSLTTemplateProcessor.java (added)
+++ webservices/axis2/trunk/java/modules/common/src/org/apache/axis2/util/XSLTTemplateProcessor.java Wed May 31 04:48:40 2006
@@ -0,0 +1,156 @@
+/*
+ * Copyright 2004,2005 The Apache Software Foundation.
+ *
+ * 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.apache.axis2.util;
+
+
+import org.w3c.dom.Document;
+
+import javax.xml.transform.Result;
+import javax.xml.transform.Source;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.TransformerFactoryConfigurationError;
+import javax.xml.transform.URIResolver;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
+import javax.xml.transform.stream.StreamSource;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+
+public class XSLTTemplateProcessor {
+
+    /**
+     * Parses an XML stream with an XSL stream
+     *
+     * @param out        Stream to write the output
+     * @param xmlStream  Source XML stream
+     * @param xsltStream Source XSL stream
+     * @throws TransformerFactoryConfigurationError
+     *
+     * @throws TransformerException
+     */
+    public static void parse(OutputStream out,
+                             InputStream xmlStream,
+                             InputStream xsltStream)
+            throws TransformerFactoryConfigurationError, TransformerException {
+        Source xmlSource = new StreamSource(xmlStream);
+        Source xsltSource = new StreamSource(xsltStream);
+        Result result = new StreamResult(out);
+        Transformer transformer = TransformerFactory.newInstance()
+                .newTransformer(xsltSource);
+        transformer.transform(xmlSource, result);
+
+    }
+
+    /**
+     * Parses an XML stream with an XSL stream
+     *
+     * @param out       Stream to write the output
+     * @param doc
+     * @param transformer
+     * @throws TransformerFactoryConfigurationError
+     *
+     * @throws TransformerException
+     */
+    public static void parse(OutputStream out,
+                             Document doc,
+                             Transformer transformer)
+            throws TransformerFactoryConfigurationError, TransformerException {
+        Source xmlSource = new DOMSource(doc);
+        Result result = new StreamResult(out);
+        transformer.transform(xmlSource, result);
+
+    }
+
+
+
+    /**
+     * @param out
+     * @param document
+     * @param xsltStream
+     * @throws TransformerFactoryConfigurationError
+     *
+     * @throws TransformerException
+     */
+    public static void parse(OutputStream out,
+                             Document document,
+                             InputStream xsltStream)
+            throws TransformerFactoryConfigurationError, TransformerException {
+        Source xsltSource = new StreamSource(xsltStream);
+        Transformer transformer = TransformerFactory.newInstance()
+                .newTransformer(xsltSource);
+        parse(out, document, transformer);
+
+    }
+
+    /**
+     * @param out
+     * @param document
+     * @param xsltStream
+     * @throws TransformerFactoryConfigurationError
+     *
+     * @throws TransformerException
+     */
+    public static void parse(OutputStream out,
+                             Document document,
+                             InputStream xsltStream,
+                             URIResolver customResolver)
+            throws TransformerFactoryConfigurationError, TransformerException {
+        parse(out, document, xsltStream, customResolver, false);
+    }
+
+    /**
+     * @param out
+     * @param document
+     * @param xsltStream
+     * @throws TransformerFactoryConfigurationError
+     *
+     * @throws TransformerException
+     */
+    public static void parse(OutputStream out,
+                             Document document,
+                             InputStream xsltStream,
+                             URIResolver customResolver,
+                             boolean pretty)
+            throws TransformerFactoryConfigurationError, TransformerException {
+        Source xsltSource = new StreamSource(xsltStream);
+        TransformerFactory transformerFactory = TransformerFactory.newInstance();
+        if(pretty) {
+            try {
+                transformerFactory.setAttribute("indent-number", new Integer(2));
+            } catch (Exception e) {
+            }
+        }
+        if (customResolver!=null){
+             transformerFactory.setURIResolver(customResolver);
+        }
+       
+        Transformer transformer = transformerFactory
+                .newTransformer(xsltSource);
+        if(pretty) {
+            try {
+                transformer.setOutputProperty("{http://xml.apache.org/xalan}indent-amount", "2");
+            } catch (Exception e) {
+            }
+        }
+
+        parse(out, document, transformer);
+
+    }
+}

Added: webservices/axis2/trunk/java/modules/common/src/org/apache/axis2/util/XSLTUtils.java
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/common/src/org/apache/axis2/util/XSLTUtils.java?rev=410500&view=auto
==============================================================================
--- webservices/axis2/trunk/java/modules/common/src/org/apache/axis2/util/XSLTUtils.java (added)
+++ webservices/axis2/trunk/java/modules/common/src/org/apache/axis2/util/XSLTUtils.java Wed May 31 04:48:40 2006
@@ -0,0 +1,73 @@
+package org.apache.axis2.util;
+
+import org.w3c.dom.Attr;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+/*
+ * Copyright 2004,2005 The Apache Software Foundation.
+ *
+ * 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.
+ */
+
+public class XSLTUtils {
+
+    public static Document getDocument() throws ParserConfigurationException {
+        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
+        documentBuilderFactory.setNamespaceAware(true);
+        return documentBuilderFactory.newDocumentBuilder().newDocument();
+    }
+
+    /**
+     * Utility method to add an attribute to a given element
+     *
+     * @param document
+     * @param AttribName
+     * @param attribValue
+     * @param element
+     */
+    public static void addAttribute(Document document,
+                                    String AttribName,
+                                    String attribValue,
+                                    Element element) {
+        Attr attribute = document.createAttribute(AttribName);
+        attribute.setValue(attribValue);
+        element.setAttributeNode(attribute);
+    }
+
+    public static Element getElement(Document document,
+                                     String elementName) {
+        return document.createElement(elementName);
+
+    }
+
+
+    /**
+     * Utility method to add an attribute to a given element
+     *
+     * @param document
+     * @param elementName
+     * @param parentNode
+     */
+    public static Element addChildElement(Document document,
+                                          String elementName,
+                                          Node parentNode) {
+        Element elt = document.createElement(elementName);
+        parentNode.appendChild(elt);
+        return elt;
+    }
+
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: axis-cvs-unsubscribe@ws.apache.org
For additional commands, e-mail: axis-cvs-help@ws.apache.org