egovframework.mgt.fit.library.parser.SourceParser.java Source code

Java tutorial

Introduction

Here is the source code for egovframework.mgt.fit.library.parser.SourceParser.java

Source

/*
 * Copyright 2011 MOPAS(Ministry of Public Administration and Security).
 *
 * 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 egovframework.mgt.fit.library.parser;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.ASTParser;
import org.eclipse.jdt.core.dom.AbstractTypeDeclaration;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.ImportDeclaration;
import org.eclipse.jdt.core.dom.PackageDeclaration;
import org.eclipse.jdt.core.dom.ParameterizedType;
import org.eclipse.jdt.core.dom.QualifiedName;
import org.eclipse.jdt.core.dom.SimpleType;
import org.eclipse.jdt.core.dom.Type;
import org.eclipse.jdt.core.dom.TypeDeclaration;

import egovframework.mgt.fit.library.exception.JavaSourceParsingException;
import egovframework.mgt.fit.library.parser.visitor.AnnotationParsingVisitor;
import egovframework.mgt.fit.library.parser.visitor.ClassParsingVisitor;
import egovframework.mgt.fit.library.parser.visitor.MethodParsingVisitor;
import egovframework.mgt.fit.library.parser.visitor.MethodResolveVisitor;
import egovframework.mgt.fit.library.unit.code.Directory;
import egovframework.mgt.fit.library.unit.code.JavaClass;
import egovframework.mgt.fit.library.unit.code.Method;
import egovframework.mgt.fit.library.unit.code.Project;
import egovframework.mgt.fit.library.unit.code.UnitList;
import egovframework.mgt.fit.library.util.StringHelper;

/**
 *   ?? ?   ?
 */
public class SourceParser {

    /**
     *  ? ?? ?.
     * @param path   (?)
     * @param project ? ?
     * @return ?  
     */
    public static boolean registerClass(String path, Project project) {

        if (project == null) {
            return false;
        }

        try {

            String source = FileUtils.readFileToString(new File(path));
            ASTParser parser = ASTParser.newParser(AST.JLS3);
            parser.setSource(source.toCharArray());
            CompilationUnit cu = (CompilationUnit) parser.createAST(null);

            /*      for (ImportDeclaration id : (List<ImportDeclaration>) cu.imports()) {
                     if (!project
              .hasClass(StringHelper.concatNameWithDot(project
                    .getKey(), id.getName().getFullyQualifiedName()))) {
                        resolveClass((QualifiedName) id.getName(), project);
                     }
                         
                  }*/

            for (AbstractTypeDeclaration type : (List<AbstractTypeDeclaration>) cu.types()) {
                if (type.getNodeType() == ASTNode.TYPE_DECLARATION) {
                    UnitList<JavaClass> imports = new UnitList<JavaClass>();
                    for (ImportDeclaration id : (List<ImportDeclaration>) cu.imports()) {
                        JavaClass ic = project.resolveClass(id.getName().toString());
                        imports.addUnit(ic);
                    }

                    resolveClass(cu.getPackage(), (TypeDeclaration) type, imports, project).setProjectClass(true)
                            .setPosition(new Directory(path));
                }
            }

        } catch (IOException ie) {
            ie.printStackTrace();
            return false;
        }
        /*
         * JavaSourceFactory jsf = new JavaSourceFactory(); try { JavaParser jp
         * = new JavaParser(jsf);
         * 
         * BufferedReader br = new BufferedReader(new InputStreamReader( new
         * BOMInputStream(new FileInputStream(new File(path)))));
         * 
         * jp.parse(br); } catch (RecognitionException re) {
         * re.printStackTrace(); return false; } catch (FileNotFoundException
         * fnfe) { fnfe.printStackTrace(); return false; } catch
         * (TokenStreamException tse) { tse.printStackTrace(); return false; }
         * catch (Exception e) { e.printStackTrace(); return false; }
         * 
         * if (!jsf.getJavaSources().hasNext()) { return false; }
         * 
         * for (Iterator iter = jsf.getJavaSources(); iter.hasNext();) {
         * JavaSource js = (JavaSource) iter.next(); resolveClass(js, project);
         * }
         */

        return true;
    }

