⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 generator.java

📁 MobilerRPC enables remote procedure calls between J2ME applications and java sevlet server.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * Copyright (c) 2003, Artem Rudoy
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package net.sourceforge.mobilerpc;

import net.sourceforge.mobilerpc.simpletypes.*;
import net.sourceforge.mobilerpc.simpletypes.wrappers.*;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import javax.xml.parsers.DocumentBuilderFactory;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.StringTokenizer;

/**
 * $Id: Generator.java,v 1.10 2003/06/19 20:26:29 artyomr Exp $
 * @author Artem Rudoy
 */
public class Generator
{
    public final static String FILE_HEADER = "/**\n" +
            " * Generated by MobileRPC 1.0.2 http://mobilerpc.sourceforge.net\n" +
            " */";

    private File configFile = null;
    private File serverDir = null;
    private File j2meDir = null;

    private String commonPackageName = null;
    private String serverPackageName = null;
    private String j2mePackageName = null;

    private HashMap types = new HashMap();
    private HashMap structs = new HashMap();
    private HashMap exceptions = new HashMap();
    private HashMap interfaces = new HashMap();

    public Generator(String configFile, String serverDir, String j2meDir)
    {
        this.configFile = new File(configFile);
        this.serverDir = new File(serverDir);
        this.j2meDir = new File(j2meDir);

        addType(new BooleanType());
        addType(new ByteType());
        addType(new ShortType());
        addType(new IntType());
        addType(new LongType());
        addType(new CharType());
        addType(new StringType());
        addType(new DateType());
        addType(new BooleanWrapperType());
        addType(new ByteWrapperType());
        addType(new CharWrapperType());
        addType(new IntWrapperType());
        addType(new LongWrapperType());
        addType(new ShortWrapperType());
    }

    public void readConfig() throws Exception
    {
        Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(configFile);
        Element docElement = doc.getDocumentElement();
        if(!docElement.getNodeName().equals("mobileRPC"))
            throw new InterfaceDefinitionException("Interface definition root element must have 'mobileRPC' name.");
        commonPackageName = DOMUtils.getAttributeValue(docElement, "commonPackage");
        if(commonPackageName == null)
            throw new InterfaceDefinitionException("Common package is not defined.");
        serverPackageName = DOMUtils.getAttributeValue(docElement, "serverPackage");
        if(serverPackageName == null)
            throw new InterfaceDefinitionException("Server package is not defined.");
        j2mePackageName = DOMUtils.getAttributeValue(docElement, "j2mePackage");
        if(j2mePackageName == null)
            throw new InterfaceDefinitionException("J2ME package is not defined.");

        // Load structs
        for(Element structElement = DOMUtils.getFirstNamedChildElement(docElement, "struct");
            structElement != null;
            structElement = DOMUtils.getNextNamedSiblingElement(structElement, "struct"))
        {
            loadStruct(structElement);
        }

        // Load exceptions
        for(Element exceptionElement = DOMUtils.getFirstNamedChildElement(docElement, "exception");
            exceptionElement != null;
            exceptionElement = DOMUtils.getNextNamedSiblingElement(exceptionElement, "exception"))
        {
            loadException(exceptionElement);
        }

        // Load interfaces
        for(Element interfaceElement = DOMUtils.getFirstNamedChildElement(docElement, "interface");
            interfaceElement != null;
            interfaceElement = DOMUtils.getNextNamedSiblingElement(interfaceElement, "interface"))
        {
            loadInterface(interfaceElement);
        }
    }

    private void loadStruct(Element element) throws InterfaceDefinitionException
    {
        String structName = DOMUtils.getAttributeValue(element, "name");
        if(structName == null)
            throw new InterfaceDefinitionException("Struct name not defined.");
        if(types.get(structName) != null)
            throw new InterfaceDefinitionException("Duplicate definition of the " + structName + " struct.");
        String superStructName = DOMUtils.getAttributeValue(element, "extends");
        Struct superStruct = null;
        if(superStructName != null)
        {
            superStruct = (Struct) structs.get(superStructName);
            if(superStruct == null)
                throw new InterfaceDefinitionException("Struct " + superStructName + " not found as superstruct for " + structName + ".");
        }
        Struct struct = new Struct(structName, commonPackageName, superStruct);
        addType(struct);
        structs.put(struct.getName(), struct);

        // Load constants
        ArrayList constants = new ArrayList();
        for(Element constantElement = DOMUtils.getFirstNamedChildElement(element, "constant");
                constantElement != null;
                constantElement = DOMUtils.getNextNamedSiblingElement(constantElement, "constant"))
        {
            constants.add(loadConstant(constantElement));
        }
        struct.setConstants((Constant[])constants.toArray(new Constant[constants.size()]));

        // Load additional code
        String additionalCode = DOMUtils.getChildElementValue(element, "additionalCode");
        struct.setAdditionalCode(additionalCode);

        // Load attributes
        ArrayList attributes = new ArrayList();
        for(Element attributeElement = DOMUtils.getFirstNamedChildElement(element, "attribute");
            attributeElement != null;
            attributeElement = DOMUtils.getNextNamedSiblingElement(attributeElement, "attribute"))
        {
            String attributeName = DOMUtils.getAttributeValue(attributeElement, "name");
            if(attributeName == null)
                throw new InterfaceDefinitionException("Attribute name is not defined.");
            Type attributeType = (Type) types.get(attributeElement.getAttribute("type"));
            if(attributeType == null)
                throw new InterfaceDefinitionException("Type for attribute " + attributeName + " in struct " + structName + " not found.");
            boolean isArray = attributeElement.getAttribute("array").equals("true");
            attributes.add(new Attribute(attributeName, attributeType, isArray));
        }
        if(attributes.size() == 0)
            throw new InterfaceDefinitionException("No attribute specified for " + structName + " struct.");
        struct.setAttributes((Attribute[]) attributes.toArray(new Attribute[attributes.size()]));
    }

    private void loadException(Element element) throws InterfaceDefinitionException
    {
        String exceptionName = DOMUtils.getAttributeValue(element, "name");
        if(exceptionName == null)
            throw new InterfaceDefinitionException("Exception name not defined.");
        if(exceptions.get(exceptionName) != null)
            throw new InterfaceDefinitionException("Duplicate definition of the " + exceptionName + " exception.");
        String superExceptionName = DOMUtils.getAttributeValue(element, "extends");
        ExceptionDescription superException = null;
        if(superExceptionName != null)
        {
            superException = (ExceptionDescription)exceptions.get(superExceptionName);
            if(superException == null)
                throw new InterfaceDefinitionException("Exception " + superExceptionName + " not found as superexception for " + exceptionName + ".");
        }
        ExceptionDescription exception = new ExceptionDescription(exceptionName, commonPackageName, superException);
        exceptions.put(exception.getName(), exception);
    }

    private void loadInterface(Element element) throws InterfaceDefinitionException
    {
        String interfaceName = DOMUtils.getAttributeValue(element, "name");
        if(interfaceName == null)
            throw new InterfaceDefinitionException("Interface name not defined.");
        if(interfaces.get(interfaceName) != null)
            throw new InterfaceDefinitionException("Duplicate definition of the " + interfaceName + " interface.");

        boolean supportCookie = "true".equals(DOMUtils.getAttributeValue(element, "supportCookie"));

        // Load constants
        ArrayList constants = new ArrayList();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -