List of usage examples for org.apache.commons.io.output NullWriter NULL_WRITER
NullWriter NULL_WRITER
To view the source code for org.apache.commons.io.output NullWriter NULL_WRITER.
Click Source Link
From source file:com.github.ansell.shp.SHPDump.java
public static void main(String... args) throws Exception { final OptionParser parser = new OptionParser(); final OptionSpec<Void> help = parser.accepts("help").forHelp(); final OptionSpec<File> input = parser.accepts("input").withRequiredArg().ofType(File.class).required() .describedAs("The input SHP file"); final OptionSpec<File> output = parser.accepts("output").withRequiredArg().ofType(File.class).required() .describedAs("The output directory to use for debugging files"); final OptionSpec<String> outputPrefix = parser.accepts("prefix").withRequiredArg().ofType(String.class) .defaultsTo("shp-debug").describedAs("The output prefix to use for debugging files"); final OptionSpec<File> outputMappingTemplate = parser.accepts("output-mapping").withRequiredArg() .ofType(File.class).describedAs("The output mapping template file if it needs to be generated."); final OptionSpec<Integer> resolution = parser.accepts("resolution").withRequiredArg().ofType(Integer.class) .defaultsTo(2048).describedAs("The output image file resolution"); final OptionSpec<String> format = parser.accepts("format").withRequiredArg().ofType(String.class) .defaultsTo("png").describedAs("The output image format"); final OptionSpec<String> removeIfEmpty = parser.accepts("remove-if-empty").withRequiredArg() .ofType(String.class).describedAs( "The name of an attribute to remove if its value is empty before outputting the resulting shapefile. Use multiple times to specify multiple fields to check"); OptionSet options = null;/*from w ww . j a va 2s .com*/ try { options = parser.parse(args); } catch (final OptionException e) { System.out.println(e.getMessage()); parser.printHelpOn(System.out); throw e; } if (options.has(help)) { parser.printHelpOn(System.out); return; } final Path inputPath = input.value(options).toPath(); if (!Files.exists(inputPath)) { throw new FileNotFoundException("Could not find input SHP file: " + inputPath.toString()); } final Path outputPath = output.value(options).toPath(); if (!Files.exists(outputPath)) { throw new FileNotFoundException("Output directory does not exist: " + outputPath.toString()); } final Path outputMappingPath = options.has(outputMappingTemplate) ? outputMappingTemplate.value(options).toPath() : null; if (options.has(outputMappingTemplate) && Files.exists(outputMappingPath)) { throw new FileNotFoundException( "Output mapping template file already exists: " + outputMappingPath.toString()); } final Set<String> filterFields = ConcurrentHashMap.newKeySet(); if (options.has(removeIfEmpty)) { for (String nextFilterField : removeIfEmpty.values(options)) { System.out.println("Will filter field if empty value found: " + nextFilterField); filterFields.add(nextFilterField); } } if (!filterFields.isEmpty()) { System.out.println("Full set of filter fields: " + filterFields); } final String prefix = outputPrefix.value(options); FileDataStore store = FileDataStoreFinder.getDataStore(inputPath.toFile()); if (store == null) { throw new RuntimeException("Could not read the given input as an ESRI Shapefile: " + inputPath.toAbsolutePath().toString()); } for (String typeName : new LinkedHashSet<>(Arrays.asList(store.getTypeNames()))) { System.out.println(""); System.out.println("Type: " + typeName); SimpleFeatureSource featureSource = store.getFeatureSource(typeName); SimpleFeatureType schema = featureSource.getSchema(); Name outputSchemaName = new NameImpl(schema.getName().getNamespaceURI(), schema.getName().getLocalPart().replace(" ", "").replace("%20", "")); System.out.println("Replacing name on schema: " + schema.getName() + " with " + outputSchemaName); SimpleFeatureType outputSchema = SHPUtils.changeSchemaName(schema, outputSchemaName); List<String> attributeList = new ArrayList<>(); for (AttributeDescriptor attribute : schema.getAttributeDescriptors()) { System.out.println("Attribute: " + attribute.getName().toString()); attributeList.add(attribute.getName().toString()); } CsvSchema csvSchema = CSVUtil.buildSchema(attributeList); SimpleFeatureCollection collection = featureSource.getFeatures(); int featureCount = 0; Path nextCSVFile = outputPath.resolve(prefix + ".csv"); Path nextSummaryCSVFile = outputPath .resolve(prefix + "-" + outputSchema.getTypeName() + "-Summary.csv"); List<SimpleFeature> outputFeatureList = new CopyOnWriteArrayList<>(); try (SimpleFeatureIterator iterator = collection.features(); Writer bufferedWriter = Files.newBufferedWriter(nextCSVFile, StandardCharsets.UTF_8, StandardOpenOption.CREATE_NEW); SequenceWriter csv = CSVUtil.newCSVWriter(bufferedWriter, csvSchema);) { List<String> nextLine = new ArrayList<>(); while (iterator.hasNext()) { SimpleFeature feature = iterator.next(); featureCount++; if (featureCount <= 2) { System.out.println(""); System.out.println(feature.getIdentifier()); } else if (featureCount % 100 == 0) { System.out.print("."); } boolean filterThisFeature = false; for (AttributeDescriptor attribute : schema.getAttributeDescriptors()) { String featureString = Optional.ofNullable(feature.getAttribute(attribute.getName())) .orElse("").toString(); nextLine.add(featureString); if (filterFields.contains(attribute.getName().toString()) && featureString.trim().isEmpty()) { filterThisFeature = true; } if (featureString.length() > 100) { featureString = featureString.substring(0, 100) + "..."; } if (featureCount <= 2) { System.out.print(attribute.getName() + "="); System.out.println(featureString); } } if (!filterThisFeature) { outputFeatureList.add(SHPUtils.changeSchemaName(feature, outputSchema)); csv.write(nextLine); } nextLine.clear(); } } try (Reader csvReader = Files.newBufferedReader(nextCSVFile, StandardCharsets.UTF_8); Writer summaryOutput = Files.newBufferedWriter(nextSummaryCSVFile, StandardCharsets.UTF_8, StandardOpenOption.CREATE_NEW); final Writer mappingWriter = options.has(outputMappingTemplate) ? Files.newBufferedWriter(outputMappingPath) : NullWriter.NULL_WRITER) { CSVSummariser.runSummarise(csvReader, summaryOutput, mappingWriter, CSVSummariser.DEFAULT_SAMPLE_COUNT, false); } if (featureCount > 100) { System.out.println(""); } System.out.println(""); System.out.println("Feature count: " + featureCount); SimpleFeatureCollection outputCollection = new ListFeatureCollection(outputSchema, outputFeatureList); Path outputShapefilePath = outputPath.resolve(prefix + "-" + outputSchema.getTypeName() + "-dump"); if (!Files.exists(outputShapefilePath)) { Files.createDirectory(outputShapefilePath); } SHPUtils.writeShapefile(outputCollection, outputShapefilePath); // Create ZIP file from the contents to keep the subfiles together Path outputShapefileZipPath = outputPath .resolve(prefix + "-" + outputSchema.getTypeName() + "-dump.zip"); try (final OutputStream out = Files.newOutputStream(outputShapefileZipPath, StandardOpenOption.CREATE_NEW); final ZipOutputStream zip = new ZipOutputStream(out, StandardCharsets.UTF_8);) { Files.list(outputShapefilePath).forEachOrdered(Unchecked.consumer(e -> { zip.putNextEntry(new ZipEntry(e.getFileName().toString())); Files.copy(e, zip); zip.closeEntry(); })); } try (final OutputStream outputStream = Files.newOutputStream( outputPath.resolve(prefix + "." + format.value(options)), StandardOpenOption.CREATE_NEW);) { MapContent map = new MapContent(); map.setTitle(prefix + "-" + outputSchema.getTypeName()); Style style = SLD.createSimpleStyle(featureSource.getSchema()); Layer layer = new FeatureLayer(new CollectionFeatureSource(outputCollection), style); map.addLayer(layer); SHPUtils.renderImage(map, outputStream, resolution.value(options), format.value(options)); } } }
From source file:de.blizzy.documentr.web.filter.SwitchablePrintWriter.java
SwitchablePrintWriter() {
super(NullWriter.NULL_WRITER);
}
From source file:com.illmeyer.polygraph.messagetype.mail.tags.MimeTag.java
@Override public void execute(PolygraphEnvironment env) throws IOException { MailTag m = env.requireAncestorTag(MailTag.class); if (m.getType() == MailType.simple) throw new PolygraphTemplateException("tag not allowed in mail of type 'simple'"); Body b = m.getPartStack().peek();//from w ww .j a v a 2 s . c o m if (b == null || !(b instanceof MimeBody)) throw new PolygraphTemplateException("mime tag not allowed here"); MimeBody mb = new MimeBody(); mb.setSubType(type); ((MimeBody) m.getPartStack().peek()).getSubElements().add(mb); m.getPartStack().push(mb); try { env.executeBody(NullWriter.NULL_WRITER); } finally { m.getPartStack().pop(); } if (mb.getSubElements().size() == 0) throw new PolygraphTemplateException("need at least one mime body (use resource tag)"); }
From source file:com.francetelecom.clara.cloud.commons.PersistenceTestUtil.java
private static void forceEagerFetching(Object entity) { XStream xStream = XStreamUtils.instanciateXstreamForHibernate(); if (logger.isDebugEnabled()) { String xmlDump = xStream.toXML(entity); logger.debug("Xml dump:" + xmlDump); } else {/* ww w .j a v a 2 s . c o m*/ // dump to equivalent of /dev/null xStream.toXML(entity, NullWriter.NULL_WRITER); } }
From source file:com.illmeyer.polygraph.messagetype.mail.tags.ResourceTag.java
@Override public void execute(PolygraphEnvironment env) throws IOException { MailTag m = env.requireAncestorTag(MailTag.class); if (m.getType() == MailType.simple) { if (type == null) throw new PolygraphTemplateException( "must specify type attribute below mail tags of type 'simple'"); env.requireParentTag(MailTag.class); }//w ww. j ava 2s . c o m if (type == Disposition.attachment && fileName == null) throw new PolygraphTemplateException("attached resources need a filename"); Document d = new Document(); if (type != null) d.setDisposition(type); d.setMimeType(mimeType); d.setPartname(name); d.setFilename(fileName); if (type == Disposition.inline) { d.setContentId(MailEnvironment.createContentId()); m.getCidMap().put(d.getPartname(), d.getContentId()); } ((MimeBody) m.getPartStack().peek()).getSubElements().add(d); m.getPartStack().push(d); try { if (env.hasBody()) env.executeBody(NullWriter.NULL_WRITER); } finally { m.getPartStack().pop(); } }
From source file:com.illmeyer.polygraph.messagetype.mail.tags.MailTag.java
@Override public void execute(PolygraphEnvironment env) throws IOException { description = new MailDescription(); if (env.getCustomAttribute(MailConstants.ECA_MAILDESC) != null) throw new PolygraphTemplateException("Mail already defined"); if (type == MailType.simple) { MimeBody mainBody = new MimeBody(); mainBody.setSubType("related"); partStack.push(mainBody);//from w w w . jav a2s. c o m description.setRootElement(mainBody); if (htmlname != null && textname != null) { MimeBody alternative = new MimeBody(); alternative.setSubType("alternative"); partStack.push(alternative); mainBody.getSubElements().add(alternative); } if (textname != null) { Document d = new Document(); d.setMimeType("text/plain"); d.setPartname(textname); ((MimeBody) partStack.peek()).getSubElements().add(d); } if (htmlname != null) { Document d = new Document(); d.setMimeType("text/html"); d.setPartname(htmlname); ((MimeBody) partStack.peek()).getSubElements().add(d); } if (htmlname != null && textname != null) partStack.pop(); env.executeBody(NullWriter.NULL_WRITER); if (mainBody.getSubElements().size() == 1) { description.setRootElement(mainBody.getSubElements().get(0)); } } else if (type == MailType.mime) { MimeBody mainBody = new MimeBody(); mainBody.setSubType(subtype); description.setRootElement(mainBody); partStack.push(mainBody); try { env.executeBody(NullWriter.NULL_WRITER); } finally { partStack.pop(); } } else throw new PolygraphTemplateException(String.format("Unsupported mail type '%s'", type)); env.setCustomAttribute(MailConstants.ECA_MAILDESC, description); env.setCustomAttribute(MailConstants.ECA_CIDMAP, cidMap); }
From source file:ocr.sapphire.ann.training.NeuronNetworkTrainer.java
public void run() throws IOException { long start = System.currentTimeMillis(); PrintWriter out = null;/* www .java 2s.co m*/ try { if (reportFile == null) { out = new PrintWriter(NullWriter.NULL_WRITER); } else { out = new PrintWriter(reportFile); } if (!StringUtils.isEmpty(trainingFile)) { out.println("Training set:\t" + trainingFile); } if (!StringUtils.isEmpty(validateFile)) { out.println("Validate set:\t" + validateFile); } out.println("Parameters:"); out.println("Learning rate:\t" + ann.getNetwork().getRate()); out.println("Momentum:\t" + ann.getNetwork().getMomentum()); out.println("Number of hidden layers:\t" + (ann.getNetwork().getLayerCount() - 2)); for (int i = 1; i < ann.getNetwork().getLayers().size() - 1; i++) { out.println("Layer " + i + ":\t" + ann.getNetwork().getLayers().get(i).getSize()); } if (reportTrainingError) { out.println("Iteration\tTraining error\tValidating error\tPerformance"); } else { out.println("Iteration\tValidating error\tPerformance"); } bestValidateError = Double.MAX_VALUE; bestPerformance = 0; double uselessIterationCounter = 0; int iteration = 0; outerFor: for (;;) { Collections.shuffle(trainingSet); for (ProcessedSample sample : trainingSet) { ann.train(sample); } iteration++; System.out.println(iteration); computeErrorAndPerformance(); if (reportTrainingError) { out.format("%d\t%f\t%f\t%f\n", iteration, trainingError, validateError, performance); } else { out.format("%d\t%f\t%f\n", iteration, validateError, performance); } // save the best network if needed if (validateError < bestValidateError) { bestNetwork = Utils.copy(ann); uselessIterationCounter = 0; } else { uselessIterationCounter += 1; if (uselessIterationCounter > USELESS_ITERATION_BEFORE_STOP) { break outerFor; } } if (performance > bestPerformance) { bestPerformance = performance; } if (validateError < bestValidateError) { bestValidateError = validateError; } } out.println("Best performance: " + bestPerformance); out.println("Best validate error: " + bestValidateError); out.println("20 most problematic chars: "); for (int i = 0; i < 20 && !errorChar.isEmpty(); i++) { char ch = errorChar.lastKey(); out.println(ch); errorChar.remove(ch); } } finally { if (out != null) { out.close(); } } long stop = System.currentTimeMillis(); trainingTime = (stop - start) / 1000.0 / 60.0; }
From source file:org.apereo.portal.portlet.container.cache.CachingPortletOutputHandlerTest.java
@Test public void testBasicWriterCaching() throws IOException { final CachingPortletOutputHandler cachingOutputHandler = new CachingPortletOutputHandler( portletOutputHandler, 10000); when(portletOutputHandler.getPrintWriter()).thenReturn(new PrintWriter(NullWriter.NULL_WRITER)); final String output = "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"; final PrintWriter printWriter = cachingOutputHandler.getPrintWriter(); printWriter.write(output);//from w w w . j av a 2s . c o m final CachedPortletData<Long> cachedPortletData = cachingOutputHandler.getCachedPortletData(1l, new CacheControlImpl()); assertNotNull(cachedPortletData); assertEquals(output, cachedPortletData.getCachedWriterOutput()); }
From source file:org.apereo.portal.portlet.container.cache.CachingPortletOutputHandlerTest.java
@Test public void testTooMuchWriterContent() throws IOException { final CachingPortletOutputHandler cachingOutputHandler = new CachingPortletOutputHandler( portletOutputHandler, 100);/*www . ja v a 2s. c o m*/ when(portletOutputHandler.getPrintWriter()).thenReturn(new PrintWriter(NullWriter.NULL_WRITER)); final String output = "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"; final PrintWriter printWriter = cachingOutputHandler.getPrintWriter(); printWriter.write(output); printWriter.write(output); final CachedPortletData<Long> cachedPortletData = cachingOutputHandler.getCachedPortletData(1l, new CacheControlImpl()); assertNull(cachedPortletData); }
From source file:org.apereo.portal.portlet.container.cache.CachingPortletOutputHandlerTest.java
@Test public void testTooMuchWriterContentThenReset() throws IOException { final CachingPortletOutputHandler cachingOutputHandler = new CachingPortletOutputHandler( portletOutputHandler, 100);/*from w w w .jav a 2s . c o m*/ when(portletOutputHandler.getPrintWriter()).thenReturn(new PrintWriter(NullWriter.NULL_WRITER)); final String output = "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"; final PrintWriter printWriter = cachingOutputHandler.getPrintWriter(); printWriter.write(output); printWriter.write(output); //Verify limit was hit CachedPortletData<Long> cachedPortletData = cachingOutputHandler.getCachedPortletData(1l, new CacheControlImpl()); assertNull(cachedPortletData); cachingOutputHandler.reset(); printWriter.write(output); //Verify limit reset cachedPortletData = cachingOutputHandler.getCachedPortletData(1l, new CacheControlImpl()); assertNotNull(cachedPortletData); assertEquals(output, cachedPortletData.getCachedWriterOutput()); }