    /**
     * ? ? ?. ? ?? ??  ? ?? .
     * @param packageDeclaration  ?
     * @param type  ?
     * @param imports  ? ?
     * @param project ? ?
     * @return ?/?? ? ? ?
     */
    private static JavaClass resolveClass(PackageDeclaration packageDeclaration, TypeDeclaration type,
            UnitList<JavaClass> imports, Project project) {

        String packageName = "";

        if (packageDeclaration != null && packageDeclaration.getName() != null) {
            packageName = packageDeclaration.getName().getFullyQualifiedName();
        }

        JavaClass jc = project.resolveClass(packageName, type.getName().getFullyQualifiedName())
                .setProjectClass(false);
        jc.setImports(imports);
        type.accept(new MethodResolveVisitor(project, jc));

        return jc;
    }

    /**
     * ? ? ?. ?? ( ?)? ? ?.
     * @param qName ?? 
     * @param project ? ?
     * @return ?/?? ? ? ?
     */
    private static JavaClass resolveClass(QualifiedName qName, Project project) {
        return project.resolveClass(qName.getQualifier().toString(), qName.getName().toString())
                .setProjectClass(false);
    }

    /**
     *  ? ? ?? ?.
     * @param path  
     * @param project ? ?
     * @return ? ?  
     */
    public static boolean parseSource(String path, Project project) throws JavaSourceParsingException {
        if (project == null) {
            return false;
        }

        try {
            String source = FileUtils.readFileToString(new File(path));
            ASTParser parser = ASTParser.newParser(AST.JLS3);
            parser.setSource(source.toCharArray());
            CompilationUnit cu = (CompilationUnit) parser.createAST(null);

            //cu.accept(new ClassParsingVisitor(project));

            for (AbstractTypeDeclaration type : (List<AbstractTypeDeclaration>) cu.types()) {
                if (type.getNodeType() == ASTNode.TYPE_DECLARATION) {
                    JavaClass jc = convertSourceToClass(cu, type, project);
                    project.updateClass(jc);
                }
            }

        } catch (IOException ie) {
            ie.printStackTrace();
            return false;
        }

        /*
         * JavaSourceFactory jsf = new JavaSourceFactory();
         * 
         * try { JavaParser jp = new JavaParser(jsf);
         * 
         * BufferedReader br = new BufferedReader(new InputStreamReader( new
         * BOMInputStream(new FileInputStream(new File(path)))));
         * 
         * jp.parse(br); } catch (RecognitionException re) {
         * re.printStackTrace(); return false; } catch (FileNotFoundException
         * fnfe) { fnfe.printStackTrace(); return false; } catch
         * (TokenStreamException tse) { tse.printStackTrace(); return false; }
         * 
         * if (!jsf.getJavaSources().hasNext()) { return false; }
         * 
         * for (Iterator<JavaSource> iter = jsf.getJavaSources();
         * iter.hasNext();) { JavaSource js = (JavaSource) iter.next();
         * JavaClass jc = convertSourceToClass(js, project);
         * project.updateClass(jc); }
         */

        return true;

    }

