/** Convert an XML string to <code>org.simpleframework.xml</code>-enabled object. */ public static <T> T fromString(Class<T> clazz, String xml) throws IOException { try { final StringReader r = new StringReader(xml); return new Persister().read(clazz, r); } catch (Exception e) { throw ExceptionUtils.wrapAs(IOException.class, e); } }
public void process() throws ProcessingException { try { final SearchEngineResponse response = fetchSearchResponse(); documents = response.results; resultsTotal = response.getResultsTotal(); } catch (Exception e) { throw ExceptionUtils.wrapAs(ProcessingException.class, e); } }
/** Creates an {@link XMLMemento} from Simple XML-annotated bean. */ static IMemento toMemento(Object benchmarkSettings) throws IOException { try { final StringWriter w = new StringWriter(); new Persister().write(benchmarkSettings, w); XMLMemento memento = XMLMemento.createReadRoot(new StringReader(w.toString())); return memento; } catch (Exception e) { throw ExceptionUtils.wrapAs(IOException.class, e); } }
/** Convert any <code>org.simpleframework.xml</code>-enabled object to an XML string. */ public static String toString(Object object) throws IOException { checkObject(object); try { final StringWriter w = new StringWriter(); new Persister().write(object, w); return w.toString(); } catch (Exception e) { throw ExceptionUtils.wrapAs(IOException.class, e); } }
/** * Reads an object from a {@link IMemento}. The memento's type (root) must equal the bean's {@link * Root} annotation name attribute. */ static <T> T fromMemento(Class<T> clazz, IMemento memento) throws IOException { try { final StringWriter sw = new StringWriter(); final XMLMemento m = XMLMemento.createWriteRoot(memento.getType()); m.putMemento(memento); m.save(sw); return new Persister().read(clazz, new StringReader(sw.toString())); } catch (Exception e) { throw ExceptionUtils.wrapAs(IOException.class, e); } }
/** Open or retrieve an open handle to an {@link IndexSearcher}. */ private IndexSearcher indexOpen(Directory directory) throws ProcessingException { synchronized (context) { IndexSearcher searcher = openIndexes.get(directory); if (searcher == null) { try { searcher = new IndexSearcher(DirectoryReader.open(directory)); openIndexes.put(directory, searcher); } catch (IOException e) { throw ExceptionUtils.wrapAs(ProcessingException.class, e); } } return searcher; } }
/** Performs tokenization and saves the results to the <code>context</code>. */ public void tokenize(PreprocessingContext context) { // Documents to tokenize final List<Document> documents = context.documents; // Fields to tokenize final String[] fieldNames = documentFields.toArray(new String[documentFields.size()]); if (fieldNames.length > 8) { throw new ProcessingException("Maximum number of tokenized fields is 8."); } // Prepare arrays images = Lists.newArrayList(); tokenTypes = new ShortArrayList(); documentIndices = new IntArrayList(); fieldIndices = new ByteArrayList(); final Iterator<Document> docIterator = documents.iterator(); int documentIndex = 0; final ITokenizer ts = context.language.getTokenizer(); final MutableCharArray wrapper = new MutableCharArray(CharArrayUtils.EMPTY_ARRAY); while (docIterator.hasNext()) { final Document doc = docIterator.next(); boolean hadTokens = false; for (int i = 0; i < fieldNames.length; i++) { final byte fieldIndex = (byte) i; final String fieldName = fieldNames[i]; final String fieldValue = doc.getField(fieldName); if (!StringUtils.isEmpty(fieldValue)) { try { short tokenType; ts.reset(new StringReader(fieldValue)); if ((tokenType = ts.nextToken()) != ITokenizer.TT_EOF) { if (hadTokens) addFieldSeparator(documentIndex); do { ts.setTermBuffer(wrapper); add(documentIndex, fieldIndex, context.intern(wrapper), tokenType); } while ((tokenType = ts.nextToken()) != ITokenizer.TT_EOF); hadTokens = true; } } catch (IOException e) { // Not possible (StringReader above)? throw ExceptionUtils.wrapAsRuntimeException(e); } } } if (docIterator.hasNext()) { addDocumentSeparator(); } documentIndex++; } addTerminator(); // Save results in the PreprocessingContext context.allTokens.documentIndex = documentIndices.toArray(); context.allTokens.fieldIndex = fieldIndices.toArray(); context.allTokens.image = images.toArray(new char[images.size()][]); context.allTokens.type = tokenTypes.toArray(); context.allFields.name = fieldNames; // Clean up images = null; fieldIndices = null; tokenTypes = null; documentIndices = null; }