List of usage examples for java.io LineNumberReader close
public void close() throws IOException
From source file:org.grails.web.errors.GrailsWrappedRuntimeException.java
/** * @param servletContext The ServletContext instance * @param t The exception that was thrown *///from www. j a v a 2s . co m public GrailsWrappedRuntimeException(ServletContext servletContext, Throwable t) { super(t.getMessage(), t); this.cause = t; Throwable cause = t; FastStringPrintWriter pw = FastStringPrintWriter.newInstance(); cause.printStackTrace(pw); stackTrace = pw.toString(); while (cause.getCause() != cause) { if (cause.getCause() == null) { break; } cause = cause.getCause(); } stackTraceLines = stackTrace.split("\\n"); if (cause instanceof MultipleCompilationErrorsException) { MultipleCompilationErrorsException mcee = (MultipleCompilationErrorsException) cause; Object message = mcee.getErrorCollector().getErrors().iterator().next(); if (message instanceof SyntaxErrorMessage) { SyntaxErrorMessage sem = (SyntaxErrorMessage) message; lineNumber = sem.getCause().getLine(); className = sem.getCause().getSourceLocator(); sem.write(pw); } } else { Matcher m1 = PARSE_DETAILS_STEP1.matcher(stackTrace); Matcher m2 = PARSE_DETAILS_STEP2.matcher(stackTrace); Matcher gsp = PARSE_GSP_DETAILS_STEP1.matcher(stackTrace); try { if (gsp.find()) { className = gsp.group(2); lineNumber = Integer.parseInt(gsp.group(3)); gspFile = URL_PREFIX + "views/" + gsp.group(1) + '/' + className; } else { if (m1.find()) { do { className = m1.group(1); lineNumber = Integer.parseInt(m1.group(2)); } while (m1.find()); } else { while (m2.find()) { className = m2.group(1); lineNumber = Integer.parseInt(m2.group(2)); } } } } catch (NumberFormatException nfex) { // ignore } } LineNumberReader reader = null; try { checkIfSourceCodeAware(t); checkIfSourceCodeAware(cause); if (getLineNumber() > -1) { String fileLocation; String url = null; if (fileName != null) { fileLocation = fileName; } else { String urlPrefix = ""; if (gspFile == null) { fileName = className.replace('.', '/') + ".groovy"; GrailsApplication application = WebApplicationContextUtils .getRequiredWebApplicationContext(servletContext) .getBean(GrailsApplication.APPLICATION_ID, GrailsApplication.class); // @todo Refactor this to get the urlPrefix from the ArtefactHandler if (application.isArtefactOfType(ControllerArtefactHandler.TYPE, className)) { urlPrefix += "/controllers/"; } else if (application.isArtefactOfType(TagLibArtefactHandler.TYPE, className)) { urlPrefix += "/taglib/"; } else if (application.isArtefactOfType(ServiceArtefactHandler.TYPE, className)) { urlPrefix += "/services/"; } url = URL_PREFIX + urlPrefix + fileName; } else { url = gspFile; GrailsApplicationAttributes attrs = null; try { attrs = grailsApplicationAttributesConstructor.newInstance(servletContext); } catch (Exception e) { ReflectionUtils.rethrowRuntimeException(e); } ResourceAwareTemplateEngine engine = attrs.getPagesTemplateEngine(); lineNumber = engine.mapStackLineNumber(url, lineNumber); } fileLocation = "grails-app" + urlPrefix + fileName; } InputStream in = null; if (!GrailsStringUtils.isBlank(url)) { in = servletContext.getResourceAsStream(url); LOG.debug("Attempting to display code snippet found in url " + url); } if (in == null) { Resource r = null; try { r = resolver.getResource(fileLocation); in = r.getInputStream(); } catch (Throwable e) { r = resolver.getResource("file:" + fileLocation); if (r.exists()) { try { in = r.getInputStream(); } catch (IOException e1) { // ignore } } } } if (in != null) { reader = new LineNumberReader(new InputStreamReader(in, "UTF-8")); String currentLine = reader.readLine(); StringBuilder buf = new StringBuilder(); while (currentLine != null) { int currentLineNumber = reader.getLineNumber(); if ((lineNumber > 0 && currentLineNumber == lineNumber - 1) || (currentLineNumber == lineNumber)) { buf.append(currentLineNumber).append(": ").append(currentLine).append("\n"); } else if (currentLineNumber == lineNumber + 1) { buf.append(currentLineNumber).append(": ").append(currentLine); break; } currentLine = reader.readLine(); } codeSnippet = buf.toString().split("\n"); } } } catch (IOException e) { LOG.warn("[GrailsWrappedRuntimeException] I/O error reading line diagnostics: " + e.getMessage(), e); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { // ignore } } } }
From source file:org.agnitas.web.ImportWizardForm.java
/** * check in the columnMapping for the key column, and eventually for gender * and mailtype read first csv line again; do not parse (allready parsed in * parseFirstline) prepare download-files for errors and parsed data read * the rest of the csv-file/*from w ww . j av a 2 s .co m*/ */ protected ActionErrors parseContent(HttpServletRequest req) { ApplicationContext aContext = this.getWebApplicationContext(); JdbcTemplate jdbc = new JdbcTemplate((DataSource) aContext.getBean("dataSource")); LinkedList aLineContent = null; String firstline = null; String csvString = new String(""); ActionErrors errors = new ActionErrors(); boolean hasGENDER = false; boolean hasMAILTYPE = false; boolean hasKeyColumn = false; this.uniqueValues = new HashSet(); this.parsedContent = new LinkedList(); this.linesOK = 0; // this.csvMaxUsedColumn=0; this.dbInsertStatus = 0; try { csvString = new String(this.getCsvFile().getFileData(), status.getCharset()); } catch (Exception e) { AgnUtils.logger().error("parseContent: " + e); errors.add("global", new ActionMessage("error.import.charset")); return errors; } try { this.loadBlacklist(this.getCompanyID(req), jdbc); } catch (Exception e) { errors.add("global", new ActionMessage("import.blacklist.read")); return errors; } LineNumberReader aReader = new LineNumberReader(new StringReader(csvString)); String myline = null; // check in the columnMapping for the key column, // and eventually for gender and mailtype: String aKey = ""; CsvColInfo aCol = null; Enumeration aMapEnu = this.columnMapping.keys(); while (aMapEnu.hasMoreElements()) { aKey = (String) aMapEnu.nextElement(); aCol = (CsvColInfo) this.columnMapping.get(aKey); if (aCol.getName().equalsIgnoreCase(GENDER_KEY)) { hasGENDER = true; } if (aCol.getName().equalsIgnoreCase(MAILTYPE_KEY)) { hasMAILTYPE = true; } if (aCol.getName().equalsIgnoreCase(this.status.getKeycolumn())) { hasKeyColumn = true; } } if (!hasKeyColumn) { errors.add("global", new ActionMessage("error.import.no_keycolumn_mapping")); } if (this.getMode() == ImportWizardForm.MODE_ADD || this.getMode() == ImportWizardForm.MODE_ADD_UPDATE) { if (!hasGENDER) { errors.add("global", new ActionMessage("error.import.no_gender_mapping")); } if (!hasMAILTYPE) { errors.add("global", new ActionMessage("error.import.no_mailtype_mapping")); } } try { // read first csv line again; do not parse (allready parsed in // parseFirstline): if ((myline = aReader.readLine()) != null) { firstline = myline; } // prepare download-files for errors and parsed data errorData.put(DATE_ERROR, new StringBuffer(firstline + '\n')); errorData.put(EMAIL_ERROR, new StringBuffer(firstline + '\n')); errorData.put(EMAILDOUBLE_ERROR, new StringBuffer(firstline + '\n')); errorData.put(GENDER_ERROR, new StringBuffer(firstline + '\n')); errorData.put(MAILTYPE_ERROR, new StringBuffer(firstline + '\n')); errorData.put(NUMERIC_ERROR, new StringBuffer(firstline + '\n')); errorData.put(STRUCTURE_ERROR, new StringBuffer(firstline + '\n')); errorData.put(BLACKLIST_ERROR, new StringBuffer(firstline + '\n')); parsedData = new StringBuffer(firstline + '\n'); // read the rest of the csv-file: // StringTokenizer file = new StringTokenizer(csvString, "\n"); if (errors.isEmpty()) { readlines = 0; int maxrows = BLOCK_SIZE; this.linesOK = 0; while ((myline = aReader.readLine()) != null && this.linesOK < maxrows) { // Bug-Fix just read the first 1000 lines to avoid trouble with heap space if (myline.trim().length() > 0) { aLineContent = parseLine(myline, (Locale) req.getSession().getAttribute(org.apache.struts.Globals.LOCALE_KEY)); if (aLineContent != null) { parsedContent.add(aLineContent); this.parsedData.append(myline + "\n"); this.linesOK++; } } readlines++; } aReader.close(); } } catch (Exception e) { AgnUtils.logger().error("parseContent: " + e); } return errors; }
From source file:com.castis.sysComp.PoisConverterSysComp.java
public void parseViewCountFile(File file) throws Exception { String line = ""; FileInputStream in = null;/*from w w w .java 2 s. c om*/ Reader isReader = null; LineNumberReader bufReader = null; if (file.getName().contains("channel")) { type = "channel"; } else { type = "vod"; } try { String encodingCharset = FILE_CHARSET; in = new FileInputStream(file); isReader = new InputStreamReader(in, encodingCharset); bufReader = new LineNumberReader(isReader); boolean first = true; while ((line = bufReader.readLine()) != null) { if (line.length() == 0) { continue; } InputDataDTO data = new InputDataDTO(); String result[] = line.split("\\|"); if (first == true && result.length <= 1) { first = false; continue; } if (result[0] == null || result[0].equals("")) { throw new DataParsingException("data parsing error(region)"); } if (result[1] == null || result[1].equals("")) { throw new DataParsingException("data parsing error(category)"); } if (result[2] == null || result[2].equals("")) { throw new DataParsingException("data parsing error(weekday)"); } if (result[3] == null || result[3].equals("")) { throw new DataParsingException("data parsing error(hour)"); } if (result[4] == null || result[4].equals("")) { throw new DataParsingException("data parsing error(platform)"); } if (result[5] == null || result[5].equals("")) { throw new DataParsingException("data parsing error(count)"); } String platform = result[4]; if (platform != null && platform.equalsIgnoreCase("stb")) platform = "STB"; else if (platform != null && platform.equalsIgnoreCase("mobile")) { platform = "Mobile"; } data.setPlatform(platform); List<TreeNodeDTO> tree = treeMap.get(platform); if (tree == null) { tree = getAxis(platform); treeMap.put(platform, tree); } String fullpath = getFullPath(tree, result[0]); if (fullpath == "") { throw new DataParsingException("region ID[" + result[0] + "] can't find fullpath."); } data.setRegion(fullpath); data.setCategory(result[1]); data.setWeekday(result[2]); data.setHour(result[3]); data.setCount(Integer.parseInt(result[5])); insertTreeNode(data); } for (WeekHourPair key : trees.keySet()) { Map<String, TreeNodeForCount> map = trees.get(key); calculateCount(map); } writeFile(file); } catch (Exception e) { String errorMsg = "Fail to parsing Line.[current line(" + bufReader.getLineNumber() + ") :" + line + "] : "; log.error(errorMsg, e); throw new DataParsingException(errorMsg, e); //throw(e); } finally { if (in != null) in.close(); if (isReader != null) isReader.close(); if (bufReader != null) bufReader.close(); } }
From source file:com.castis.sysComp.PoisConverterSysComp.java
private void writeFile(File file) throws IOException { Map<String, String> middleNodeMap = new HashMap<String, String>(); String line = ""; FileInputStream in = null;//from w w w. j a v a 2 s. c o m Reader isReader = null; LineNumberReader bufReader = null; FileOutputStream fos = null; String dir = filePolling.getValidFileDirectory(resultDir); String fileName = file.getName(); int index = fileName.indexOf("-"); if (index != -1) { fileName = fileName.substring(index + 1, fileName.length()); } String tempDir = dir + "/temp/"; File targetDirectory = new File(CiFileUtil.getReplaceFullPath(tempDir)); if (!targetDirectory.isDirectory()) { CiFileUtil.createDirectory(tempDir); } fos = new FileOutputStream(tempDir + fileName); int byteSize = 2048; ByteBuffer byteBuffer = ByteBuffer.allocateDirect(byteSize); GatheringByteChannel outByteCh = fos.getChannel(); try { String encodingCharset = FILE_CHARSET; in = new FileInputStream(file); isReader = new InputStreamReader(in, encodingCharset); bufReader = new LineNumberReader(isReader); boolean first = true; while ((line = bufReader.readLine()) != null) { if (line.length() == 0) { continue; } InputDataDTO data = new InputDataDTO(); String result[] = line.split("\\|"); if (first == true && result.length <= 1) { first = false; continue; } String platform = result[4]; if (platform != null && platform.equalsIgnoreCase("stb")) platform = "STB"; else if (platform != null && platform.equalsIgnoreCase("mobile")) { platform = "Mobile"; } data.setPlatform(platform); List<TreeNodeDTO> tree = treeMap.get(platform); if (tree == null) { tree = getAxis(platform); treeMap.put(platform, tree); } String fullpath = getFullPath(tree, result[0]); data.setRegion(fullpath); data.setCategory(result[1]); data.setWeekday(result[2]); data.setHour(result[3]); data.setCount(Integer.parseInt(result[5])); List<subDataDTO> subDataList = writeNodeInfoOnFile(byteSize, byteBuffer, outByteCh, data, "Y"); if (subDataList != null && subDataList.size() > 0) { writeMiddleNode(byteSize, byteBuffer, outByteCh, data, middleNodeMap, subDataList, "N"); } } fos.close(); index = fileName.indexOf("_"); String targetDir = resultDir; File sourceFile = new File(tempDir + fileName); if (index != -1) { String directory = fileName.substring(0, index); targetDir += "/viewCount/" + directory; } try { File resultTargetDir = new File(CiFileUtil.getReplaceFullPath(targetDir)); if (!resultTargetDir.isDirectory()) { CiFileUtil.createDirectory(targetDir); } CiFileUtil.renameFile(sourceFile, targetDir, fileName); } catch (Exception e) { log.error(e.getMessage()); } } catch (Exception e) { String errorMsg = "Fail to parsing Line.[current line(" + bufReader.getLineNumber() + ") :" + line + "] : "; log.error(errorMsg, e); throw new DataParsingException(errorMsg, e); //throw(e); } finally { if (in != null) in.close(); if (isReader != null) isReader.close(); if (bufReader != null) bufReader.close(); } }
From source file:ua.utility.kfsdbupgrade.App.java
/** * From the {@link #upgradeRoot}/* w w w .j a v a 2 s . c o m*/ * <code>/post-upgrade/sql/kfs-indexes.sql</code> file, create any indices * that are present in the SQL file but in the database that is being worked * against TODO there's more going on here... come back after digging * through submethods * * @param conn * @param stmt */ protected boolean createExistingIndexes(Connection conn, Statement stmt, File kfsIndexesSqlFile) { boolean success = true; LineNumberReader lnr = null; logHeader2("creating KFS indexes that existed prior to upgrade where required "); try { lnr = new LineNumberReader(new FileReader(kfsIndexesSqlFile)); String line = null; while ((line = lnr.readLine()) != null) { if (StringUtils.isNotBlank(line) && line.startsWith("--")) { // Skip lines starting with a comment continue; } String tableName = getIndexTableName(line); String indexName = getIndexName(line); if (StringUtils.isNotBlank(tableName) && StringUtils.isNotBlank(indexName)) { if (tableExists(conn, stmt, tableName)) { boolean unique = line.contains(" UNIQUE "); List<String> columnNames = getIndexColumnNames(line); if (!indexExists(conn, stmt, tableName, columnNames)) { if (indexNameExists(conn, stmt, tableName, indexName)) { indexName = getNextTableIndexName(conn, stmt, tableName); } StringBuilder sql = new StringBuilder(256); sql.append("CREATE "); if (unique) { sql.append("UNIQUE "); } sql.append("INDEX KULOWNER."); sql.append(indexName); sql.append(" ON KULOWNER."); sql.append(tableName); sql.append("("); String comma = ""; for (String columnName : columnNames) { sql.append(comma); sql.append(columnName); comma = ","; } sql.append(")"); try { stmt.execute(sql.toString()); } catch (SQLException ex) { success = false; LOGGER.error("failed to create index: " + sql.toString(), ex); } } } } } } catch (Exception ex) { success = false; LOGGER.error(ex); } finally { try { if (lnr != null) { lnr.close(); } } catch (Exception ex) { } ; } postUpgradeFilesProcessed.add(kfsIndexesSqlFile); return success; }
From source file:com.castis.sysComp.PoisConverterSysComp.java
public void parseRegionFile(File file) throws Exception { String line = ""; FileInputStream in = null;/*from www .j a va 2 s .c om*/ Reader isReader = null; LineNumberReader bufReader = null; FileOutputStream fos = null; String fileName = file.getName(); int index = fileName.indexOf("-"); if (index != -1) { fileName = fileName.substring(index + 1, fileName.length()); } String dir = filePolling.getValidFileDirectory(resultDir); String tempDir = dir + "/temp/"; File targetDirectory = new File(CiFileUtil.getReplaceFullPath(tempDir)); if (!targetDirectory.isDirectory()) { CiFileUtil.createDirectory(tempDir); } fos = new FileOutputStream(tempDir + fileName); int byteSize = 2048; ByteBuffer byteBuffer = ByteBuffer.allocateDirect(byteSize); GatheringByteChannel outByteCh = fos.getChannel(); try { in = new FileInputStream(file); isReader = new InputStreamReader(in, "UTF-16LE"); bufReader = new LineNumberReader(isReader); boolean first = true; while ((line = bufReader.readLine()) != null) { byte[] utf8 = line.getBytes("UTF-8"); String string = new String(utf8, "UTF-8"); String data[] = string.split("\t"); if (first == true) { first = false; if (data[0] == null || data[0].contains("region") == false) { throw new DataParsingException("data parsing error(not formatted)"); } continue; } if (data[0] == null || data[0].equals("")) { throw new DataParsingException("data parsing error(region id)"); } if (data[1] == null || data[1].equals("")) { throw new DataParsingException("data parsing error(region name)"); } if (data[2] == null || data[2].equals("")) { throw new DataParsingException("data parsing error(parent id)"); } StringBuffer strBuffer = new StringBuffer(); strBuffer.append(data[0]); strBuffer.append("\t"); strBuffer.append(data[1]); strBuffer.append("\t"); strBuffer.append(data[2]); strBuffer.append("\r\n"); byte[] outByte = null; try { outByte = strBuffer.toString().getBytes("UTF-8"); } catch (UnsupportedEncodingException e2) { e2.printStackTrace(); } byteBuffer.put(outByte); byteBuffer.flip(); try { outByteCh.write(byteBuffer); } catch (IOException e) { } byteBuffer.clear(); } fos.close(); index = fileName.indexOf("_"); String targetDir = resultDir; File sourceFile = new File(tempDir + fileName); if (index != -1) { String directory = fileName.substring(0, index); targetDir += "/" + directory; } try { File resultTargetDir = new File(CiFileUtil.getReplaceFullPath(targetDir)); if (!resultTargetDir.isDirectory()) { CiFileUtil.createDirectory(targetDir); } CiFileUtil.renameFile(sourceFile, targetDir, fileName); } catch (Exception e) { log.error(e.getMessage()); } } catch (Exception e) { String errorMsg = "Fail to parsing Line.[current line(" + bufReader.getLineNumber() + ") :" + line + "] : "; log.error(errorMsg, e); throw new DataParsingException(errorMsg, e); //throw(e); } finally { if (in != null) in.close(); if (isReader != null) isReader.close(); if (bufReader != null) bufReader.close(); } }
From source file:org.kalypso.model.wspm.tuhh.schema.simulation.PolynomeReader.java
public void read(final File polyFile) throws IOException { LineNumberReader reader = null; try {//from w w w.j a va2 s . c o m reader = new LineNumberReader(new FileReader(polyFile)); while (reader.ready()) { final String line = reader.readLine(); if (line == null) break; final String trimmedLine = line.trim().replaceAll(" \\(h\\)", "\\(h\\)"); //$NON-NLS-1$ //$NON-NLS-2$ final String[] tokens = trimmedLine.split(" +"); //$NON-NLS-1$ if (tokens.length < 8) continue; /* Determine if this is a good line: good lines are lines whos first token is a number */ final BigDecimal station; try { station = new BigDecimal(tokens[0]); } catch (final NumberFormatException nfe) { /* Just ignore this line */ continue; } try { final String description = tokens[1]; // final String whatIsN = tokens[2]; final char type = tokens[3].charAt(0); final int order = Integer.parseInt(tokens[4]); final double rangeMin = Double.parseDouble(tokens[5].replace('D', 'E')); final double rangeMax = Double.parseDouble(tokens[6].replace('D', 'E')); if (tokens.length < 7 + order + 1) { /* A good line but bad content. Give user a hint that something might be wrong. */ m_log.log(false, Messages.getString( "org.kalypso.model.wspm.tuhh.schema.simulation.PolynomeProcessor.22"), //$NON-NLS-1$ polyFile.getName(), reader.getLineNumber()); continue; } final List<Double> coefficients = new ArrayList<>(order); for (int i = 7; i < 7 + order + 1; i++) { final double coeff = Double.parseDouble(tokens[i].replace('D', 'E')); coefficients.add(coeff); } final Double[] doubles = coefficients.toArray(new Double[coefficients.size()]); final double[] coeffDoubles = ArrayUtils.toPrimitive(doubles); final String domainId; final String rangeId = IWspmTuhhQIntervallConstants.DICT_PHENOMENON_WATERLEVEL; switch (type) { case 'Q': domainId = IWspmTuhhQIntervallConstants.DICT_PHENOMENON_RUNOFF; break; case 'A': domainId = IWspmTuhhQIntervallConstants.DICT_PHENOMENON_AREA; break; case 'a': domainId = IWspmTuhhQIntervallConstants.DICT_PHENOMENON_ALPHA; break; default: m_log.log(false, Messages.getString( "org.kalypso.model.wspm.tuhh.schema.simulation.PolynomeProcessor.23"), //$NON-NLS-1$ station); continue; } /* find feature for station */ final QIntervallResult qresult = m_intervalIndex.get(station); if (qresult == null) m_log.log(false, Messages.getString( "org.kalypso.model.wspm.tuhh.schema.simulation.PolynomeProcessor.24"), //$NON-NLS-1$ station, line); else { /* create new polynome */ final IPolynomial1D poly1d = qresult.createPolynomial(); poly1d.setName(description); poly1d.setDescription(description); poly1d.setCoefficients(coeffDoubles); poly1d.setRange(rangeMin, rangeMax); poly1d.setDomainPhenomenon(domainId); poly1d.setRangePhenomenon(rangeId); } } catch (final NumberFormatException nfe) { /* A good line but bad content. Give user a hint that something might be wrong. */ m_log.log(false, Messages.getString( "org.kalypso.model.wspm.tuhh.schema.simulation.PolynomeProcessor.25"), //$NON-NLS-1$ polyFile.getName(), reader.getLineNumber(), nfe.getLocalizedMessage()); } catch (final Exception e) { // should never happen m_log.log(e, Messages.getString( "org.kalypso.model.wspm.tuhh.schema.simulation.PolynomeProcessor.25"), //$NON-NLS-1$ polyFile.getName(), reader.getLineNumber(), e.getLocalizedMessage()); } } reader.close(); } finally { IOUtils.closeQuietly(reader); } }
From source file:org.opencms.setup.CmsSetupBean.java
/** * Saves the properties to a file.<p> * /*from ww w . java 2 s .co m*/ * @param properties the properties to be saved * @param source the source file to get the keys from * @param target the target file to save the properties to * @param forceWrite the keys of the properties which should always be written, even if they don't exist in the configuration file */ private void save(CmsParameterConfiguration properties, String source, String target, Set<String> forceWrite) { try { Set<String> alreadyWritten = new HashSet<String>(); LineNumberReader lnr = new LineNumberReader(new FileReader(new File(m_configRfsPath + source))); FileWriter fw = new FileWriter(new File(m_configRfsPath + target)); while (true) { String line = lnr.readLine(); if (line == null) { break; } line = line.trim(); if ("".equals(line)) { // output empty line fw.write("\n"); } else if (line.startsWith("#")) { // output comment fw.write(line); fw.write("\n"); } else { int index = line.indexOf('='); int index1 = line.indexOf("\\="); if ((line.indexOf('=') > -1) && (index1 != (index - 1))) { String key = line.substring(0, line.indexOf('=')).trim(); if (alreadyWritten.contains(key)) { continue; } // write key fw.write((key + "=")); try { Object obj = properties.getObject(key); if (obj != null) { String valueToWrite = getPropertyValueToWrite(obj); fw.write(valueToWrite); } } catch (NullPointerException e) { // no value found - do nothing } // add trailing line feed fw.write("\n"); // remember that this properties is already written (multi values) alreadyWritten.add(key); } } } if (forceWrite != null) { for (String forced : forceWrite) { if (!alreadyWritten.contains(forced) && properties.containsKey(forced)) { fw.write("\n\n"); fw.write(forced + "="); try { Object obj = properties.getObject(forced); if (obj != null) { String valueToWrite = getPropertyValueToWrite(obj); fw.write(valueToWrite); } } catch (NullPointerException e) { // no value found - do nothing } fw.write("\n"); } } } lnr.close(); fw.close(); } catch (Exception e) { m_errors.add("Could not save properties to " + target + " \n"); m_errors.add(e.toString() + "\n"); } }
From source file:gpl.pierrick.brihaye.aramorph.InMemoryDictionaryHandler.java
/** Loads a dictionary into a <CODE>Set</CODE> where the <PRE>key</PRE> is entry and its <PRE>value</PRE> is a * <CODE>List</CODE> (each entry can have multiple values) * @param set The set/*from w w w . j a v a2 s . c o m*/ * @param name A human-readable name * @param is The stream * @throws RuntimeException If a problem occurs when reading the dictionary */ private void loadDictionary(Map set, String name, InputStream is) throws RuntimeException { //TODO : should be static HashSet lemmas = new HashSet(); int forms = 0; String lemmaID = ""; System.out.print("Loading dictionary : " + name + " "); try { LineNumberReader IN = new LineNumberReader(new InputStreamReader(is, "ISO8859_1")); String line = null; while ((line = IN.readLine()) != null) { if ((IN.getLineNumber() % 1000) == 1) System.out.print("."); // new lemma if (line.startsWith(";; ")) { lemmaID = line.substring(3); // lemmaID's must be unique if (lemmas.contains(lemmaID)) throw new RuntimeException("Lemma " + lemmaID + "in " + name + " (line " + IN.getLineNumber() + ") isn't unique"); lemmas.add(lemmaID); } // comment else if (line.startsWith(";")) { } else { String split[] = line.split("\t", -1); //-1 to avoid triming of trail values //a little error-checking won't hurt : if (split.length != 4) { throw new RuntimeException("Entry in " + name + " (line " + IN.getLineNumber() + ") doesn't have 4 fields (3 tabs)"); } String entry = split[0]; // get the entry for use as key String vocalization = split[1]; String morphology = split[2]; String glossPOS = split[3]; String gloss; String POS; Pattern p; Matcher m; // two ways to get the POS info: // (1) explicitly, by extracting it from the gloss field: p = Pattern.compile(".*" + "<pos>(.+?)</pos>" + ".*"); m = p.matcher(glossPOS); if (m.matches()) { POS = m.group(1); //extract POS from glossPOS gloss = glossPOS; //we clean up the gloss later (see below) } // (2) by deduction: use the morphology (and sometimes the voc and gloss) to deduce the appropriate POS else { // we need the gloss to guess proper names gloss = glossPOS; // null prefix or suffix if (morphology.matches("^(Pref-0|Suff-0)$")) { POS = ""; } else if (morphology.matches("^F" + ".*")) { POS = vocalization + "/FUNC_WORD"; } else if (morphology.matches("^IV" + ".*")) { POS = vocalization + "/VERB_IMPERFECT"; } else if (morphology.matches("^PV" + ".*")) { POS = vocalization + "/VERB_PERFECT"; } else if (morphology.matches("^CV" + ".*")) { POS = vocalization + "/VERB_IMPERATIVE"; } else if (morphology.matches("^N" + ".*")) { // educated guess (99% correct) if (gloss.matches("^[A-Z]" + ".*")) { POS = vocalization + "/NOUN_PROP"; } // (was NOUN_ADJ: some of these are really ADJ's and need to be tagged manually) else if (vocalization.matches(".*" + "iy~$")) { POS = vocalization + "/NOUN"; } else POS = vocalization + "/NOUN"; } else { throw new RuntimeException( "No POS can be deduced in " + name + " (line " + IN.getLineNumber() + ")"); } } // clean up the gloss: remove POS info and extra space, and convert upper-ASCII to lower (it doesn't convert well to UTF-8) gloss = gloss.replaceFirst("<pos>.+?</pos>", ""); gloss = gloss.trim(); //TODO : we definitely need a translate() method in the java packages ! gloss = gloss.replaceAll(";", "/"); //TODO : is it necessary ? gloss = gloss.replaceAll("", "A"); gloss = gloss.replaceAll("", "A"); gloss = gloss.replaceAll("", "A"); gloss = gloss.replaceAll("", "A"); gloss = gloss.replaceAll("", "A"); gloss = gloss.replaceAll("", "A"); gloss = gloss.replaceAll("", "C"); gloss = gloss.replaceAll("", "E"); gloss = gloss.replaceAll("", "E"); gloss = gloss.replaceAll("", "E"); gloss = gloss.replaceAll("", "E"); gloss = gloss.replaceAll("", "I"); gloss = gloss.replaceAll("", "I"); gloss = gloss.replaceAll("", "I"); gloss = gloss.replaceAll("", "I"); gloss = gloss.replaceAll("", "N"); gloss = gloss.replaceAll("", "O"); gloss = gloss.replaceAll("", "O"); gloss = gloss.replaceAll("", "O"); gloss = gloss.replaceAll("", "O"); gloss = gloss.replaceAll("", "O"); gloss = gloss.replaceAll("", "U"); gloss = gloss.replaceAll("", "U"); gloss = gloss.replaceAll("", "U"); gloss = gloss.replaceAll("", "U"); gloss = gloss.replaceAll("", "a"); gloss = gloss.replaceAll("", "a"); gloss = gloss.replaceAll("", "a"); gloss = gloss.replaceAll("", "a"); gloss = gloss.replaceAll("", "a"); gloss = gloss.replaceAll("", "a"); gloss = gloss.replaceAll("", "c"); gloss = gloss.replaceAll("", "e"); gloss = gloss.replaceAll("", "e"); gloss = gloss.replaceAll("", "e"); gloss = gloss.replaceAll("", "e"); gloss = gloss.replaceAll("", "i"); gloss = gloss.replaceAll("", "i"); gloss = gloss.replaceAll("", "i"); gloss = gloss.replaceAll("", "i"); gloss = gloss.replaceAll("", "n"); gloss = gloss.replaceAll("", "o"); gloss = gloss.replaceAll("", "o"); gloss = gloss.replaceAll("", "o"); gloss = gloss.replaceAll("", "o"); gloss = gloss.replaceAll("", "o"); gloss = gloss.replaceAll("", "u"); gloss = gloss.replaceAll("", "u"); gloss = gloss.replaceAll("", "u"); gloss = gloss.replaceAll("", "u"); gloss = gloss.replaceAll("", "AE"); gloss = gloss.replaceAll("", "Sh"); gloss = gloss.replaceAll("", "Zh"); gloss = gloss.replaceAll("", "ss"); gloss = gloss.replaceAll("", "ae"); gloss = gloss.replaceAll("", "sh"); gloss = gloss.replaceAll("", "zh"); // note that although we read 4 fields from the dict we now save 5 fields in the hash table // because the info in last field, glossPOS, was split into two: gloss and POS DictionaryEntry de = new DictionaryEntry(entry, lemmaID, vocalization, morphology, gloss, POS); if (set.containsKey(entry)) { ((Collection) set.get(entry)).add(de); } else set.put(entry, de); forms++; } } IN.close(); System.out.println(); if (!"".equals(lemmaID)) System.out.print(lemmas.size() + " lemmas and "); System.out.println(set.size() + " entries totalizing " + forms + " forms"); } catch (IOException e) { throw new RuntimeException("Can not open : " + name); } }
From source file:massbank.BatchSearchWorker.java
/** * T}t@C???iHTML`?j/* w ww. jav a 2s . c om*/ * @param resultFile t@C * @param htmlFile YtpHTMLt@C */ private void createSummary(File resultFile, File htmlFile) { LineNumberReader in = null; PrintWriter out = null; try { //(1) t@C? String line; int cnt = 0; ArrayList<String> nameList = new ArrayList<String>(); ArrayList<String> top1LineList = new ArrayList<String>(); TreeSet<String> top1IdList = new TreeSet<String>(); in = new LineNumberReader(new FileReader(resultFile)); while ((line = in.readLine()) != null) { line = line.trim(); if (line.equals("")) { cnt = 0; } else { cnt++; if (cnt == 1) { nameList.add(line); } else if (cnt == 2) { if (line.equals("-1")) { top1LineList.add("Invalid"); } if (line.equals("0")) { top1LineList.add("0"); } } else if (cnt == 4) { String[] vals = line.split("\t"); String id = vals[0]; top1IdList.add(id); top1LineList.add(line); } } } //? http://www.massbank.jp/ T?[o??KEGG???s HashMap<String, ArrayList> massbank2mapList = new HashMap<String, ArrayList>(); //(2)p HashMap<String, String> massbank2keggList = new HashMap<String, String>(); //(2)p HashMap<String, ArrayList> map2keggList = new HashMap<String, ArrayList>(); //(3)p ArrayList<String> mapNameList = new ArrayList<String>(); //(4)p boolean isKeggReturn = false; // if (serverUrl.indexOf("www.massbank.jp") == -1) { // isKeggReturn = false; // } if (isKeggReturn) { //(2) KEGG ID, Map IDDB String where = "where MASSBANK in("; Iterator it = top1IdList.iterator(); while (it.hasNext()) { String id = (String) it.next(); where += "'" + id + "',"; } where = where.substring(0, where.length() - 1); where += ")"; String sql = "select MASSBANK, t1.KEGG, MAP from " + "(SELECT MASSBANK,KEGG FROM OTHER_DB_IDS " + where + ") t1, PATHWAY_CPDS t2" + " where t1.KEGG=t2.KEGG order by MAP,MASSBANK"; ArrayList<String> mapList = null; try { Class.forName("com.mysql.jdbc.Driver"); String connectUrl = "jdbc:mysql://localhost/MassBank_General"; Connection con = DriverManager.getConnection(connectUrl, "bird", "bird2006"); Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery(sql); String prevId = ""; while (rs.next()) { String id = rs.getString(1); String kegg = rs.getString(2); String map = rs.getString(3); if (!id.equals(prevId)) { if (!prevId.equals("")) { massbank2mapList.put(prevId, mapList); } mapList = new ArrayList<String>(); massbank2keggList.put(id, kegg); } mapList.add(map); prevId = id; } massbank2mapList.put(prevId, mapList); rs.close(); stmt.close(); con.close(); } catch (Exception e) { e.printStackTrace(); } if (mapList != null) { //(3) Pathway Map?FtXg?? it = massbank2mapList.keySet().iterator(); while (it.hasNext()) { String id = (String) it.next(); String kegg = (String) massbank2keggList.get(id); ArrayList<String> list1 = massbank2mapList.get(id); for (int i = 0; i < list1.size(); i++) { String map = list1.get(i); ArrayList<String> list2 = null; if (map2keggList.containsKey(map)) { list2 = map2keggList.get(map); list2.add(kegg); } else { list2 = new ArrayList<String>(); list2.add(kegg); map2keggList.put(map, list2); } } } //(4) SOAPPathway Map?Ft?\bh?s it = map2keggList.keySet().iterator(); List<Callable<HashMap<String, String>>> tasks = new ArrayList(); while (it.hasNext()) { String map = (String) it.next(); mapNameList.add(map); ArrayList<String> list = map2keggList.get(map); String[] cpds = list.toArray(new String[] {}); Callable<HashMap<String, String>> task = new ColorPathway(map, cpds); tasks.add(task); } Collections.sort(mapNameList); // Xbhv?[10 ExecutorService exsv = Executors.newFixedThreadPool(10); List<Future<HashMap<String, String>>> results = exsv.invokeAll(tasks); // Pathway mapi[?? String saveRootPath = MassBankEnv.get(MassBankEnv.KEY_TOMCAT_APPTEMP_PATH) + "pathway"; File rootDir = new File(saveRootPath); if (!rootDir.exists()) { rootDir.mkdir(); } // String savePath = saveRootPath + File.separator + this.jobId; // File newDir = new File(savePath); // if ( !newDir.exists() ) { // newDir.mkdir(); // } //(6) Pathway mapURL for (Future<HashMap<String, String>> future : results) { HashMap<String, String> res = future.get(); it = res.keySet().iterator(); String map = (String) it.next(); String mapUrl = res.get(map); String filePath = saveRootPath + File.separator + this.jobId + "_" + map + ".png"; FileUtil.downloadFile(mapUrl, filePath); } } } //(7) ?o out = new PrintWriter(new BufferedWriter(new FileWriter(htmlFile))); // wb_?[?o String reqIonStr = "Both"; try { if (Integer.parseInt(this.ion) > 0) { reqIonStr = "Positive"; } else if (Integer.parseInt(this.ion) < 0) { reqIonStr = "Negative"; } } catch (NumberFormatException nfe) { nfe.printStackTrace(); } String title = "Summary of Batch Service Results"; out.println("<html>"); out.println("<head>"); out.println("<title>" + title + "</title>"); out.println("</head>"); out.println("<body>"); out.println("<h1>" + title + "</h1>"); out.println("<hr>"); out.println("<h3>Request Date : " + this.time + "</h3>"); out.println("Instrument Type : " + this.inst + "<br>"); out.println("MS Type : " + this.ms + "<br>"); out.println("Ion Mode : " + reqIonStr + "<br>"); out.println("<br>"); out.println("<hr>"); out.println("<table border=\"1\" cellspacing=\"0\" cellpadding=\"2\">"); String cols = String.valueOf(mapNameList.size()); out.println("<tr>"); out.println("<th bgcolor=\"LavenderBlush\" rowspan=\"1\">No.</th>"); out.println("<th bgcolor=\"LavenderBlush\" rowspan=\"1\">Query Name</th>"); out.println("<th bgcolor=\"LightCyan\" rowspan=\"1\">Score</th>"); out.println("<th bgcolor=\"LightCyan\" rowspan=\"1\">Hit</th>"); out.println("<th bgcolor=\"LightCyan\" rowspan=\"1\">MassBank ID</th>"); out.println("<th bgcolor=\"LightCyan\" rowspan=\"1\">Record Title</th>"); out.println("<th bgcolor=\"LightCyan\" rowspan=\"1\">Formula</th>"); out.println("<th bgcolor=\"LightCyan\" rowspan=\"1\">Exact Mass</th>"); if (isKeggReturn) { out.println("<th bgcolor=\"LightYellow\" rowspan=\"2\">KEGG ID</th>"); out.println( "<th bgcolor=\"LightYellow\" colspan=\"" + cols + "\">Colored Pathway Maps</th>"); } out.println("</tr>"); out.print("<tr bgcolor=\"moccasin\">"); for (int i = 0; i < mapNameList.size(); i++) { out.print("<th>MAP" + String.valueOf(i + 1) + "</th>"); } out.println("</tr>"); for (int i = 0; i < nameList.size(); i++) { out.println("<tr>"); String no = String.format("%5d", i + 1); no = no.replace(" ", " "); out.println("<td>" + no + "</td>"); // Query Name String queryName = nameList.get(i); out.println("<td nowrap>" + queryName + "</td>"); line = top1LineList.get(i); if (line.equals("0")) { if (isKeggReturn) { cols = String.valueOf(mapNameList.size() + 5); } else { cols = String.valueOf(6); } out.println("<td colspan=\"" + cols + "\">No Hit Record</td>"); } else if (line.equals("Invalid")) { if (isKeggReturn) { cols = String.valueOf(mapNameList.size() + 5); } else { cols = String.valueOf(4); } out.println("<td colspan=\"" + cols + "\">Invalid Query</td>"); } else { String[] data = formatLine(line); String id = data[0]; String recTitle = data[1]; String formula = data[2]; String emass = data[3]; String score = data[4]; String hit = data[5]; boolean isHiScore = false; if (Integer.parseInt(hit) >= 3 && Double.parseDouble(score) >= 0.8) { isHiScore = true; } // Score if (isHiScore) { out.println("<td><b>" + score + "</b></td>"); } else { out.println("<td>" + score + "</td>"); } // hit peak if (isHiScore) { out.println("<td align=\"right\"><b>" + hit + "</b></td>"); } else { out.println("<td align=\"right\">" + hit + "</td>"); } // MassBank ID & Link out.println("<td><a href=\"" + serverUrl + "jsp/FwdRecord.jsp?id=" + id + "\" target=\"_blank\">" + id + "</td>"); // Record Title out.println("<td>" + recTitle + "</td>"); // Formula out.println("<td nowrap>" + formula + "</td>"); // Exact Mass out.println("<td nowrap>" + emass + "</td>"); // KEGG ID & Link if (isKeggReturn) { String keggLink = " -"; if (massbank2keggList.containsKey(id)) { String keggUrl = "http://www.genome.jp/dbget-bin/www_bget?"; String kegg = massbank2keggList.get(id); switch (kegg.charAt(0)) { case 'C': keggUrl += "cpd:" + kegg; break; case 'D': keggUrl += "dr:" + kegg; break; case 'G': keggUrl += "gl:" + kegg; break; } keggLink = "<a href=\"" + keggUrl + "\" target=\"_blank\">" + kegg + "</a>"; } out.println("<td>" + keggLink + "</td>"); // Pathway Map Link if (massbank2mapList.containsKey(id)) { ArrayList<String> list = massbank2mapList.get(id); for (int l1 = mapNameList.size() - 1; l1 >= 0; l1--) { boolean isFound = false; String map = ""; for (int l2 = list.size() - 1; l2 >= 0; l2--) { map = list.get(l2); if (map.equals(mapNameList.get(l1))) { isFound = true; break; } } if (isFound) { ArrayList<String> list2 = map2keggList.get(map); String mapUrl = serverUrl + "temp/pathway/" + this.jobId + "_" + map + ".png"; out.println("<td nowrap><a href=\"" + mapUrl + "\" target=\"_blank\">map:" + map + "(" + list2.size() + ")</a></td>"); } else { out.println("<td> -</td>"); } } } else { for (int l1 = mapNameList.size() - 1; l1 >= 0; l1--) { out.println("<td> -</td>"); } } } } out.println("</tr>"); } out.println("</table>"); } catch (Exception e) { e.printStackTrace(); } finally { try { if (in != null) { in.close(); } } catch (IOException e) { } if (out != null) { out.flush(); out.close(); } } }