Search in sources :

Example 81 with FileReader

use of java.io.FileReader in project translationstudio8 by heartsome.

the class XliffReader method readXliffFile.

/**
	 * After parse file, this method read the xliff file from the parse info
	 * @param xlfOs
	 *            the xliff file out put stream
	 * @param iniFile
	 *            the configure file of mif,witch define the ESC
	 * @param isReadMasterPage
	 *            xliff contain the masterpage or not
	 * @param monitor
	 *            the progress monitor
	 * @throws MifParseException
	 * @throws IOException
	 *             ;
	 * @throws UnSuportedFileExcetption
	 */
public void readXliffFile(BufferedWriter xlfOs, String iniFile, boolean isReadMasterPage, IProgressMonitor monitor) throws MifParseException, IOException, UnSuportedFileExcetption {
    if (monitor == null) {
        monitor = new NullProgressMonitor();
    }
    rUtil = new ReaderUtil(iniFile);
    File xf = File.createTempFile("xliftemp", ".temp");
    // first read the index
    List<Marker> markers = mpbf.getMarkers();
    if (markers.size() > 0) {
        File f = File.createTempFile("miftemp", ".tmp");
        BufferedWriter tmpWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f), mifEncoding));
        try {
            this.xlfOs = new BufferedWriter(new FileWriter(xf));
            // generate the xliff content to temporary file xf
            for (Marker m : markers) {
                outputSegmenet(rUtil.cleanString(m.getContent()), m.getOffset(), m.getEndOffset(), 1);
            }
            // generate temporary file ,the file had extracted index content and contains '%%%index%%%'
            readSkeletonFile(tmpWriter, null);
        } finally {
            if (tmpWriter != null) {
                tmpWriter.close();
            }
            if (this.xlfOs != null) {
                this.xlfOs.close();
            }
        }
        // reload the temporary file
        loadFile(f.getAbsolutePath(), mifEncoding);
        // after reload delete the temporary file
        f.delete();
    }
    // second read the content
    this.xlfOs = xlfOs;
    List<Page> pages = mpbf.getPages();
    monitor.setTaskName(Messages.getString("mif.Mif2Xliff.task3"));
    if (pages.size() == 0) {
        // no pages specified ,direct read the Para statement
        monitor.beginTask(Messages.getString("mif.Mif2Xliff.task3"), 1);
        monitor.worked(1);
        readPara();
    } else {
        monitor.beginTask(Messages.getString("mif.Mif2Xliff.task3"), pages.size());
        for (Page page : pages) {
            String pageType = page.getPageType().toLowerCase();
            if (pageType.equals("bodypage")) {
                readPage(page);
            } else if (pageType.indexOf("masterpage") != -1 && isReadMasterPage) {
                readPage(page);
            }
            monitor.worked(1);
        }
    }
    BufferedReader r = null;
    try {
        r = new BufferedReader(new FileReader(xf));
        String line = r.readLine();
        while (line != null) {
            this.xlfOs.write(line + "\n");
            line = r.readLine();
        }
    } finally {
        if (r != null) {
            r.close();
        }
        xf.delete();
    }
    monitor.done();
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) FileWriter(java.io.FileWriter) ReaderUtil(net.heartsome.cat.converter.mif.common.ReaderUtil) Page(net.heartsome.cat.converter.mif.bean.Page) Marker(net.heartsome.cat.converter.mif.bean.Marker) BufferedWriter(java.io.BufferedWriter) FileOutputStream(java.io.FileOutputStream) BufferedReader(java.io.BufferedReader) OutputStreamWriter(java.io.OutputStreamWriter) FileReader(java.io.FileReader) File(java.io.File)

Example 82 with FileReader

use of java.io.FileReader in project translationstudio8 by heartsome.

the class SystemPreferenceInitializer method initializeDefaultPreferences.

