@Override @SuppressWarnings("SleepWhileHoldingLock") public void run() { try { // initialize the statusbar status.removeAll(); JProgressBar progress = new JProgressBar(); progress.setMinimum(0); progress.setMaximum(doc.getLength()); status.add(progress); status.revalidate(); // start writing Writer out = new FileWriter(f); Segment text = new Segment(); text.setPartialReturn(true); int charsLeft = doc.getLength(); int offset = 0; while (charsLeft > 0) { doc.getText(offset, Math.min(4096, charsLeft), text); out.write(text.array, text.offset, text.count); charsLeft -= text.count; offset += text.count; progress.setValue(offset); try { Thread.sleep(10); } catch (InterruptedException e) { Logger.getLogger(FileSaver.class.getName()).log(Level.SEVERE, null, e); } } out.flush(); out.close(); } catch (IOException e) { final String msg = e.getMessage(); SwingUtilities.invokeLater( new Runnable() { public void run() { JOptionPane.showMessageDialog( getFrame(), "Could not save file: " + msg, "Error saving file", JOptionPane.ERROR_MESSAGE); } }); } catch (BadLocationException e) { System.err.println(e.getMessage()); } // we are done... get rid of progressbar status.removeAll(); status.revalidate(); }
public static void main(String[] args) throws Exception { String sourceUrlString = "data/test.html"; if (args.length == 0) System.err.println("Using default argument of \"" + sourceUrlString + '"'); else sourceUrlString = args[0]; if (sourceUrlString.indexOf(':') == -1) sourceUrlString = "file:" + sourceUrlString; StreamedSource streamedSource = new StreamedSource(new URL(sourceUrlString)); // streamedSource.setBuffer(new char[65000]); // uncomment this to use a fixed buffer size Writer writer = null; try { writer = new OutputStreamWriter( new FileOutputStream("StreamedSourceCopyOutput.html"), streamedSource.getEncoding()); System.out.println("Processing segments:"); int lastSegmentEnd = 0; for (Segment segment : streamedSource) { System.out.println(segment.getDebugInfo()); if (segment.getEnd() <= lastSegmentEnd) continue; // if this tag is inside the previous tag (e.g. a server tag) then ignore it as // it was already output along with the previous tag. lastSegmentEnd = segment.getEnd(); if (segment instanceof Tag) { Tag tag = (Tag) segment; // HANDLE TAG // Uncomment the following line to ensure each tag is valid XML: // writer.write(tag.tidy()); continue; } else if (segment instanceof CharacterReference) { CharacterReference characterReference = (CharacterReference) segment; // HANDLE CHARACTER REFERENCE // Uncomment the following line to decode all character references instead of copying them // verbatim: // characterReference.appendCharTo(writer); continue; } else { // HANDLE PLAIN TEXT } // unless specific handling has prevented getting to here, simply output the segment as is: writer.write(segment.toString()); } writer.close(); System.err.println( "\nA copy of the source document has been output to StreamedSourceCopyOuput.html"); } catch (Exception ex) { if (writer != null) try { writer.close(); } catch (IOException ex2) { } throw ex; } }
private void writeTextCollapseWhiteSpace(final int end, final int depth) throws IOException { // sets index to end // assert index < end boolean lastWasWhiteSpace = false; updateNextTag(); while (index < end) { while (nextTag != null && index == nextTag.begin) { if (lastWasWhiteSpace) { writer.write(' '); lastWasWhiteSpace = false; } writeTag(nextTag, depth, end); if (index == end) return; } final char ch = sourceText.charAt(index++); if (Segment.isWhiteSpace(ch)) { lastWasWhiteSpace = true; } else { if (lastWasWhiteSpace) { writer.write(' '); lastWasWhiteSpace = false; } writer.write(ch); } } if (lastWasWhiteSpace) writer.write(' '); }
public void writeTo(final Writer writer) throws IOException { this.writer = writer; if (segment instanceof Source) ((Source) segment).fullSequentialParse(); nextTag = segment.source.findNextTag(segment.begin); index = segment.begin; writeContent(segment.end, segment.getChildElements(), 0); writer.flush(); }
/** Test of evaluateAt method, of class ParameterisedGroup. */ @Test public void testEvaluateAt() throws ParameterException { System.out.println("--- testEvaluateAt..."); // SegmentLocation loc1 = new PostSynapticTerminalLocation(d2.getSegmentId(), 0.5f); assertEquals(0.5, pg1.evaluateAt(cell, d2, 0.5f), 0); assertEquals(15, pg2.evaluateAt(cell, d2, 0.5f), 0); //////// assertEquals(15, pg5.evaluateAt(cell, loc1), 0); // SegmentLocation loc2 = new PostSynapticTerminalLocation(d3.getSegmentId(), 0.5f); assertEquals(5d / 6d, pg1.evaluateAt(cell, d3, 0.5f), 1e-7); assertEquals(25, pg2.evaluateAt(cell, d3, 0.5f), 0); assertEquals(0.5, pg3.evaluateAt(cell, d3, 0.5f), 0); assertEquals(0, pg4.evaluateAt(cell, d3, 0), 0); assertEquals(5, pg4.evaluateAt(cell, d3, 0.5f), 0); SegmentLocation sl1 = new SegmentLocation(d3.getSegmentId(), 0.5f); assertEquals( 25, pg5.evaluateAt(cell, cell.getSegmentWithId(sl1.getSegmentId()), sl1.getFractAlong()), 0); assertEquals(30, pg5.evaluateAt(cell, d3, 1), 0); GenesisCompartmentalisation g = new GenesisCompartmentalisation(); Cell gCell = g.getCompartmentalisation(cell); System.out.println(CellTopologyHelper.printDetails(cell, null)); System.out.println( "Changed from morph: " + cell.getMorphSummary() + ", to morph: " + gCell.getMorphSummary()); System.out.println("getSegmentMapper: " + g.getSegmentMapper()); System.out.println(CellTopologyHelper.printDetails(gCell, null)); SegmentLocation sl1_g = g.getSegmentMapper().mapSegmentLocation(sl1); System.out.println("Mapped: " + sl1 + " to " + sl1_g); assertEquals( 25, pg5.evaluateAt(gCell, gCell.getSegmentWithId(sl1_g.getSegmentId()), sl1_g.getFractAlong()), 0); }
private void writeText(final int end, int depth) throws IOException { // sets index to end if (index == end) return; while (Segment.isWhiteSpace(sourceText.charAt(index))) if (++index == end) return; // trim whitespace. writeIndent(depth); if (collapseWhiteSpace) { writeTextCollapseWhiteSpace(end, depth); } else { writeTextInline(end, depth, false); } writeFormattingNewLine(); }
private void writeSpecifiedTextInline(final CharSequence text, int depth) throws IOException { final int textLength = text.length(); int i = writeSpecifiedLine(text, 0); if (i < textLength) { final int subsequentLineDepth = depth + 1; do { while (Segment.isWhiteSpace(text.charAt(i))) if (++i >= textLength) return; // trim whitespace. writeEssentialNewLine(); writeIndent(subsequentLineDepth); i = writeSpecifiedLine(text, i); } while (i < textLength); } }
private boolean writeTextInline( final int end, int depth, final boolean increaseIndentAfterFirstLineBreak) throws IOException { // returns true if all text was on one line, otherwise false // sets index to end // assert index < end writeLineKeepWhiteSpace(end, depth); if (index == end) return true; final int subsequentLineDepth = increaseIndentAfterFirstLineBreak ? depth + 1 : depth; do { while (Segment.isWhiteSpace(sourceText.charAt(index))) if (++index == end) return false; // trim whitespace. writeEssentialNewLine(); // essential because we might be inside a tag attribute value. If // new lines in normal text aren't required this method wouldn't // have been called. writeIndent(subsequentLineDepth); writeLineKeepWhiteSpace(end, subsequentLineDepth); } while (index < end); return false; }
// Documentation inherited from CharStreamSource public long getEstimatedMaximumOutputLength() { return segment.length() * 2; }
@Before public void setUp() { System.out.println("--------------- setUp() ParameterisedGroupTest"); cell = new OneSegment("Simple"); d1 = cell.addDendriticSegment( 1, "d1", new Point3f(10, 0, 0), cell.getFirstSomaSegment(), 1, "Sec1", false); d2 = cell.addDendriticSegment(2, "d2", new Point3f(20, 0, 0), d1, 1, "Sec2", false); d3 = cell.addDendriticSegment(7, "d3", new Point3f(20, 10, 0), d2, 1, "Sec3", false); d3.getSection().setStartRadius(3); d1.getSection().setNumberInternalDivisions(4); d2.getSection().setNumberInternalDivisions(3); d3.getSection().setNumberInternalDivisions(5); d3.getSection().addToGroup(tipSection); pg1 = new ParameterisedGroup( "ZeroToOne", Section.DENDRITIC_GROUP, Metric.PATH_LENGTH_FROM_ROOT, ProximalPref.MOST_PROX_AT_0, DistalPref.MOST_DIST_AT_1, "p1"); pg2 = new ParameterisedGroup( "StartToEnd", Section.DENDRITIC_GROUP, Metric.PATH_LENGTH_FROM_ROOT, ProximalPref.NO_TRANSLATION, DistalPref.NO_NORMALISATION, "p2"); pg3 = new ParameterisedGroup( "TipZeroToOne", tipSection, Metric.PATH_LENGTH_FROM_ROOT, ProximalPref.MOST_PROX_AT_0, DistalPref.MOST_DIST_AT_1, "p3"); pg4 = new ParameterisedGroup( "TipToEnd", tipSection, Metric.PATH_LENGTH_FROM_ROOT, ProximalPref.MOST_PROX_AT_0, DistalPref.NO_NORMALISATION, "p4"); pg5 = new ParameterisedGroup( "TipToEnd_PathFromRoot", tipSection, Metric.PATH_LENGTH_FROM_ROOT, ProximalPref.NO_TRANSLATION, DistalPref.NO_NORMALISATION, "p5"); /* pg5 = new ParameterisedGroup("3DDistZeroToOne", Section.ALL, Metric.THREE_D_RADIAL_POSITION, ProximalPref.MOST_PROX_AT_0, DistalPref.MOST_DIST_AT_1);*/ }
private void dnaCommand(HttpServletRequest req, DazzleResponse resp, DazzleDataSource dds) throws IOException, DataSourceException, ServletException, DazzleException { DazzleReferenceSource drs = (DazzleReferenceSource) dds; List segments = DazzleTools.getSegments(dds, req, resp); if (segments.size() == 0) { throw new DazzleException( DASStatus.STATUS_BAD_COMMAND_ARGUMENTS, "No segments specified for dna command"); } // Fetch and validate the requests. Map segmentResults = new HashMap(); for (Iterator i = segments.iterator(); i.hasNext(); ) { Segment seg = (Segment) i.next(); try { Sequence seq = drs.getSequence(seg.getReference()); if (seq.getAlphabet() != DNATools.getDNA()) { throw new DazzleException( DASStatus.STATUS_SERVER_ERROR, "Sequence " + seg.toString() + " is not in the DNA alphabet"); } if (seg.isBounded()) { if (seg.getMin() < 1 || seg.getMax() > seq.length()) { throw new DazzleException( DASStatus.STATUS_BAD_COORDS, "Segment " + seg.toString() + " doesn't fit sequence of length " + seq.length()); } } segmentResults.put(seg, seq); } catch (NoSuchElementException ex) { throw new DazzleException(DASStatus.STATUS_BAD_REFERENCE, ex); } catch (DataSourceException ex) { throw new DazzleException(DASStatus.STATUS_SERVER_ERROR, ex); } } // // Looks okay -- generate the response document // XMLWriter xw = resp.startDasXML("DASDNA", "dasdna.dtd"); try { xw.openTag("DASDNA"); for (Iterator i = segmentResults.entrySet().iterator(); i.hasNext(); ) { Map.Entry me = (Map.Entry) i.next(); Segment seg = (Segment) me.getKey(); Sequence seq = (Sequence) me.getValue(); xw.openTag("SEQUENCE"); xw.attribute("id", seg.getReference()); xw.attribute("version", drs.getLandmarkVersion(seg.getReference())); if (seg.isBounded()) { xw.attribute("start", "" + seg.getStart()); xw.attribute("stop", "" + seg.getStop()); } else { xw.attribute("start", "" + 1); xw.attribute("stop", "" + seq.length()); } SymbolList syms = seq; if (seg.isBounded()) { syms = syms.subList(seg.getMin(), seg.getMax()); } if (seg.isInverted()) { syms = DNATools.reverseComplement(syms); } xw.openTag("DNA"); xw.attribute("length", "" + syms.length()); for (int pos = 1; pos <= syms.length(); pos += 60) { int maxPos = Math.min(syms.length(), pos + 59); xw.println(syms.subStr(pos, maxPos)); } xw.closeTag("DNA"); xw.closeTag("SEQUENCE"); } xw.closeTag("DASDNA"); xw.close(); } catch (Exception ex) { throw new DazzleException(ex, "Error writing DNA document"); } }
/** * Replaces the specified {@linkplain Segment segment} in this output document with the specified * text. * * <p>Specifying a <code>null</code> argument to the <code>text</code> parameter is exactly * equivalent to specifying an empty string, and results in the segment being completely removed * from the output document. * * @param segment the segment to replace. * @param text the replacement text, or <code>null</code> to remove the segment. */ public void replace(final Segment segment, final CharSequence text) { replace(segment.getBegin(), segment.getEnd(), text); }