sqlmodel.java

来自「一个eclipse插件源代码。用于web开发」· Java 代码 · 共 217 行

JAVA
217
字号
/*
 * $Header: /home/cvs/WEBPUMP2.0/WebPumpIDE_Src/WebPumpIDE/src/com/webpump/ui/sql/SQLModel.java,v 1.1.1.1 2004/07/01 09:07:52 wang_j Exp $
 * $Revision: 1.1.1.1 $
 * $Date: 2004/07/01 09:07:52 $
 *
 * ====================================================================
 *
 * The NanJing HopeRun(IT-FOREST) Software License, Version 2.0.0
 *
 * Copyright 2003-2004 by NanJing HopeRun(IT-FOREST) Information System Co., Ltd, CHINA and
 *                        IT Forest Corporation
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * HopeRun(IT-FOREST) Information System Co., Ltd, CHINA and IT Forest Corporation.
 * You shall not disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into with
 * HopeRun(IT-FOREST) Information System Co., Ltd, CHINA and IT Forest Corporation.
 */
 
package com.webpump.ui.sql;

import java.util.HashMap;
import java.util.Vector;
import com.webpump.ui.base.data.BaseModel;
import com.webpump.ui.perspective.WebpumpIDEPlugin;

import java.io.*;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.pde.core.*;

/**
 * Class for model of SQL.
 * 
 * @author luo_sa
 * @version 2.0.0 2004-2-24
 */
public class SQLModel extends BaseModel {
  

    /** Store the extenal sql. */
    private HashMap hmExtSQL = new HashMap();
    
    /** A starting mark. */
    public static String COMMENT_LABEL = "#";
    
    
    
    /**
     * Parse the information in objHashMap to Model.
     * @param objHashMap
     */
    protected void processDocument(HashMap objHashMap) {
        
        if (m_objDataObject == null) {
            m_objDataObject = new SQLInfo();
            m_objDataObject.setModel(this);
            m_objDataObject.setInTheModel(true);
        }
        ((SQLInfo) m_objDataObject).parse(objHashMap);             
    }
    
    /**
     * Load the file  ,parse the file stream to model.
     */
    public void load(InputStream stream, boolean outOfSync)
        throws CoreException {
            
        BufferedReader reader = null;
        
        try {
            String strCurrentLine = "";
            String strPreLine="";
            boolean bSqlStartFlag = false;
            boolean bSqlEndFlag = false;
            boolean bCurrentLineIsSqlStart = false;

            String strTemp ="";
            String strKey ="";
            String strSQL = "";
            Vector vTemp = new Vector();
			
			reader = new BufferedReader(new InputStreamReader(stream));
            //reader.readLine();
            strCurrentLine = reader.readLine();
                        
            while(strCurrentLine !=null) {
                strCurrentLine = new String(strCurrentLine.trim().getBytes(),"UTF-8") ;

                if (!strCurrentLine.startsWith(COMMENT_LABEL)) {

                    //check current line is sql start or not
                    int nIndexofEqual = 0;
                    nIndexofEqual = strCurrentLine.indexOf("=");
                    if ((bSqlStartFlag == false) && (bSqlEndFlag == false))
                        strPreLine = "";
                    if (    bSqlStartFlag == false
                            && nIndexofEqual>0
                            && (   strPreLine.equals("")
                                  || strPreLine.lastIndexOf(";") == strPreLine.length()-1 ) ) {
                        bSqlStartFlag = true;
                        bCurrentLineIsSqlStart = true;
                    }
                    //check current line is sql end or not
                    if ( ( !strCurrentLine.equals("")) && strCurrentLine.lastIndexOf(";") == strCurrentLine.length() - 1) {
                        bSqlEndFlag = true;
                    }

                    //store the line info to the temp vector
                    if (bSqlStartFlag == true) {
                        if (bCurrentLineIsSqlStart) {
                            strKey = strCurrentLine.substring(0, nIndexofEqual);
                            strKey = strKey.trim();
                            strTemp = strCurrentLine.substring(nIndexofEqual+1, strCurrentLine.length());
                            strTemp = strTemp.trim();
                            vTemp.add(strTemp);
                            bCurrentLineIsSqlStart = false;
                        } else {
                            vTemp.add(strCurrentLine);
                        }
                    }
                    //if there is a complete sql sentence
                    if (bSqlStartFlag == true && bSqlEndFlag==true) {
                        strTemp="";
                        String[] strTempArray = new String [vTemp.size()];
                        vTemp.copyInto(strTempArray);
                        for (int nIndex = 0; nIndex < strTempArray.length; nIndex++) {
                            strTemp = strTemp + strTempArray[nIndex] + "\n";
                        }
                        vTemp.clear();
                        strSQL = strTemp.trim();

                        // end modify
                        if (strSQL.endsWith(";")) {
                            strSQL = strSQL.substring(0, strSQL.length() -1 );
                        }
                        if (hmExtSQL == null) {
                            hmExtSQL = new HashMap();
                        }
                       
                        hmExtSQL.put(strKey, strSQL);

                        strTemp = "";
                        strSQL = "";
                        bSqlStartFlag = false;
                        bSqlEndFlag = false;
                    }
                    strPreLine = strCurrentLine;
                }

                strCurrentLine = reader.readLine();
            }

        } catch(Exception e) {
            
        } finally {
            
            if (reader != null)
                try {
                    reader.close();
                }
                catch (IOException e1){
                    WebpumpIDEPlugin.log(e1);
                }
        }
        
        processDocument(hmExtSQL);
        loaded = true;      
        
    }
    
    /**
     * If the file has been changed ,reload the file and parse to model.
     */
    public void reload(InputStream stream, boolean outOfSync)
        throws CoreException {   
            
        refresh();    
        load(stream, outOfSync);
        
        fireModelChanged(
            new ModelChangedEvent(
                IModelChangedEvent.WORLD_CHANGED,
                new Object[] { m_objDataObject },
                null));
                
    }
    
    /**
     * Clear the  data object and hashmap .
     */
    public void refresh() {
        
        m_objDataObject = null;
        //hmExtSQL = null; 
		hmExtSQL = new HashMap();
        
    }
    
    
    /**
     * Get String of SQLModel object.
     * @return
     */
    public String getSQLModelString() {
        return ((SQLInfo) m_objDataObject).getSQLInfoString();
    }

		
	public void save(PrintWriter writer) {
		if (isLoaded()) {
			m_objDataObject.write("", writer);
		}
		dirty = false;
	}
}

⌨️ 快捷键说明

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