Exemplo n.º 1
0
  public static String getScriptDir() {
    // If the script directory has already been determined, return it
    if (m_scriptDir != null) return m_scriptDir;

    m_scriptDir = getConfigDir() + SCRIPT_DIR + File.separator;

    try {
      // Check if the directory exists
      File dir = new File(m_scriptDir);
      if (!dir.exists()) {
        // Create the directory
        boolean created = dir.mkdirs();

        // If the directory could not be created,
        // set the script directory to config directory
        if (!created) {
          Log.logErrorRB("SU_SCRIPT_DIR_CREATE_ERROR");
          m_scriptDir = getConfigDir();
        }
      }
    } catch (SecurityException e) {
      // The system doesn't want us to write where we want to write
      // reset the script dir to the current config dir
      m_scriptDir = getConfigDir();

      // log the exception, but only after the script dir has been reset
      Log.logErrorRB("SU_SCRIPT_DIR_CREATE_ERROR");
      Log.log(e.toString());
    }
    return m_scriptDir;
  }
Exemplo n.º 2
0
  /** Download a file to the disk */
  public static void downloadFileToDisk(String address, String filename)
      throws MalformedURLException {
    URLConnection urlConn;
    InputStream in = null;
    OutputStream out = null;
    try {
      URL url = new URL(address);
      urlConn = url.openConnection();
      in = urlConn.getInputStream();
      out = new BufferedOutputStream(new FileOutputStream(filename));

      byte[] byteBuffer = new byte[1024];

      int numRead;
      while ((numRead = in.read(byteBuffer)) != -1) {
        out.write(byteBuffer, 0, numRead);
      }
    } catch (IOException ex) {
      Log.logErrorRB("IO exception");
      Log.log(ex);
    } finally {
      try {
        if (in != null) {
          in.close();
        }

        if (out != null) {
          out.close();
        }
      } catch (IOException ex) {
        // munch this
      }
    }
  }
