Package fr.tm.elibel.smartqvt.qvtparser

Source Code of fr.tm.elibel.smartqvt.qvtparser.WebParse

/*
* Copyright (c) 2006 France Telecom
* All rights reserved.
*
* This software is published under the terms of the EPL (http://www.eclipse.org/legal/epl-v10.html)
* in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/

package fr.tm.elibel.smartqvt.qvtparser;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.ui.console.ConsolePlugin;
import org.eclipse.ui.console.IConsole;
import org.eclipse.ui.console.MessageConsole;
import org.eclipse.ui.console.MessageConsoleStream;

import fr.tm.elibel.smartqvt.qvtparser.templates.QVTFile;
import fr.tm.elibel.smartqvt.stdlibs.QvtLib;
import fr.tm.elibel.smartqvt.stdlibs.QvtLib.Model;
import fr.tm.elibel.smartqvt.utils.UtilsPlugin;

public class WebParse {

  private File file;

  private MessageConsoleStream mcs;

  private File xmiFile = null;

  public WebParse(File file) {
    this.file = file;
    mcs = getConsole(file.getName());
    mcs.getConsole().clearConsole();
    mcs.getConsole().activate();
  }

  public static MessageConsoleStream getConsole(String consoleName) {
    MessageConsoleStream console;
    IConsole ic = null;
    IConsole[] array = ConsolePlugin.getDefault().getConsoleManager()
        .getConsoles();
    for (int i = 0; i < array.length; i++) {
      if (array[i].getName().matches("QVT: " + consoleName)) {
        ic = array[i];
      }
    }
    if (ic == null) {
      ic = new MessageConsole("QVT: " + consoleName, null);
      ConsolePlugin.getDefault().getConsoleManager().addConsoles(
          new IConsole[] { ic });
    }
    console = ((MessageConsole) ic).newMessageStream();
    return console;
  }

  protected void fullBuild(final IProgressMonitor monitor)
      throws CoreException {
    // try {
    // getProject().accept(new MyBuildVisitor());
    // } catch (CoreException e) {
    // }
  }

  public void run() {
    String result = "Ok";
    try {
      try {
        send(file);
      } catch (Exception e) {
        e.printStackTrace();
        mcs.print(e.getMessage());
        for (int i = 0; i < e.getStackTrace().length; i++) {
          mcs.println(e.getStackTrace()[i].toString());
        }
        result = "Failed";
      }
      mcs.println("End of WebParse: " + result);
    } catch (Exception e) {
      e.printStackTrace();
      mcs.print(e.getMessage());
      for (int i = 0; i < e.getStackTrace().length; i++) {
        mcs.println(e.getStackTrace()[i].toString());
      }
    } catch (Error e) {
      e.printStackTrace();
      mcs.print(e.getMessage());
      for (int i = 0; i < e.getStackTrace().length; i++) {
        mcs.println(e.getStackTrace()[i].toString());
      }
      mcs.println("Error:");
      mcs.println(e.toString());
    }
  }

  protected IStatus run(IProgressMonitor monitor) {

    IStatus status = Status.OK_STATUS;
    String result = "Ok";
    try {
      try {
        send(file);
      } catch (Exception e) {
        e.printStackTrace();
        mcs.print(e.getMessage());
        for (int i = 0; i < e.getStackTrace().length; i++) {
          mcs.println(e.getStackTrace()[i].toString());
        }
        status = Status.CANCEL_STATUS;
        result = "Failed";
      }
      // display.syncExec(new RunnableResult(result));
      mcs.println("End of WebParse: " + result);

    } catch (RuntimeException e) {
      e.printStackTrace();
    }
    return status;
  }

  // private class RunnableResult implements Runnable {
  // String result;
  //   
  // public RunnableResult(String result)
  // {
  // this.result = result;
  // }
  //   
  // public void run() {
  // MessageConsole mc = new MessageConsole("Qvt Web Parse", null);
  // ConsolePlugin.getDefault().getConsoleManager().addConsoles(new
  // IConsole[]{ mc });
  // MessageConsoleStream mcs = mc.newMessageStream();
  // mcs.println(result);
  // }
  // }

  public void send(File file) {
    IPreferenceStore ips = QvtparserPlugin.getDefault()
        .getPreferenceStore();
    String strURL = ips.getString("WEB_PARSER_URL");
    String login = ips.getString("WEB_PARSER_LOGIN");
    String password = ips.getString("WEB_PARSER_PASSWORD");
    String tmpfile = ips.getString("WEB_PARSER_TMPFILE");
    String message = "";
    String lineSeparator = System.getProperties().getProperty(
        "line.separator");
    try {
      URL url = new URL(strURL);
      URLConnection conn = url.openConnection();
      conn.setDoInput(true);
      conn.setDoOutput(true);
      conn.setUseCaches(false);
      conn
          .setRequestProperty("Content-Type",
              "multipart/form-data; boundary=---------------------------1331692493953");
      String code = login + ":" + password;
      String encoding = new sun.misc.BASE64Encoder().encode(code
          .getBytes());
      conn.setRequestProperty("Authorization", "Basic " + encoding);
      DataOutputStream out = new DataOutputStream(conn.getOutputStream());

      FileInputStream fis = new FileInputStream(file);
      InputStreamReader inputStreamReader = new InputStreamReader(fis);
      BufferedReader bufferedReader = new BufferedReader(
          inputStreamReader);
      String progId = file.getName().replaceAll("\\.*", "");
      String line = bufferedReader.readLine();
      boolean stop = false;
      String metamodels = "";
      while (line != null && !stop) {
        if (line.matches("--define .*")) {
          String fileName = file.getParent() + "/"
              + line.substring(9, line.length());
          File ecorefile = new File(fileName);
          mcs.println("--define: " + ecorefile.getName());
          QvtLib qvtLib = new QvtLib();
          Model m = qvtLib.new EMFXMIModel(ecorefile);
          metamodels = metamodels + UtilsPlugin.getDefault().transformEcore2EmofTxt(m);
        } else {
          stop = true;
        }
        line = bufferedReader.readLine();
      }
      System.getProperties().setProperty("line.separator", "\n");
      bufferedReader.close();
      inputStreamReader.close();
      fis.close();
      fis = new FileInputStream(file);
      inputStreamReader = new InputStreamReader(fis);
      bufferedReader = new BufferedReader(inputStreamReader);
      String qvtFile = (new QVTFile()).generate(metamodels,
          bufferedReader);
      PrintStream trout = new PrintStream(new FileOutputStream(new File(
          tmpfile)));
      qvtFile = qvtFile.replaceAll("\r", "");
      trout.println(qvtFile);
      String content = (new fr.tm.elibel.smartqvt.qvtparser.templates.WebParse())
          .generate(file.getName(), progId, qvtFile);
      content = content.replaceAll("\r", "");
      out.writeBytes(content);
      DataInputStream conninput = new DataInputStream(conn
          .getInputStream());
      out.close();

      InputStreamReader sr = new InputStreamReader(conninput);
      BufferedReader br = new BufferedReader(sr);
      String str = br.readLine();
      boolean success = false;
      boolean failed = false;
      String result = "";
      while (str != null) {
        System.out.println("out:" + str);
        if (str.matches("<H3>Successful execution</H3>")) {
          success = true;
          str = br.readLine();
          result = br.readLine();
        }
        if (str.matches("<H3>Failed execution</H3>")) {
          failed = true;
        }
        if (failed || success) {
          message = message + str + "\n";
        }
        str = br.readLine();
      }
      conninput.close();
      if (success) {
        getXMI(file, encoding, result);
      }
      getLog(file, encoding, message);
      mcs
          .println("-----------------------------------------------------------------------------------");
      mcs.println(message.replaceAll("<[^>]*>", ""));
    } catch (Exception e) {
      e.printStackTrace();
      mcs.print(e.getMessage());
      for (int i = 0; i < e.getStackTrace().length; i++) {
        mcs.println(e.getStackTrace()[i].toString());
      }
    }
    System.getProperties().setProperty("line.separator", lineSeparator);
  }

  private void getLog(File file, String encoding, String result)
      throws MalformedURLException, IOException, FileNotFoundException {

    mcs.println("Failed execution");
    System.out.println(result);
    String lookFor = "<LI>See the <A HREF=\"";
    int pos = result.indexOf(lookFor);
    int pos3 = result.indexOf("\"", +pos + lookFor.length() + 2);
    IPreferenceStore ips = QvtparserPlugin.getDefault()
        .getPreferenceStore();
    String strURL = ips.getString("WEB_PARSER_URL");
    // strURL = strURL.replaceAll("http://", "");
    strURL = strURL.replaceAll("http://", "");
    strURL = strURL.split("/")[0];
    String adresse = "http://" + strURL
        + result.substring(pos + lookFor.length(), pos3);
    mcs.println("adresse: " + adresse);
    URL url2 = new URL(adresse);
    URLConnection conn2 = url2.openConnection();
    conn2.setDoInput(true);
    conn2.setDoOutput(true);
    conn2.setUseCaches(false);
    conn2.setRequestProperty("Authorization", "Basic " + encoding);
    DataInputStream conninput2 = new DataInputStream(conn2.getInputStream());
    BufferedReader br2 = new BufferedReader(new InputStreamReader(
        conninput2));
    String str2 = br2.readLine();
    while (str2 != null) {
      mcs.println(str2);
      str2 = br2.readLine();
    }
  }

  private void getXMI(File file, String encoding, String result)
      throws MalformedURLException, IOException, FileNotFoundException {
    mcs.println("Successful execution");
    System.out.println(result);
    int pos = result.indexOf("<A HREF=\"/~belaunde/univ-site/tmp/");
    int pos2 = result.indexOf("<A HREF=\"", pos + 1);
    int pos3 = result.indexOf("\"", pos2 + 10);
    // System.out.println(pos2+"-"+pos3+":"+result.substring(pos2+9,pos3));
    IPreferenceStore ips = QvtparserPlugin.getDefault()
        .getPreferenceStore();
    String strURL = ips.getString("WEB_PARSER_URL");
    // strURL = strURL.replace("http://", "");
    strURL = strURL.replaceAll("http://", "");
    strURL = strURL.split("/")[0];
    String adresse = "http://" + strURL + result.substring(pos2 + 9, pos3);
    mcs.println("adresse: " + adresse);
    URL url2 = new URL(adresse);
    URLConnection conn2 = url2.openConnection();
    conn2.setDoInput(true);
    conn2.setDoOutput(true);
    conn2.setUseCaches(false);
    conn2.setRequestProperty("Authorization", "Basic " + encoding);
    DataInputStream conninput2 = new DataInputStream(conn2.getInputStream());
    BufferedReader br2 = new BufferedReader(new InputStreamReader(
        conninput2));
    String str2 = br2.readLine();
    xmiFile = new File(file.getAbsolutePath() + "operational");
    PrintStream fileout = new PrintStream(new FileOutputStream(xmiFile));
    while (str2 != null) {
      fileout.println(str2);
      str2 = br2.readLine();
    }
  }

  public File getXmiFile() {
    return xmiFile;
  }
}
TOP

Related Classes of fr.tm.elibel.smartqvt.qvtparser.WebParse

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.