    /**
     *  ? ? ? . ???? resolveClass   ? .
     * @param cu ? 
     * @param atd  ?
     * @param project ? ?
     * @return ?? ? ?
     */
    public static JavaClass convertSourceToClass(CompilationUnit cu, AbstractTypeDeclaration atd, Project project)
            throws JavaSourceParsingException {

        TypeDeclaration type = (TypeDeclaration) atd;

        String packageName = cu.getPackage().getName().getFullyQualifiedName();
        String className = type.getName().getFullyQualifiedName();

        if (!project.hasClass(packageName, className)) {
            throw new JavaSourceParsingException("Class NOT created in parsing phase one : "
                    + StringHelper.concatNameWithDot(packageName, className));
        }

        JavaClass jc = project.resolveClass(StringHelper.concatNameWithDot(packageName, className));

        jc.setNode(type);

        type.accept(new ClassParsingVisitor(project, jc));
        type.accept(new AnnotationParsingVisitor(jc, ASTNode.TYPE_DECLARATION));

        jc.setInterface(type.isInterface());

        jc.setModifier(type.getModifiers());

        // set super class
        if (type.getSuperclassType() != null) {
            jc.setSuperClass(
                    project.resolveClass(jc.getFullnameWithSimpleName(type.getSuperclassType().toString())));
        } else {
            jc.setSuperClass(project.OBJECT_CLASS);
        }

        // set implements
        for (Type interfaceType : (List<Type>) type.superInterfaceTypes()) {
            if (interfaceType instanceof SimpleType) {
                if (type == null || type.getSuperclassType() == null) {
                }
                jc.addInterface(project.resolveClass(jc.getFullnameWithSimpleName(
                        ((SimpleType) interfaceType).getName().getFullyQualifiedName())));
            } else if (interfaceType instanceof ParameterizedType) {
            }
        }

        /*
         * // access modifier 
         * jc.setModifier(SourceUnit.convertStringToModifier
         * (source.getProtection().toString()));
         * 
         * if (source.isAbstract()) { jc.setModifier(SourceUnit.ABSTRACT); }
         * 
         * JavaQName[] supers = source.getExtends();
         * 
         * for (JavaQName jq : supers) { if
         * (project.hasClass(StringHelper.concatNameWithDot(project.getKey(),
         * jq.getPackageName(), jq.getClassName()))) { jc.setSuperClass(
         * project.getClass( StringHelper
         * .concatNameWithDot(jq.getPackageName(), jq.getClassName()))); } else
         * { jc.setSuperClass( project .getClass(jq.getPackageName(),
         * jq.getClassName()) .setProjectClass(false)); } }
         * 
         * // import ?
         * 
         * JavaQName[] imports = source.getImports();
         * 
         * for (JavaQName jq : imports) { if
         * (project.hasClass(StringHelper.concatNameWithDot(project.getKey(),
         * jq.getPackageName(), jq.getClassName()))) { jc.addImport(
         * project.getClass( StringHelper
         * .concatNameWithDot(jq.getPackageName(), jq.getClassName()))); } else
         * { jc.addImport( project .getClass(jq.getPackageName(),
         * jq.getClassName()) .setProjectClass(false)); } }
         * 
         * 
         * for (JavaMethod jm : source.getMethods()) {
         * jc.addMethod(convertMethod(jm, project)); }
         */

        jc.setProjectClass(true);

        return jc;

    }

    /**
     * ?? ??   ? ? ?. 
     * @param project ? ?
     * @return ?? ?? ? 
     * @throws JavaSourceParsingException
     */
    public static boolean parseDetailInformation(Project project) throws JavaSourceParsingException {
        if (project == null) {
            return false;
        }

        for (JavaClass jc : project.getClassPool().getProjectClassList()) {
            for (Method m : jc.getMethods()) {
                m.getNode().accept(new MethodParsingVisitor(m));
            }
        }

        return true;

    }

    /**
     * ?? ?? ??  ? ?? .
     * @param project ? ?
     * @return ? ? 
     */
    public static boolean setSubclasses(Project project) {
        if (project == null) {
            return false;
        }

        UnitList<JavaClass> classes = project.getClassPool().getProjectClassList();

        for (JavaClass jc : classes) {
            if (jc.getSuperClass() != null) {
                jc.getSuperClass().addSubclass(jc);
            }

            for (JavaClass inf : jc.getInterfaces()) {
                inf.addSubclass(jc);
            }
        }

        return true;
    }

}