Exemplo n.º 3
0
  /** External TMX - level 2. Replace all tags into shortcuts. */
  protected void parseSegExtLevel2() throws Exception {
    segContent.setLength(0);
    segInlineTag.setLength(0);
    inlineTagHandler.reset();

    int inlineLevel = 0;
    while (true) {
      XMLEvent e = xml.nextEvent();
      switch (e.getEventType()) {
        case XMLEvent.START_ELEMENT:
          StartElement eStart = e.asStartElement();
          if ("hi".equals(eStart.getName().getLocalPart())) {
            // tag should be skipped
            break;
          }
          inlineLevel++;
          segInlineTag.setLength(0);
          if ("bpt".equals(eStart.getName().getLocalPart())) {
            inlineTagHandler.startBPT(
                getAttributeValue(eStart, "i"), getAttributeValue(eStart, "x"));
            inlineTagHandler.setTagShortcutLetter(
                StringUtil.getFirstLetterLowercase(getAttributeValue(eStart, "type")));
          } else if ("ept".equals(eStart.getName().getLocalPart())) {
            inlineTagHandler.startEPT(getAttributeValue(eStart, "i"));
          } else if ("it".equals(eStart.getName().getLocalPart())) {
            inlineTagHandler.startOTHER();
            inlineTagHandler.setOtherTagShortcutLetter(
                StringUtil.getFirstLetterLowercase(getAttributeValue(eStart, "type")));
            inlineTagHandler.setCurrentPos(getAttributeValue(eStart, "pos"));
          } else if ("ph".equals(eStart.getName().getLocalPart())) {
            inlineTagHandler.startOTHER();
            inlineTagHandler.setOtherTagShortcutLetter(
                StringUtil.getFirstLetterLowercase(getAttributeValue(eStart, "type")));
          } else {
            inlineTagHandler.startOTHER();
          }
          break;
        case XMLEvent.END_ELEMENT:
          EndElement eEnd = e.asEndElement();
          if ("hi".equals(eEnd.getName().getLocalPart())) {
            // tag should be skipped
            break;
          }
          inlineLevel--;
          if ("seg".equals(eEnd.getName().getLocalPart())) {
            return;
          }
          boolean slashBefore = false;
          boolean slashAfter = false;
          char tagName = StringUtil.getFirstLetterLowercase(segInlineTag);
          Integer tagN;
          if ("bpt".equals(eEnd.getName().getLocalPart())) {
            if (tagName != 0) {
              inlineTagHandler.setTagShortcutLetter(tagName);
            } else {
              tagName = inlineTagHandler.getTagShortcutLetter();
            }
            tagN = inlineTagHandler.endBPT();
          } else if ("ept".equals(eEnd.getName().getLocalPart())) {
            slashBefore = true;
            tagName = inlineTagHandler.getTagShortcutLetter();
            tagN = inlineTagHandler.endEPT();
          } else if ("it".equals(eEnd.getName().getLocalPart())) {
            if (tagName != 0) {
              inlineTagHandler.setOtherTagShortcutLetter(tagName);
            } else {
              tagName = inlineTagHandler.getOtherTagShortcutLetter();
            }
            tagN = inlineTagHandler.endOTHER();
            if ("end".equals(inlineTagHandler.getCurrentPos())) {
              slashBefore = true;
            }
          } else if ("ph".equals(eEnd.getName().getLocalPart())) {
            if (tagName != 0) {
              inlineTagHandler.setOtherTagShortcutLetter(tagName);
            } else {
              tagName = inlineTagHandler.getOtherTagShortcutLetter();
            }
            tagN = inlineTagHandler.endOTHER();
            if (useSlash) {
              slashAfter = true;
            }
          } else {
            tagN = inlineTagHandler.endOTHER();
            if (useSlash) {
              slashAfter = true;
            }
          }
          if (tagName == 0) {
            tagName = 'f';
          }
          if (tagN == null) {
            // check error of TMX reading
            Log.logErrorRB(
                "TMX_ERROR_READING_LEVEL2",
                e.getLocation().getLineNumber(),
                e.getLocation().getColumnNumber());
            errorsCount++;
            segContent.setLength(0);
            // wait for end seg
            while (true) {
              XMLEvent ev = xml.nextEvent();
              switch (ev.getEventType()) {
                case XMLEvent.END_ELEMENT:
                  EndElement evEnd = (EndElement) ev;
                  if ("seg".equals(evEnd.getName().getLocalPart())) {
                    return;
                  }
              }
            }
          }

          segContent.append('<');
          if (slashBefore) {
            segContent.append('/');
          }
          segContent.append(tagName);
          segContent.append(Integer.toString(tagN));
          if (slashAfter) {
            segContent.append('/');
          }
          segContent.append('>');
          break;
        case XMLEvent.CHARACTERS:
          Characters c = (Characters) e;
          if (inlineLevel == 0) {
            segContent.append(c.getData());
          } else {
            segInlineTag.append(c.getData());
          }
          break;
      }
    }
  }
