List of usage examples for java.lang Character toString
public static String toString(int codePoint)
From source file:com.github.mobile.ui.repo.RepositoryListFragment.java
private void updateHeaders(final List<Repository> repos) { HeaderFooterListAdapter<?> rootAdapter = getListAdapter(); if (rootAdapter == null) return;// w w w . j a v a 2 s . co m DefaultRepositoryListAdapter adapter = (DefaultRepositoryListAdapter) rootAdapter.getWrappedAdapter(); adapter.clearHeaders(); if (repos.isEmpty()) return; // Add recent header if at least one recent repository Repository first = repos.get(0); if (recentRepos.contains(first)) adapter.registerHeader(first, getString(string.recently_viewed)); // Advance past all recent repositories int index; Repository current = null; for (index = 0; index < repos.size(); index++) { Repository repository = repos.get(index); if (recentRepos.contains(repository.getId())) current = repository; else break; } if (index >= repos.size()) return; if (current != null) adapter.registerNoSeparator(current); // Register header for first character current = repos.get(index); char start = Character.toLowerCase(current.getName().charAt(0)); adapter.registerHeader(current, Character.toString(start).toUpperCase(US)); char previousHeader = start; for (index = index + 1; index < repos.size(); index++) { current = repos.get(index); char repoStart = Character.toLowerCase(current.getName().charAt(0)); if (repoStart <= start) continue; // Don't include separator for the last element of the previous // character if (previousHeader != repoStart) adapter.registerNoSeparator(repos.get(index - 1)); adapter.registerHeader(current, Character.toString(repoStart).toUpperCase(US)); previousHeader = repoStart; start = repoStart++; } // Don't include separator for last element adapter.registerNoSeparator(repos.get(repos.size() - 1)); }
From source file:io.amient.kafka.metrics.Dashboard.java
public ObjectNode newTarget(ObjectNode panel, String aliasPattern, String rawQuery) { ArrayNode targets = ((ArrayNode) panel.get("targets")); ObjectNode target = targets.addObject(); target.put("refId", Character.toString((char) (64 + targets.size()))); target.put("query", rawQuery); target.put("alias", aliasPattern); target.put("rawQuery", true); return target; }
From source file:org.apache.fop.fonts.truetype.TTFFontLoader.java
private void copyWidthsSingleByte(TTFFile ttf) { int[] wx = ttf.getWidths(); for (int i = singleFont.getFirstChar(); i <= singleFont.getLastChar(); i++) { singleFont.setWidth(i, ttf.getCharWidth(i)); }/* w w w .j a v a 2 s . co m*/ for (CMapSegment segment : ttf.getCMaps()) { if (segment.getUnicodeStart() < 0xFFFE) { for (char u = (char) segment.getUnicodeStart(); u <= segment.getUnicodeEnd(); u++) { int codePoint = singleFont.getEncoding().mapChar(u); if (codePoint <= 0) { int glyphIndex = segment.getGlyphStartIndex() + u - segment.getUnicodeStart(); String glyphName = ttf.getGlyphName(glyphIndex); if (glyphName.length() == 0 && ttf.getPostScriptVersion() != PostScriptVersion.V2) { glyphName = "u" + HexEncoder.encode(u); } if (glyphName.length() > 0) { String unicode = Character.toString(u); NamedCharacter nc = new NamedCharacter(glyphName, unicode); singleFont.addUnencodedCharacter(nc, wx[glyphIndex]); } } } } } }
From source file:com.android.pwdhashandroid.pwdhash.PasswordHasher.java
protected String ApplyConstraints(String hash, int size, boolean nonalphanumeric) { Log.d(tag, "ApplyConstraints:"); int startingSize = size - 4; // Leave room for extra characters <-- this implicitly means that password has to be at least 2 chars long // String result = hash.substring(0, startingSize); // If the password is longer then the actual hash, return the hash, // otherwise only the first "startingSize" bits of the hash String result = startingSize > hash.length() ? hash : hash.substring(0, startingSize); // to avoid Exception for startingsize longer then hash, we return the length of the hash as sthe startinpoint if longer extras = CreateQueue(/* w ww .j a v a2 s. co m*/ hash.substring(startingSize > hash.length() ? hash.length() : startingSize).toCharArray()); Pattern matchSymbol = Pattern.compile("\\W"); //Regex matchSymbol = new Regex("\\W"); Log.d(tag, "startingSize:\t" + startingSize); Log.d(tag, "result (start):\t" + result); Log.d(tag, "extras (start):\t" + new String(extras.toString())); Log.d(tag, "nonalphanumeric:\t" + nonalphanumeric); // Add the extra characters Log.d(tag, "A-1Z:\t\t\t"); result += (Pattern.compile("[A-Z]").matcher(result).find() ? nextExtraChar() : nextBetween('A', 26)); Log.d(tag, result); Log.d(tag, "a-z:\t\t\t"); result += (Pattern.compile("[a-z]").matcher(result).find() ? nextExtraChar() : nextBetween('a', 26)); Log.d(tag, result); Log.d(tag, "0-9:\t\t\t"); result += (Pattern.compile("[0-9]").matcher(result).find() ? nextExtraChar() : nextBetween('0', 10)); Log.d(tag, result); Log.d(tag, "\\W:\t\t\t"); result += (Pattern.compile("\\W").matcher(result).find() && nonalphanumeric ? nextExtraChar() : '+'); Log.d(tag, result); while (matchSymbol.matcher(result).find() && !nonalphanumeric) { //Log.d(tag, "Replace '" + matchSymbol.Match(result) + "':\t\t"); //Log.d(tag, "Replace '" + matchSymbol.matcher(result).group() + "':\t\t"); result = result.replaceFirst("\\W", Character.toString(nextBetween('A', 26))); // Log.d(tag, result); } // Rotate the result to make it harder to guess the inserted locations Log.d(tag, "Rotate "); char[] rotateArr = result.toCharArray(); rotateArr = rotate(rotateArr, nextExtra()); result = new String(rotateArr); Log.d(tag, result); return result; }
From source file:org.codice.ddf.catalog.ui.query.suggestion.UtmUpsCoordinateProcessor.java
/** * Private utility method for updating a UTM string in-place with a new latitude band. The * provided input <b>must</b> match {@link #PATTERN_UTM_COORDINATE}. * * @param input UTM string input; must match {@link #PATTERN_UTM_COORDINATE}. * @param newLatBand character to use as the new latitude band for the new UTM string. * @return a new UTM string with the provided {@code newLatBand}, regardless if the original UTM * {@code input} had a lat band or not. *//* w w w . ja v a2s. c o m*/ private static String setLatBand(final String input, final Character newLatBand) { notNull(newLatBand, "newLatBand cannot be null"); final int indexOfFirstWhiteSpace = input.indexOf(SPACE_CHAR); final int indexOfFirstWhiteSpaceOrLatBand = Character.isLetter(input.charAt(indexOfFirstWhiteSpace - 1)) ? indexOfFirstWhiteSpace - 1 // Use lat band index instead : indexOfFirstWhiteSpace; return input.substring(0, indexOfFirstWhiteSpaceOrLatBand).concat(Character.toString(newLatBand)) .concat(input.substring(indexOfFirstWhiteSpace)); }
From source file:org.easyrec.service.core.impl.ItemAssocServiceImpl.java
@Override public void importItemAssocsFromCSV(String fileName, ItemAssocVO<Integer, Integer> defaults) { long start = System.currentTimeMillis(); if (logger.isInfoEnabled()) { logger.info("==================== starting importing 'itemAssoc' ===================="); logger.info("importing 'itemAssoc' from CSV '" + fileName + "'"); logger.info("using interface defaults '" + defaults + "'"); logger.info("========================================================================"); }/*ww w. j a va2 s.c om*/ if (fileName == null) { throw new IllegalArgumentException("missing 'fileName'"); } BufferedReader br = null; int lineCounter = 3; int savedCounter = 0; int removedCounter = 0; int skippedCounter = 0; int errorCounter = 0; int currentSavedCounter = 0; int currentRemovedCounter = 0; String command = null; try { try { br = new BufferedReader(new InputStreamReader(new FileInputStream(fileName))); } catch (FileNotFoundException e) { throw new IllegalArgumentException("file '" + fileName + "' not found", e); } String line = null; String elementsOfLine[] = null; // read command try { line = br.readLine(); } catch (IOException e) { throw new IllegalStateException("unexpected IOException", e); } try { // in case of we have an 'old style' file format, containing no 'type' definition, then the first line would contain the command command = AutoImportUtils.retrieveCommandFromLine(line); } catch (IllegalArgumentException e) { // this should be the normal case, the file contains a 'type' definition, so we need to skip this line // read/skip type try { line = br.readLine(); } catch (IOException ioe) { throw new IllegalStateException("unexpected IOException", ioe); } command = AutoImportUtils.retrieveCommandFromLine(line); } boolean commandEqualsInsertCommand = AutoImportUtils.COMMAND_INSERT.equalsIgnoreCase(command); // read header and generate headerDefaults try { line = br.readLine(); } catch (IOException e) { throw new IllegalStateException("unexpected IOException", e); } ItemAssocVO<Integer, Integer> headerDefaults = generateDefaultsFromHeader(line, defaults); if (logger.isInfoEnabled()) { logger.info("extracted header defaults from csv file, using: " + headerDefaults); } // fetch next line try { while ((line = br.readLine()) != null) { lineCounter++; // skip empty lines if ("".equals(line)) { AutoImportUtils.logSkippedLine(logger, lineCounter, line, "line is empty"); skippedCounter++; continue; } // skip comment lines if (line.startsWith(Character.toString(AutoImportUtils.CSV_COMMENT_CHAR))) { AutoImportUtils.logSkippedLine(logger, lineCounter, line, "line is a comment"); skippedCounter++; continue; } // skip lines, with wrong number of columns elementsOfLine = line.split(AutoImportUtils.CSV_SEPARATOR, ItemAssocVO.CSV_NUMBER_OF_COLUMNS); if (elementsOfLine.length != ItemAssocVO.CSV_NUMBER_OF_COLUMNS) { StringBuilder s = new StringBuilder("', number of columns should be '"); s.append(ItemAssocVO.CSV_NUMBER_OF_COLUMNS); s.append("', but was '"); s.append(elementsOfLine.length); s.append("'"); AutoImportUtils.logSkippedLine(logger, lineCounter, line, s.toString()); skippedCounter++; continue; } ItemAssocVO<Integer, Integer> iAssoc = null; try { iAssoc = (ItemAssocVO<Integer, Integer>) headerDefaults.clone(); } catch (CloneNotSupportedException e) { throw new IllegalStateException( "value object 'ItemAssocVO' does not support .clone() anymore, check that!!"); } // parse 'tenantId' if (!"".equals(elementsOfLine[0])) { try { iAssoc.setTenant(Integer.parseInt(elementsOfLine[0])); } catch (NumberFormatException nfe) { if (commandEqualsInsertCommand) { AutoImportUtils.logSkippedLine(logger, lineCounter, line, "value for field 'tenantId' is no valid 'Integer'"); skippedCounter++; continue; } } } else { // 'tenantId' NOT NULL if (commandEqualsInsertCommand && iAssoc.getTenant() == null) { AutoImportUtils.logSkippedLine(logger, lineCounter, line, "no value for field 'tenantId' is set"); skippedCounter++; continue; } } // parse 'itemFromId' if (!"".equals(elementsOfLine[1])) { try { iAssoc.getItemFrom().setItem(Integer.parseInt(elementsOfLine[1])); } catch (NumberFormatException nfe) { if (commandEqualsInsertCommand) { AutoImportUtils.logSkippedLine(logger, lineCounter, line, "value for field 'itemFromId' is no valid 'Integer'"); skippedCounter++; continue; } } } else { // 'itemFromId' NOT NULL if (commandEqualsInsertCommand && iAssoc.getItemFrom().getItem() == null) { AutoImportUtils.logSkippedLine(logger, lineCounter, line, "no value for field 'itemFromId' is set"); skippedCounter++; continue; } } // parse 'itemFromTypeId' if (!"".equals(elementsOfLine[2])) { try { iAssoc.getItemFrom().setType(Integer.parseInt(elementsOfLine[2])); } catch (NumberFormatException nfe) { if (commandEqualsInsertCommand) { AutoImportUtils.logSkippedLine(logger, lineCounter, line, "value for field 'itemFromTypeId' is no valid 'Integer'"); skippedCounter++; continue; } } } else { // 'itemFromTypeId' NOT NULL if (commandEqualsInsertCommand && iAssoc.getItemFrom().getType() == null) { AutoImportUtils.logSkippedLine(logger, lineCounter, line, "no value for field 'itemFromTypeId' is set"); skippedCounter++; continue; } } // parse 'assocTypeId' if (!"".equals(elementsOfLine[3])) { try { iAssoc.setAssocType(Integer.parseInt(elementsOfLine[3])); } catch (NumberFormatException nfe) { if (commandEqualsInsertCommand) { AutoImportUtils.logSkippedLine(logger, lineCounter, line, "value for field 'assocTypeId' is no valid 'Integer'"); skippedCounter++; continue; } } } else { // 'assocTypeId' NOT NULL if (commandEqualsInsertCommand && iAssoc.getAssocType() == null) { AutoImportUtils.logSkippedLine(logger, lineCounter, line, "no value for field 'assocTypeId' is set"); skippedCounter++; continue; } } // parse 'assocValue' if (!"".equals(elementsOfLine[4])) { try { iAssoc.setAssocValue(Double.parseDouble(elementsOfLine[4])); } catch (NumberFormatException nfe) { if (commandEqualsInsertCommand) { AutoImportUtils.logSkippedLine(logger, lineCounter, line, "value for field 'assocValue' is no valid 'Double'"); skippedCounter++; continue; } } } else { // 'assocValue' NOT NULL if (commandEqualsInsertCommand && iAssoc.getAssocValue() == null) { AutoImportUtils.logSkippedLine(logger, lineCounter, line, "no value for field 'assocValue' is set"); skippedCounter++; continue; } } // parse 'itemToId' if (!"".equals(elementsOfLine[5])) { try { iAssoc.getItemTo().setItem(Integer.parseInt(elementsOfLine[5])); } catch (NumberFormatException nfe) { if (commandEqualsInsertCommand) { AutoImportUtils.logSkippedLine(logger, lineCounter, line, "value for field 'itemToId' is no valid 'Integer'"); skippedCounter++; continue; } } } else { if (commandEqualsInsertCommand && iAssoc.getItemTo().getItem() == null) { AutoImportUtils.logSkippedLine(logger, lineCounter, line, "no value for field 'itemToId' is set"); skippedCounter++; continue; } } // parse 'itemToTypeId' if (!"".equals(elementsOfLine[6])) { try { iAssoc.getItemTo().setType(Integer.parseInt(elementsOfLine[6])); } catch (NumberFormatException nfe) { if (commandEqualsInsertCommand) { AutoImportUtils.logSkippedLine(logger, lineCounter, line, "value for field 'itemToTypeId' is no valid 'Integer'"); skippedCounter++; continue; } } } else { // 'itemToTypeId' NOT NULL if (commandEqualsInsertCommand && iAssoc.getItemTo().getType() == null) { AutoImportUtils.logSkippedLine(logger, lineCounter, line, "no value for field 'itemToTypeId' is set"); skippedCounter++; continue; } } // parse 'sourceTypeId' if (!"".equals(elementsOfLine[7])) { try { iAssoc.setSourceType(Integer.parseInt(elementsOfLine[7])); } catch (NumberFormatException nfe) { if (commandEqualsInsertCommand) { AutoImportUtils.logSkippedLine(logger, lineCounter, line, "value for field 'sourceTypeId' is no valid 'Integer'"); skippedCounter++; continue; } } } else { // 'sourceTypeId' NOT NULL if (commandEqualsInsertCommand && iAssoc.getSourceType() == null) { AutoImportUtils.logSkippedLine(logger, lineCounter, line, "no value for field 'sourceTypeId' is set"); skippedCounter++; continue; } } // parse 'sourceInfo' if (!"".equals(elementsOfLine[8])) { iAssoc.setSourceInfo(elementsOfLine[8]); } // parse 'viewTypeId' if (!"".equals(elementsOfLine[9])) { try { iAssoc.setViewType(Integer.parseInt(elementsOfLine[9])); } catch (NumberFormatException nfe) { if (commandEqualsInsertCommand) { AutoImportUtils.logSkippedLine(logger, lineCounter, line, "value for field 'viewTypeId' is no valid 'Integer'"); skippedCounter++; continue; } } } else { // 'viewTypeId' NOT NULL if (commandEqualsInsertCommand && iAssoc.getViewType() == null) { AutoImportUtils.logSkippedLine(logger, lineCounter, line, "no value for field 'viewTypeId' is set"); skippedCounter++; continue; } } // parse 'active' if (!"".equals(elementsOfLine[10])) { iAssoc.setActive(Boolean.parseBoolean(elementsOfLine[10])); } else { // 'active' NOT NULL if (commandEqualsInsertCommand && iAssoc.isActive() == null) { AutoImportUtils.logSkippedLine(logger, lineCounter, line, "no value for field 'active' is set"); skippedCounter++; continue; } } try { if (AutoImportUtils.COMMAND_INSERT.equals(command)) { int savedThisIteration = 0; if (importOverwriteDuplicates) { savedThisIteration = insertOrUpdateItemAssoc(iAssoc); } else { savedThisIteration = insertItemAssoc(iAssoc); } currentSavedCounter += savedThisIteration; savedCounter += savedThisIteration; int currentSavedMultiplier = currentSavedCounter / reportBlockSize; if (currentSavedMultiplier > 0) { if (logger.isInfoEnabled()) { logger.info("number of saved 'itemAssoc' entries: " + (currentSavedMultiplier * reportBlockSize)); } currentSavedCounter %= reportBlockSize; } } else { // COMMAND_REMOVE int removedThisIteration = itemAssocDAO.removeItemAssocsQBE(iAssoc); currentRemovedCounter += removedThisIteration; removedCounter += removedThisIteration; int currentRemovedMultiplier = currentRemovedCounter / reportBlockSize; if (currentRemovedMultiplier > 0) { if (logger.isInfoEnabled()) { logger.info("number of removed 'itemAssoc' entries: " + (currentRemovedMultiplier * reportBlockSize)); } currentRemovedCounter %= reportBlockSize; } } } catch (Exception e) { errorCounter++; logger.error("error occured during insertItemAssoc() '" + iAssoc + "'", e); } } // end of while } catch (IOException e) { throw new IllegalStateException("unexpected IOException", e); } } finally { // close stream if (br != null) { try { br.close(); } catch (IOException e) { throw new IllegalStateException("unexpected IOException", e); } } } if (AutoImportUtils.COMMAND_INSERT.equals(command)) { if (logger.isInfoEnabled()) { if (currentSavedCounter > 0) { logger.info("number of saved 'itemAssoc' entries: " + currentSavedCounter); } } } else { if (logger.isInfoEnabled()) { if (currentRemovedCounter > 0) { logger.info("number of removed 'itemAssoc' entries: " + currentRemovedCounter); } } } if (logger.isInfoEnabled()) { logger.info( "==================== finished importing from file '" + fileName + "' ===================="); logger.info("total number of saved 'itemAssoc' entries: " + savedCounter); logger.info("total number of removed 'itemAssoc' entries: " + removedCounter); logger.info("total number of skipped 'itemAssoc' entries: " + skippedCounter); logger.info("total number of errors occured while import: " + errorCounter); logger.info("used defaults: '" + defaults + "'"); logger.info("time taken: " + (System.currentTimeMillis() - start) + " ms"); logger.info("========================================================================"); } }
From source file:com.github.pockethub.ui.repo.RepositoryListFragment.java
private void updateHeaders(final List<Repo> repos) { HeaderFooterListAdapter<?> rootAdapter = getListAdapter(); if (rootAdapter == null) return;// w w w. j av a 2 s .c o m DefaultRepositoryListAdapter adapter = (DefaultRepositoryListAdapter) rootAdapter.getWrappedAdapter(); adapter.clearHeaders(); if (repos.isEmpty()) return; // Add recent header if at least one recent repository Repo first = repos.get(0); if (recentRepos.contains(first)) adapter.registerHeader(first, getString(R.string.recently_viewed)); // Advance past all recent repositories int index; Repo current = null; for (index = 0; index < repos.size(); index++) { Repo repository = repos.get(index); if (recentRepos.contains(repository.id)) current = repository; else break; } if (index >= repos.size()) return; if (current != null) adapter.registerNoSeparator(current); // Register header for first character current = repos.get(index); char start = Character.toLowerCase(current.name.charAt(0)); adapter.registerHeader(current, Character.toString(start).toUpperCase(US)); char previousHeader = start; for (index = index + 1; index < repos.size(); index++) { current = repos.get(index); char repoStart = Character.toLowerCase(current.name.charAt(0)); if (repoStart <= start) continue; // Don't include separator for the last element of the previous // character if (previousHeader != repoStart) adapter.registerNoSeparator(repos.get(index - 1)); adapter.registerHeader(current, Character.toString(repoStart).toUpperCase(US)); previousHeader = repoStart; start = repoStart++; } // Don't include separator for last element adapter.registerNoSeparator(repos.get(repos.size() - 1)); }
From source file:es.uvigo.ei.sing.adops.datatypes.ProjectExperiment.java
private String checkFastaFile() throws IllegalArgumentException { final Set<Character> aminos = new HashSet<Character>( Arrays.asList('a', 'c', 't', 'g', 'A', 'C', 'T', 'G', '-')); BufferedReader br = null;//from w w w . j a va 2 s . c o m try { final StringBuilder sb = new StringBuilder(); final LinkedHashMap<String, StringBuilder> replacements = new LinkedHashMap<String, StringBuilder>(); br = new BufferedReader(new FileReader(this.fastaFile)); String line = null; while ((line = br.readLine()) != null && !line.startsWith(">")) { sb.append(line).append('\n'); } String seqId = null; String seq = null; while (line != null) { seqId = line; seq = ""; while ((line = br.readLine()) != null && !line.startsWith(">")) { seq += line; } // Non ACTG characters replacement char[] charSequence = seq.toCharArray(); String data = ""; for (int i = 0; i < charSequence.length; i++) { if (aminos.contains(charSequence[i])) { data += charSequence[i]; } else { if (replacements.containsKey(seqId)) { replacements.get(seqId).append(String.format(", [%d,%c]", i + 1, charSequence[i])); } else { replacements.put(seqId, new StringBuilder(String.format("[%d,%c]", i + 1, charSequence[i]))); } data += '-'; } } // Incomplete codons replacement charSequence = data.toCharArray(); data = ""; String codon = ""; for (int i = 0; i < charSequence.length; i++) { codon += Character.toString(charSequence[i]); if ((i + 1) % 3 == 0) { if (codon.contains("-")) { data += "---"; if (replacements.containsKey(seqId)) { replacements.get(seqId).append(String.format(", [%s,---]", codon)); } else { replacements.put(seqId, new StringBuilder(String.format("[%s,---]", codon))); } } else { data += codon; } codon = ""; } } sb.append(seqId).append('\n'); sb.append(data).append('\n'); } FileUtils.write(this.fastaFile, sb.toString()); if (replacements.isEmpty()) { return ""; } else { final StringBuilder summary = new StringBuilder("Replacements done on input file\n"); for (Map.Entry<String, StringBuilder> replacement : replacements.entrySet()) { summary.append(replacement.getKey()).append('\n'); summary.append(replacement.getValue()).append('\n'); } summary.append("\n-----\n"); return summary.toString(); } } catch (Exception e) { throw new IllegalArgumentException("Input file is not a valida Fasta file"); } finally { if (br != null) { try { br.close(); } catch (IOException ioe) { } } } }
From source file:org.apache.xmlgraphics.image.loader.impl.AbstractImageSessionContext.java
/** * Convert from a <code>URL</code> to a <code>File</code>. * <p>/*from w w w. j a v a 2 s . c o m*/ * This method will decode the URL. * Syntax such as <code>file:///my%20docs/file.txt</code> will be * correctly decoded to <code>/my docs/file.txt</code>. * <p> * Note: this method has been copied over from Apache Commons IO and enhanced to support * UNC paths. * * @param url the file URL to convert, <code>null</code> returns <code>null</code> * @return the equivalent <code>File</code> object, or <code>null</code> * if the URL's protocol is not <code>file</code> * @throws IllegalArgumentException if the file is incorrectly encoded */ public static File toFile(URL url) { if (url == null || !url.getProtocol().equals("file")) { return null; } else { try { String filename = ""; if (url.getHost() != null && url.getHost().length() > 0) { filename += Character.toString(File.separatorChar) + Character.toString(File.separatorChar) + url.getHost(); } filename += url.getFile().replace('/', File.separatorChar); filename = java.net.URLDecoder.decode(filename, "UTF-8"); final File f = new File(filename); if (!f.isFile()) { return null; } return f; } catch (java.io.UnsupportedEncodingException uee) { assert false; return null; } } }
From source file:de.dfki.km.perspecting.obie.connection.RDFTripleParser.java
/** * // www .j a va 2 s.c o m * @param input * @param mimetype * @param sessionPath * @param absoluteBaseURI * @return * @throws Exception */ public TripleStats parseTriples(final InputStream[] input, final MediaType rdf_mimetype, final File sessionPath, final String absoluteBaseURI, final MediaType file_mimetype) throws Exception { final TripleStats stats = new TripleStats(); // int count = 0; new File(sessionPath.getAbsolutePath() + "/dump/").mkdirs(); stats.datatypeProps = new File(sessionPath.getAbsolutePath() + "/dump/datatypeProperties.lst"); stats.objectProps = new File(sessionPath.getAbsolutePath() + "/dump/objectProperties.lst"); stats.datatypeProps.deleteOnExit(); stats.objectProps.deleteOnExit(); stats.datatypeProps.setReadable(true, false); stats.objectProps.setReadable(true, false); if (stats.datatypeProps.exists() && stats.objectProps.exists()) { return stats; } // stats.literalLanguageList = new File(sessionPath + // "/dump/literals_language.lst"); // final BufferedWriter literalLanguageWriter = new BufferedWriter(new // FileWriter( // stats.literalLanguageList, true)); final BufferedWriter datatypePropertiesWriter = new BufferedWriter( new FileWriter(stats.datatypeProps, false)); final BufferedWriter objectPropertiesWriter = new BufferedWriter(new FileWriter(stats.objectProps, false)); final ArrayList<Callable<Boolean>> threads = new ArrayList<Callable<Boolean>>(); int sourceCount = 0; for (final InputStream stream : input) { final String source = (++sourceCount) + ""; log.info("Parsing: " + source + " from ( " + input.length + " )"); final RDFParser parser = getParser(rdf_mimetype); parser.setRDFHandler(new RDFHandler() { long tripleCount = 0; @Override public void startRDF() throws RDFHandlerException { log.info("Start parsing RDF triples"); } @Override public void handleStatement(Statement stmt) throws RDFHandlerException { try { tripleCount++; if (tripleCount % 10000 == 0) { log.info(source + ": Parsed " + tripleCount + " RDF triples"); } // get triple components String p = stmt.getPredicate().toString(); String s = stmt.getSubject().toString(); String o = stmt.getObject().toString(); // test URIs if (s.length() > URISIZE) { log.warning("Skipping too long subject " + s); return; } if (p.length() > URISIZE) { log.warning("Skipping too long predicate " + p); return; } if (stmt.getSubject() instanceof URI) s = fixJavaURI(s); p = fixJavaURI(p); // check object properties URIs if (stmt.getObject() instanceof URI) { if (o.length() > URISIZE) { return; } else { o = fixJavaURI(o); appendObjectTriple(s, p, o); } } else if (stmt.getObject() instanceof Literal) { o = stmt.getObject().stringValue().replaceAll("[\n\t\\\\\"]", "").trim(); if (o.length() < 2 || o.length() > 100) { return; } appendLiteralTriple(s, p, o, ((Literal) stmt.getObject()).getLanguage()); } else { log.warning("Skipping bad triple " + stmt); } } catch (Exception e) { log.log(Level.SEVERE, "Error in parsing: " + source, e); } } /** * Encodes characters invalid (e.g. "|") in the uri and returns * the encoded string. * * @param uri * uri to enctode * @return encoded uri */ private String fixJavaURI(String uri) { try { new java.net.URI(uri); } catch (URISyntaxException e) { String badChar = Character.toString(uri.charAt(e.getIndex())); try { log.fine("Fixing bad uri: " + uri); return fixJavaURI(uri.replace(badChar, URLEncoder.encode(badChar, "utf-8"))); } catch (UnsupportedEncodingException e1) { throw new RuntimeException(e1); } } return uri; } private void appendLiteralTriple(String subject, String predicate, String literal, String language) throws IOException { if (language == null) { language = ALL; } synchronized (SEMAPHOR) { stats.datatypePropsSize++; datatypePropertiesWriter.write(RDFTripleParser.encloseCharacterString(subject)); datatypePropertiesWriter.append(','); datatypePropertiesWriter.write(RDFTripleParser.encloseCharacterString(predicate)); datatypePropertiesWriter.append(','); datatypePropertiesWriter.write(RDFTripleParser.encloseCharacterString(literal)); datatypePropertiesWriter.append(','); datatypePropertiesWriter .write(Integer.toString(hashing.hash(literal.toLowerCase(Locale.US)))); datatypePropertiesWriter.newLine(); } } private void appendObjectTriple(String subject, String predicate, String object) throws IOException { synchronized (SEMAPHOR) { stats.objectPropsSize++; objectPropertiesWriter.write(RDFTripleParser.encloseCharacterString(subject)); objectPropertiesWriter.append(','); objectPropertiesWriter.write(RDFTripleParser.encloseCharacterString(predicate)); objectPropertiesWriter.append(','); objectPropertiesWriter.write(RDFTripleParser.encloseCharacterString(object)); objectPropertiesWriter.newLine(); } } @Override public void handleNamespace(String arg0, String arg1) throws RDFHandlerException { } @Override public void handleComment(String arg0) throws RDFHandlerException { } @Override public void endRDF() throws RDFHandlerException { log.info("Finished parsing RDF triples " + tripleCount + " RDF triples"); } }); threads.add(new Callable<Boolean>() { /* * (non-Javadoc) * * @see java.util.concurrent.Callable#call() */ @Override public Boolean call() throws Exception { InputStream unpackedStream = getStream(stream, file_mimetype); try { if (absoluteBaseURI != null) parser.parse(unpackedStream, absoluteBaseURI); else parser.parse(unpackedStream, BASEURI); } catch (Exception e) { new Exception("Error during parsing " + source + " with mimetype " + file_mimetype, e) .printStackTrace(); unpackedStream.close(); return false; } unpackedStream.close(); return true; } }); } for (Future<Boolean> future : pool.invokeAll(threads)) { if (!future.get()) { throw new Exception("error occured during parsing"); } } // literalLanguageWriter.close(); objectPropertiesWriter.close(); datatypePropertiesWriter.close(); return stats; }