@Override
public void initializeDefaultPreferences() {
    IPreferenceStore store = Activator.getDefault().getPreferenceStore();
    store.setDefault(IPreferenceConstants.SYSTEM_AUTO_UPDATE, IPreferenceConstants.SYSTEM_CHECK_UPDATE_WITH_WEEKLY);
    store.setDefault(IPreferenceConstants.SYSTEM_CHECK_UPDATE_WITH_WEEKLY_DATE, 2);
    store.setDefault(IPreferenceConstants.SYSTEM_LANGUAGE, IPreferenceConstants.SYSTEM_LANGUAGE_WITH_EN);
    // 默认语言从产品的 ini 文件中取值
    Location configArea = Platform.getInstallLocation();
    String locale = "en";
    URL location = null;
    try {
        location = new URL(configArea.getURL().toExternalForm() + "configuration" + File.separator + "config.ini");
    } catch (MalformedURLException e) {
        // This should never happen
        LOGGER.error(Messages.getString("preferencepage.SystemPreferenceInitializer.logger1"), e);
    }
    try {
        String fileName = location.getFile();
        BufferedReader in = new BufferedReader(new FileReader(fileName));
        boolean isNl = false;
        String line = in.readLine();
        while (line != null) {
            if (line.startsWith("osgi.nl=")) {
                isNl = true;
                locale = line.substring("osgi.nl=".length()).trim();
                break;
            }
            line = in.readLine();
        }
        in.close();
        if (!isNl) {
            locale = "en";
        }
    } catch (FileNotFoundException e) {
        LOGGER.error(Messages.getString("preferencepage.SystemPreferenceInitializer.logger1"), e);
    } catch (IOException e) {
        LOGGER.error("", e);
    }
    if (locale != null) {
        if (locale.startsWith("en")) {
            CommonFunction.setSystemLanguage("en");
            store.setValue(IPreferenceConstants.SYSTEM_LANGUAGE, IPreferenceConstants.SYSTEM_LANGUAGE_WITH_EN);
        } else if (locale.startsWith("zh")) {
            CommonFunction.setSystemLanguage("zh");
            store.setValue(IPreferenceConstants.SYSTEM_LANGUAGE, IPreferenceConstants.SYSTEM_LANGUAGE_WITH_ZH_CN);
        }
    }
    store.setDefault(IPreferenceConstants.SYSTEM_USER, System.getProperty("user.name"));
    //将用户保存到平台首选项中
    PlatformUI.getPreferenceStore().setDefault(IPreferenceConstants.SYSTEM_USER, store.getDefaultString(IPreferenceConstants.SYSTEM_USER));
    FontData fd = JFaceResources.getDefaultFont().getFontData()[0];
    store.setDefault(IPreferenceConstants.XLIFF_EDITOR_FONT_NAME, fd.getName());
    int fontSize = fd.getHeight();
    store.setDefault(IPreferenceConstants.XLIFF_EDITOR_FONT_SIZE, fontSize < 13 ? 13 : fontSize);
    store.setDefault(IPreferenceConstants.MATCH_VIEW_FONT_NAME, fd.getName());
    store.setDefault(IPreferenceConstants.MATCH_VIEW_FONT_SIZE, fontSize < 13 ? 13 : fontSize);
    store.setDefault(IPreferenceConstants.XLIFF_EDITOR_SHOWHIDEN_NONPRINTCHARACTER, false);
}
Also used : MalformedURLException(java.net.MalformedURLException) FontData(org.eclipse.swt.graphics.FontData) BufferedReader(java.io.BufferedReader) FileNotFoundException(java.io.FileNotFoundException) FileReader(java.io.FileReader) IOException(java.io.IOException) IPreferenceStore(org.eclipse.jface.preference.IPreferenceStore) URL(java.net.URL) Location(org.eclipse.osgi.service.datalocation.Location)

Example 83 with FileReader

use of java.io.FileReader in project translationstudio8 by heartsome.

the class SystemPreferencePage method changeLocale.

/**
	 * 修改产品 ini 文件中的语言
	 */