Exemplo n.º 4
0
  /**
   * Returns the location of the configuration directory, depending on the user's platform. Also
   * creates the configuration directory, if necessary. If any problems occur while the location of
   * the configuration directory is being determined, an empty string will be returned, resulting in
   * the current working directory being used.
   *
   * <p>Windows XP : &lt;Documents and Settings&gt;>\&lt;User name&gt;\Application Data\OmegaT
   * Windows Vista : User\&lt;User name&gt;\AppData\Roaming Linux: &lt;User Home&gt;/.omegat
   * Solaris/SunOS: &lt;User Home&gt;/.omegat FreeBSD: &lt;User Home&gt;/.omegat Mac OS X: &lt;User
   * Home&gt;/Library/Preferences/OmegaT Other: User home directory
   *
   * @return The full path of the directory containing the OmegaT configuration files, including
   *     trailing path separator.
   * @author Henry Pijffers ([email protected])
   */
  public static String getConfigDir() {
    // if the configuration directory has already been determined, return it
    if (m_configDir != null) return m_configDir;

    String cd = RuntimePreferences.getConfigDir();
    if (cd != null) {
      // use the forced specified directory
      m_configDir = new File(cd).getAbsolutePath() + File.separator;
      return m_configDir;
    }

    OsType os = Platform.getOsType(); // name of operating system
    String home; // user home directory

    // get os and user home properties
    try {
      // get the user's home directory
      home = System.getProperty("user.home");
    } catch (SecurityException e) {
      // access to the os/user home properties is restricted,
      // the location of the config dir cannot be determined,
      // set the config dir to the current working dir
      m_configDir = new File(".").getAbsolutePath() + File.separator;

      // log the exception, only do this after the config dir
      // has been set to the current working dir, otherwise
      // the log method will probably fail
      Log.logErrorRB("SU_USERHOME_PROP_ACCESS_ERROR");
      Log.log(e.toString());

      return m_configDir;
    }

    // if os or user home is null or empty, we cannot reliably determine
    // the config dir, so we use the current working dir (= empty string)
    if ((os == null) || (home == null) || (home.length() == 0)) {
      // set the config dir to the current working dir
      m_configDir = new File(".").getAbsolutePath() + File.separator;
      return m_configDir;
    }

    // check for Windows versions
    if (os == OsType.WIN32 || os == OsType.WIN64) {
      String appData = null;

      // We do not use %APPDATA%
      // Trying first Vista/7, because "Application Data" exists also as virtual folder,
      // so we would not be able to differentiate with 2000/XP otherwise
      File appDataFile = new File(home, "AppData\\Roaming");
      if (appDataFile.exists()) {
        appData = appDataFile.getAbsolutePath();
      } else {
        // Trying to locate "Application Data" for 2000 and XP
        // C:\Documents and Settings\<User>\Application Data
        appDataFile = new File(home, "Application Data");
        if (appDataFile.exists()) {
          appData = appDataFile.getAbsolutePath();
        }
      }

      if ((appData != null) && (appData.length() > 0)) {
        // if a valid application data dir has been found,
        // append an OmegaT subdir to it
        m_configDir = appData + WINDOWS_CONFIG_DIR;
      } else {
        // otherwise set the config dir to the user's home directory,
        // usually
        // C:\Documents and Settings\<User>\OmegaT
        m_configDir = home + WINDOWS_CONFIG_DIR;
      }
    }
    // Check for UNIX varieties
    // Solaris is generally detected as SunOS
    else if (os == OsType.LINUX32 || os == OsType.LINUX64 || os == OsType.OTHER) {
      // set the config dir to the user's home dir + "/.omegat/", so it's
      // hidden
      m_configDir = home + UNIX_CONFIG_DIR;
    }
    // check for Mac OS X
    else if (Platform.isMacOSX()) {
      // set the config dir to the user's home dir +
      // "/Library/Preferences/OmegaT/"
      m_configDir = home + OSX_CONFIG_DIR;
    }
    // other OSes / default
    else {
      // use the user's home directory by default
      m_configDir = home + File.separator;
    }

    // create the path to the configuration dir, if necessary
    if (m_configDir.length() > 0) {
      try {
        // check if the dir exists
        File dir = new File(m_configDir);
        if (!dir.exists()) {
          // create the dir
          boolean created = dir.mkdirs();

          // if the dir could not be created,
          // set the config dir to the current working dir
          if (!created) {
            Log.logErrorRB("SU_CONFIG_DIR_CREATE_ERROR");
            m_configDir = new File(".").getAbsolutePath() + File.separator;
          }
        }
      } catch (SecurityException e) {
        // the system doesn't want us to write where we want to write
        // reset the config dir to the current working dir
        m_configDir = new File(".").getAbsolutePath() + File.separator;

        // log the exception, but only after the config dir has been
        // reset
        Log.logErrorRB("SU_CONFIG_DIR_CREATE_ERROR");
        Log.log(e.toString());
      }
    }

    // we should have a correct, existing config dir now
    return m_configDir;
  }