private void changeLocale(String locale) {
    Location configArea = Platform.getInstallLocation();
    if (configArea == null) {
        return;
    }
    URL location = null;
    try {
        location = new URL(configArea.getURL().toExternalForm() + "configuration" + File.separator + "config.ini");
    } catch (MalformedURLException e) {
    // This should never happen
    }
    try {
        String fileName = location.getFile();
        File file = new File(fileName);
        fileName += ".bak";
        file.renameTo(new File(fileName));
        BufferedReader in = new BufferedReader(new FileReader(fileName));
        BufferedWriter out = new BufferedWriter(new FileWriter(location.getFile()));
        try {
            String line = in.readLine();
            while (line != null) {
                if (line.startsWith("osgi.nl=")) {
                    out.write("osgi.nl=" + locale);
                } else {
                    out.write(line);
                }
                out.newLine();
                line = in.readLine();
            }
            out.flush();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            File tmpFile = new File(location.getFile() + ".bak");
            if (tmpFile.exists()) {
                tmpFile.delete();
            }
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) FileWriter(java.io.FileWriter) BufferedReader(java.io.BufferedReader) FileNotFoundException(java.io.FileNotFoundException) FileReader(java.io.FileReader) IOException(java.io.IOException) File(java.io.File) URL(java.net.URL) Location(org.eclipse.osgi.service.datalocation.Location) BufferedWriter(java.io.BufferedWriter)

Example 84 with FileReader

use of java.io.FileReader in project hazelcast by hazelcast.

the class DiagnosticsLogTest method loadLogfile.

private String loadLogfile() {
    File file = diagnosticsLogFile.file;
    if (file == null || !file.exists()) {
        return null;
    }
    try {
        BufferedReader br = new BufferedReader(new FileReader(file));
        try {
            StringBuilder sb = new StringBuilder();
            String line = br.readLine();
            while (line != null) {
                sb.append(line);
                sb.append(LINE_SEPARATOR);
                line = br.readLine();
            }
            return sb.toString();
        } finally {
            br.close();
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) IOException(java.io.IOException) File(java.io.File)

Example 85 with FileReader

use of java.io.FileReader in project translationstudio8 by heartsome.

the class EncodingResolver method getHTMLEncoding.

private static String getHTMLEncoding(String fileName) {
    // return UTF-8 as default
    //$NON-NLS-1$
    String result = "UTF-8";
    try {
        FileReader input = new FileReader(fileName);
        BufferedReader buffer = new BufferedReader(input);
        String line;
        Pattern pattern = Pattern.compile("[a-zA-Z-_\\d]+");
        while ((line = buffer.readLine()) != null) {
            int index = line.indexOf("charset=");
            if (index != -1) {
                Matcher matcher = pattern.matcher(line.substring(index + "charset=".length()));
                if (matcher.find()) {
                    result = matcher.group();
                    break;
                }
            }
        }
        input.close();
    } catch (Exception e) {
        LOGGER.error("", e);
        e.printStackTrace();
    }
    String[] encodings = TextUtil.getPageCodes();
    for (int i = 0; i < encodings.length; i++) {
        if (encodings[i].equalsIgnoreCase(result)) {
            return encodings[i];
        }
    }
    // 如果不是有效的编码就返回 UTF-8
    return "UTF-8";
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Aggregations

FileReader (java.io.FileReader)1873 BufferedReader (java.io.BufferedReader)1289 IOException (java.io.IOException)893 File (java.io.File)811 FileNotFoundException (java.io.FileNotFoundException)304 ArrayList (java.util.ArrayList)274 Test (org.junit.Test)197 FileWriter (java.io.FileWriter)148 HashMap (java.util.HashMap)116 Reader (java.io.Reader)99 BufferedWriter (java.io.BufferedWriter)98 Properties (java.util.Properties)66 InputStreamReader (java.io.InputStreamReader)64 LineNumberReader (java.io.LineNumberReader)61 Matcher (java.util.regex.Matcher)61 Map (java.util.Map)59 List (java.util.List)56 PrintWriter (java.io.PrintWriter)51 StringTokenizer (java.util.StringTokenizer)51 HashSet (java.util.HashSet)50