public void buildDict(Set<String> dict) throws IOException { int c = 0; for (String key : dict) { if (++c % 1000 == 0) { writer.flush(); System.err.println(c + "/" + dict.size() + " items processed."); } IdTouple touple = getIdTouple(key); // touple.oid = guessOther(key); writer .append(key) .append("\t") .append(touple.gid) .append("\t") .append(touple.pname) /*.append("\t").append(touple.oid)*/ .append("\n"); writer.flush(); } writer.close(); if (file != null) { FileWriter fw = new FileWriter(file); fw.write(new Date().toString()); fw.close(); } }
public void createExcel(String file) { String filename = createPath(file); System.out.println(filename); /* try { WorkbookSettings ws = new WorkbookSettings(); ws.setLocale(new Locale("sv", "SE")); WritableWorkbook workbook = Workbook.createWorkbook(new File(filename), ws); WritableSheet s = workbook.createSheet("Sheet1", 0); //writeDataSheet(s); workbook.write(); workbook.close(); } catch (IOException e) { e.printStackTrace(); } catch (WriteException e) { e.printStackTrace(); }**/ try { File newFile = new File(filename); Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(newFile), "UTF8")); // BufferedWrite output = new BufferedWriter(new OutputStreamReader(new // FileInputS§tream(filename), "iso-8859-1")); // FileWriter fw = new FileWriter(newFile.getAbsoluteFile()); // BufferedWriter bw = new BufferedWriter(fw); // bw.write("Artikel;Antal/st;Pris/st;Total\n"); out.append("Artikel;Antal/st;Pris/st;Total\n"); for (Data d : dataList) { out.append(d.toString()); out.append("\n"); // bw.write(d.toString()); // bw.write("\n"); } // bw.close(); out.flush(); out.close(); } catch (UnsupportedEncodingException unsuppEn) { unsuppEn.printStackTrace(); } catch (IOException ioE) { ioE.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } }
private void generateReport(String fileName, Template featureResult, VelocityContext context) throws Exception { Writer writer = new FileWriter(new File(reportDirectory, fileName)); featureResult.merge(context, writer); writer.flush(); writer.close(); }
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(); }
private void println(String s) { if (logger != null) { try { logger.append(s).append("\n"); logger.flush(); } catch (IOException e) { e.printStackTrace(); } } }
private static void sendServerProblemResponse(HttpServletResponse res, String s) throws IOException { // Commenting out the status code because we want the client to eval the error() call // res.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); res.setContentType("x-marklogic/xquery; charset=UTF-8"); if (s != null && s.length() > 4096) { // Cap super long errors s = s.substring(0, 2048) + " ...[trimmed]... " + s.substring(s.length() - 2048); } Writer writer = res.getWriter(); writer.write("error('" + escapeSingleQuotes(s) + "')"); writer.flush(); }
@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(); }
private Object request( String url, boolean post, Hashtable params, boolean basicAuth, boolean processOutput) throws Exception { HttpConnection conn = null; Writer writer = null; InputStream is = null; try { if (!post && (params != null)) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); OutputStreamWriter osw = new OutputStreamWriter(baos, this.encoding); this.encodeParams(params, osw); osw.flush(); osw.close(); osw = null; url += "?" + new String(baos.toByteArray(), this.encoding); baos = null; } conn = (HttpConnection) Connector.open(url); conn.setRequestMethod(post ? HttpConnection.POST : HttpConnection.GET); if (basicAuth) { if (!this.areCredentialsSet()) throw new Exception("Credentials are not set"); String token = base64Encode((this.user + ":" + this.pass).getBytes(this.encoding)); conn.setRequestProperty("Authorization", "Basic " + token); } if (post && (params != null)) { OutputStream os = conn.openOutputStream(); writer = new OutputStreamWriter(os, this.encoding); this.encodeParams(params, writer); writer.flush(); writer.close(); os = null; writer = null; } int code = conn.getResponseCode(); if ((code != 200) && (code != 302)) throw new Exception("Unexpected response code " + code + ": " + conn.getResponseMessage()); is = conn.openInputStream(); if (processOutput) { synchronized (this.json) { return this.json.parse(is); } } else { this.pump(is, System.out, 1024); return null; } } finally { if (writer != null) writer.close(); if (is != null) is.close(); if (conn != null) conn.close(); } }
public boolean continueShellProcess(Process proc) { if (progIndicator.isAborted()) { try { Writer stream; stream = new OutputStreamWriter((BufferedOutputStream) proc.getOutputStream()); stream.write((char) 3); stream.flush(); stream.close(); } catch (IOException e) { } return false; } return true; }
public static void toJSON(ConfigurationAdmin admin, Writer osw, String filter) throws Exception { Configuration[] list = admin.listConfigurations(filter); Encoder encoder = codec.enc().to(osw); Protocol p = new Protocol(); p.version = 1; p.date = new Date(); p.size = list.length; encoder.put(p).append('\n'); if (list != null) for (Configuration c : list) { Dictionary<String, Object> d = c.getProperties(); Export export = new Export(); export.values = new HashMap<String, Object>(); export.factoryPid = c.getFactoryPid(); export.pid = c.getPid(); for (Enumeration<String> e = d.keys(); e.hasMoreElements(); ) { String k = e.nextElement(); Object v = d.get(k); if (!(v instanceof String)) { if (export.types == null) export.types = new HashMap<String, Type>(); Type type = new Type(); Class<?> clazz = v.getClass(); if (v instanceof Collection) { Collection<?> coll = (Collection<?>) v; clazz = String.class; if (coll.size() > 0) type.vectorOf = shortName(coll.iterator().next().getClass()); else type.vectorOf = shortName(String.class); } else if (v.getClass().isArray()) { type.arrayOf = shortName(clazz.getComponentType()); } else type.scalar = shortName(v.getClass()); export.types.put(k, type); } export.values.put(k, v); } encoder.mark().put(export); // encoder.put(encoder.digest()); encoder.append('\n'); } osw.flush(); }
private boolean handleMultiPartGet( HttpServletRequest request, HttpServletResponse response, Repository db, String scope, String pattern) throws Exception { String boundary = createBoundaryString(); response.setHeader( ProtocolConstants.HEADER_CONTENT_TYPE, "multipart/related; boundary=\"" + boundary + '"'); // $NON-NLS-1$ OutputStream outputStream = response.getOutputStream(); Writer out = new OutputStreamWriter(outputStream); try { out.write("--" + boundary + EOL); // $NON-NLS-1$ out.write( ProtocolConstants.HEADER_CONTENT_TYPE + ": " + ProtocolConstants.CONTENT_TYPE_JSON + EOL + EOL); //$NON-NLS-1$ out.flush(); JSONObject getURIs = new Diff(getURI(request), db).toJSON(); JsonURIUnqualificationStrategy.ALL.run(request, getURIs); out.write(getURIs.toString()); out.write(EOL + "--" + boundary + EOL); // $NON-NLS-1$ out.write(ProtocolConstants.HEADER_CONTENT_TYPE + ": plain/text" + EOL + EOL); // $NON-NLS-1$ out.flush(); handleGetDiff(request, response, db, scope, pattern, outputStream); out.write(EOL); out.flush(); } finally { IOUtilities.safeClose(out); } return true; }
public static int copy(Reader input, Writer output) throws IOException { char[] buffer = new char[8 * 1024]; int count = 0; int n = 0; try { while (-1 != (n = input.read(buffer))) { output.write(buffer, 0, n); count += n; } } finally { output.flush(); output.close(); } return count; }
/** * Write the data in a Graph to a GML OutputStream. * * @param gMLOutputStream the GML OutputStream to write the Graph data to * @throws java.io.IOException thrown if there is an error generating the GML data */ public void outputGraph(final OutputStream gMLOutputStream) throws IOException { // ISO 8859-1 as specified in the GML documentation Writer writer = new BufferedWriter(new OutputStreamWriter(gMLOutputStream, Charset.forName("ISO-8859-1"))); List<Vertex> verticies = new ArrayList<Vertex>(); List<Edge> edges = new ArrayList<Edge>(); populateLists(verticies, edges); if (normalize) { LexicographicalElementComparator comparator = new LexicographicalElementComparator(); Collections.sort(verticies, comparator); Collections.sort(edges, comparator); } writeGraph(writer, verticies, edges); writer.flush(); writer.close(); }
/** * Writes the specified portion of the final content of this output document to the specified * <code>Writer</code>. * * <p>Any zero-length output segments located at <code>begin</code> or <code>end</code> are * included in the output. * * @param writer the destination <code>java.io.Writer</code> for the output. * @param begin the character position at which to start the output, inclusive. * @param end the character position at which to end the output, exclusive. * @throws IOException if an I/O exception occurs. * @see #writeTo(Writer) */ public void writeTo(final Writer writer, final int begin, final int end) throws IOException { try { if (outputSegments.isEmpty()) { Util.appendTo(writer, sourceText, begin, end); return; } int pos = begin; Collections.sort(outputSegments, OutputSegment.COMPARATOR); for (final Iterator i = outputSegments.iterator(); i.hasNext(); ) { final OutputSegment outputSegment = (OutputSegment) i.next(); if (outputSegment.getEnd() < pos) continue; // skip output segments before begin, and any that are enclosed by other output // segments if (outputSegment.getEnd() == pos && outputSegment.getBegin() < pos) continue; // skip output segments that end at pos unless they are zero length if (outputSegment.getBegin() > end) break; // stop processing output segments if they are not longer in the desired output // range if (outputSegment.getBegin() == end && outputSegment.getEnd() > end) break; // stop processing output segments if they start at end unless they are zero length if (outputSegment.getBegin() > pos) { Util.appendTo(writer, sourceText, pos, outputSegment.getBegin()); } if (outputSegment.getBegin() < pos && outputSegment instanceof BlankOutputSegment) { // Overlapping BlankOutputSegments requires special handling to ensure the correct number // of blanks are inserted. for (final int outputSegmentEnd = outputSegment.getEnd(); pos < outputSegmentEnd; pos++) writer.write(' '); } else { outputSegment.writeTo(writer); pos = outputSegment.getEnd(); } } if (pos < end) Util.appendTo(writer, sourceText, pos, end); } finally { writer.flush(); } }
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // get the number of workers to run int count = this.getRequestedCount(request, 4); // get the executor service final ServletContext sc = this.getServletContext(); final ExecutorService executorService = (ExecutorService) sc.getAttribute("myExecutorService"); // create work coordinator CountDownLatch countDownLatch = new CountDownLatch(count); Long t1 = System.nanoTime(); // create the workers List<RunnableWorkUnit2> workers = new ArrayList<RunnableWorkUnit2>(); for (int i = 0; i < count; i++) { RunnableWorkUnit2 wu = new RunnableWorkUnit2("RunnableTask" + String.valueOf(i + 1), countDownLatch); workers.add(wu); } // run the workers through the executor for (RunnableWorkUnit2 wu : workers) { executorService.execute(wu); } try { System.out.println("START WAITING"); countDownLatch.await(); System.out.println("DONE WAITING"); } catch (InterruptedException ex) { ex.printStackTrace(); } Long t2 = System.nanoTime(); Writer w = response.getWriter(); w.write(String.format("\n Request processed in %dms!", (t2 - t1) / 1000000)); w.flush(); w.close(); }
/** Flushes and closes the output stream to the process. */ public synchronized void outClose() throws IOException { out.flush(); out.close(); }
private static void sendXQueryResponse(HttpServletResponse res, Object o) throws IOException { // Make sure to leave the status code alone. It defaults to 200, but sometimes // callers of this method will have set it to a custom code. res.setContentType("x-marklogic/xquery; charset=UTF-8"); // res.setContentType("text/plain"); Writer writer = res.getWriter(); // care to handle errors later? if (o == null) { writer.write("()"); } else if (o instanceof byte[]) { writer.write("binary {'"); writer.write(hexEncode((byte[]) o)); writer.write("'}"); } else if (o instanceof Object[]) { Object[] arr = (Object[]) o; writer.write("("); for (int i = 0; i < arr.length; i++) { sendXQueryResponse(res, arr[i]); if (i + 1 < arr.length) writer.write(", "); } writer.write(")"); } else if (o instanceof String) { writer.write("'"); writer.write(escapeSingleQuotes(o.toString())); writer.write("'"); } else if (o instanceof Integer) { writer.write("xs:int("); writer.write(o.toString()); writer.write(")"); } else if (o instanceof Long) { writer.write("xs:integer("); writer.write(o.toString()); writer.write(")"); } else if (o instanceof Float) { Float flt = (Float) o; writer.write("xs:float("); if (flt.equals(Float.POSITIVE_INFINITY)) { writer.write("'INF'"); } else if (flt.equals(Float.NEGATIVE_INFINITY)) { writer.write("'-INF'"); } else if (flt.equals(Float.NaN)) { writer.write("fn:number(())"); // poor man's way to write NaN } else { writer.write(o.toString()); } writer.write(")"); } else if (o instanceof Double) { Double dbl = (Double) o; writer.write("xs:double("); if (dbl.equals(Double.POSITIVE_INFINITY)) { writer.write("'INF'"); } else if (dbl.equals(Double.NEGATIVE_INFINITY)) { writer.write("'-INF'"); } else if (dbl.equals(Double.NaN)) { writer.write("fn:number(())"); // poor man's way to write NaN } else { writer.write(o.toString()); } writer.write(")"); } else if (o instanceof Boolean) { writer.write("xs:boolean('"); writer.write(o.toString()); writer.write("')"); } else if (o instanceof BigDecimal) { writer.write("xs:decimal("); writer.write(o.toString()); writer.write(")"); } else if (o instanceof Date) { // We want something like: 2006-04-30T01:28:30.499-07:00 // We format to get: 2006-04-30T01:28:30.499-0700 // Then we add in the colon writer.write("xs:dateTime('"); String d = dateFormat.format((Date) o); writer.write(d.substring(0, d.length() - 2)); writer.write(":"); writer.write(d.substring(d.length() - 2)); writer.write("')"); } else if (o instanceof XMLGregorianCalendar) { XMLGregorianCalendar greg = (XMLGregorianCalendar) o; QName type = greg.getXMLSchemaType(); if (type.equals(DatatypeConstants.DATETIME)) { writer.write("xs:dateTime('"); } else if (type.equals(DatatypeConstants.DATE)) { writer.write("xs:date('"); } else if (type.equals(DatatypeConstants.TIME)) { writer.write("xs:time('"); } else if (type.equals(DatatypeConstants.GYEARMONTH)) { writer.write("xs:gYearMonth('"); } else if (type.equals(DatatypeConstants.GMONTHDAY)) { writer.write("xs:gMonthDay('"); } else if (type.equals(DatatypeConstants.GYEAR)) { writer.write("xs:gYear('"); } else if (type.equals(DatatypeConstants.GMONTH)) { writer.write("xs:gMonth('"); } else if (type.equals(DatatypeConstants.GDAY)) { writer.write("xs:gDay('"); } writer.write(greg.toXMLFormat()); writer.write("')"); } else if (o instanceof Duration) { Duration dur = (Duration) o; /* // The following fails on Xerces QName type = dur.getXMLSchemaType(); if (type.equals(DatatypeConstants.DURATION)) { writer.write("xs:duration('"); } else if (type.equals(DatatypeConstants.DURATION_DAYTIME)) { writer.write("xdt:dayTimeDuration('"); } else if (type.equals(DatatypeConstants.DURATION_YEARMONTH)) { writer.write("xdt:yearMonthDuration('"); } */ // If no years or months, must be DURATION_DAYTIME if (dur.getYears() == 0 && dur.getMonths() == 0) { writer.write("xdt:dayTimeDuration('"); } // If has years or months but nothing else, must be DURATION_YEARMONTH else if (dur.getDays() == 0 && dur.getHours() == 0 && dur.getMinutes() == 0 && dur.getSeconds() == 0) { writer.write("xdt:yearMonthDuration('"); } else { writer.write("xs:duration('"); } writer.write(dur.toString()); writer.write("')"); } else if (o instanceof org.jdom.Element) { org.jdom.Element elt = (org.jdom.Element) o; writer.write("xdmp:unquote('"); // Because "<" in XQuery is the same as "<" I need to double escape any ampersands writer.write( new org.jdom.output.XMLOutputter() .outputString(elt) .replaceAll("&", "&") .replaceAll("'", "''")); writer.write("')/*"); // make sure to return the root elt } else if (o instanceof org.jdom.Document) { org.jdom.Document doc = (org.jdom.Document) o; writer.write("xdmp:unquote('"); writer.write( new org.jdom.output.XMLOutputter() .outputString(doc) .replaceAll("&", "&") .replaceAll("'", "''")); writer.write("')"); } else if (o instanceof org.jdom.Text) { org.jdom.Text text = (org.jdom.Text) o; writer.write("text {'"); writer.write(escapeSingleQuotes(text.getText())); writer.write("'}"); } else if (o instanceof org.jdom.Attribute) { // <fake xmlns:pref="http://uri.com" pref:attrname="attrvalue"/>/@*:attrname // <fake xmlns="http://uri.com" attrname="attrvalue"/>/@*:attrname org.jdom.Attribute attr = (org.jdom.Attribute) o; writer.write("<fake xmlns"); if ("".equals(attr.getNamespacePrefix())) { writer.write("=\""); } else { writer.write(":" + attr.getNamespacePrefix() + "=\""); } writer.write(attr.getNamespaceURI()); writer.write("\" "); writer.write(attr.getQualifiedName()); writer.write("=\""); writer.write(escapeSingleQuotes(attr.getValue())); writer.write("\"/>/@*:"); writer.write(attr.getName()); } else if (o instanceof org.jdom.Comment) { org.jdom.Comment com = (org.jdom.Comment) o; writer.write("comment {'"); writer.write(escapeSingleQuotes(com.getText())); writer.write("'}"); } else if (o instanceof org.jdom.ProcessingInstruction) { org.jdom.ProcessingInstruction pi = (org.jdom.ProcessingInstruction) o; writer.write("processing-instruction "); writer.write(pi.getTarget()); writer.write(" {'"); writer.write(escapeSingleQuotes(pi.getData())); writer.write("'}"); } else if (o instanceof QName) { QName q = (QName) o; writer.write("fn:expanded-QName('"); writer.write(escapeSingleQuotes(q.getNamespaceURI())); writer.write("','"); writer.write(q.getLocalPart()); writer.write("')"); } else { writer.write( "error('XQuery tried to retrieve unsupported type: " + o.getClass().getName() + "')"); } writer.flush(); }
private static void sendStringResponse(HttpServletResponse res, String s) throws IOException { res.setContentType("text/plain; charset=UTF-8"); Writer w = res.getWriter(); w.write(s); w.flush(); }
/** * Read a line from the <i>in</i> {@link InputStream}, and return the line (without any trailing * newlines). * * @param prompt the prompt to issue to the console, may be null. * @return a line that is read from the terminal, or null if there was null input (e.g., * <i>CTRL-D</i> was pressed). */ public String readLine(final String prompt, final Character mask) throws IOException { this.mask = mask; if (prompt != null) this.prompt = prompt; try { terminal.beforeReadLine(this, this.prompt, mask); if ((this.prompt != null) && (this.prompt.length() > 0)) { out.write(this.prompt); out.flush(); } // if the terminal is unsupported, just use plain-java reading if (!terminal.isSupported()) { return readLine(in); } while (true) { int[] next = readBinding(); if (next == null) { return null; } int c = next[0]; int code = next[1]; if (c == -1) { return null; } boolean success = true; switch (code) { case EXIT: // ctrl-d if (buf.buffer.length() == 0) { return null; } case COMPLETE: // tab success = complete(); break; case MOVE_TO_BEG: success = setCursorPosition(0); break; case KILL_LINE: // CTRL-K success = killLine(); break; case CLEAR_SCREEN: // CTRL-L success = clearScreen(); break; case KILL_LINE_PREV: // CTRL-U success = resetLine(); break; case NEWLINE: // enter moveToEnd(); printNewline(); // output newline return finishBuffer(); case DELETE_PREV_CHAR: // backspace success = backspace(); break; case DELETE_NEXT_CHAR: // delete success = deleteCurrentCharacter(); break; case MOVE_TO_END: success = moveToEnd(); break; case PREV_CHAR: success = moveCursor(-1) != 0; break; case NEXT_CHAR: success = moveCursor(1) != 0; break; case NEXT_HISTORY: success = moveHistory(true); break; case PREV_HISTORY: success = moveHistory(false); break; case REDISPLAY: break; case PASTE: success = paste(); break; case DELETE_PREV_WORD: success = deletePreviousWord(); break; case PREV_WORD: success = previousWord(); break; case NEXT_WORD: success = nextWord(); break; case START_OF_HISTORY: success = history.moveToFirstEntry(); if (success) setBuffer(history.current()); break; case END_OF_HISTORY: success = history.moveToLastEntry(); if (success) setBuffer(history.current()); break; case CLEAR_LINE: moveInternal(-(buf.buffer.length())); killLine(); break; case INSERT: buf.setOvertyping(!buf.isOvertyping()); break; case UNKNOWN: default: if (c != 0) { // ignore null chars ActionListener action = (ActionListener) triggeredActions.get(new Character((char) c)); if (action != null) action.actionPerformed(null); else putChar(c, true); } else success = false; } if (!(success)) { beep(); } flushConsole(); } } finally { terminal.afterReadLine(this, this.prompt, mask); } }
/** * Flush the console output stream. This is important for printout out single characters (like a * backspace or keyboard) that we want the console to handle immedately. */ public final void flushConsole() throws IOException { out.flush(); }
/** * Dumps an {@link FST} to a GraphViz's <code>dot</code> language description for visualization. * Example of use: * * <pre> * PrintStream ps = new PrintStream("out.dot"); * fst.toDot(ps); * ps.close(); * </pre> * * and then, from command line: * * <pre> * dot -Tpng -o out.png out.dot * </pre> * * <p>Note: larger FSTs (a few thousand nodes) won't even render, don't bother. * * @param sameRank If <code>true</code>, the resulting <code>dot</code> file will try to order * states in layers of breadth-first traversal. This may mess up arcs, but makes the output * FST's structure a bit clearer. * @param labelStates If <code>true</code> states will have labels equal to their offsets in their * binary format. Expands the graph considerably. * @see "http://www.graphviz.org/" */ public static <T> void toDot(FST<T> fst, Writer out, boolean sameRank, boolean labelStates) throws IOException { final String expandedNodeColor = "blue"; // This is the start arc in the automaton (from the epsilon state to the first state // with outgoing transitions. final FST.Arc<T> startArc = fst.getFirstArc(new FST.Arc<T>()); // A queue of transitions to consider for the next level. final List<FST.Arc<T>> thisLevelQueue = new ArrayList<FST.Arc<T>>(); // A queue of transitions to consider when processing the next level. final List<FST.Arc<T>> nextLevelQueue = new ArrayList<FST.Arc<T>>(); nextLevelQueue.add(startArc); // A list of states on the same level (for ranking). final List<Integer> sameLevelStates = new ArrayList<Integer>(); // A bitset of already seen states (target offset). final BitSet seen = new BitSet(); seen.set(startArc.target); // Shape for states. final String stateShape = "circle"; // Emit DOT prologue. out.write("digraph FST {\n"); out.write(" rankdir = LR; splines=true; concentrate=true; ordering=out; ranksep=2.5; \n"); if (!labelStates) { out.write(" node [shape=circle, width=.2, height=.2, style=filled]\n"); } emitDotState(out, "initial", "point", "white", ""); emitDotState( out, Integer.toString(startArc.target), stateShape, fst.isExpandedTarget(startArc) ? expandedNodeColor : null, ""); out.write(" initial -> " + startArc.target + "\n"); final T NO_OUTPUT = fst.outputs.getNoOutput(); int level = 0; while (!nextLevelQueue.isEmpty()) { // we could double buffer here, but it doesn't matter probably. thisLevelQueue.addAll(nextLevelQueue); nextLevelQueue.clear(); level++; out.write("\n // Transitions and states at level: " + level + "\n"); while (!thisLevelQueue.isEmpty()) { final FST.Arc<T> arc = thisLevelQueue.remove(thisLevelQueue.size() - 1); if (fst.targetHasArcs(arc)) { // scan all arcs final int node = arc.target; fst.readFirstTargetArc(arc, arc); while (true) { // Emit the unseen state and add it to the queue for the next level. if (arc.target >= 0 && !seen.get(arc.target)) { final boolean isExpanded = fst.isExpandedTarget(arc); emitDotState( out, Integer.toString(arc.target), stateShape, isExpanded ? expandedNodeColor : null, labelStates ? Integer.toString(arc.target) : ""); seen.set(arc.target); nextLevelQueue.add(new FST.Arc<T>().copyFrom(arc)); sameLevelStates.add(arc.target); } String outs; if (arc.output != NO_OUTPUT) { outs = "/" + fst.outputs.outputToString(arc.output); } else { outs = ""; } final String cl; if (arc.label == FST.END_LABEL) { cl = "~"; } else { cl = printableLabel(arc.label); } out.write(" " + node + " -> " + arc.target + " [label=\"" + cl + outs + "\"]\n"); // Break the loop if we're on the last arc of this state. if (arc.isLast()) { break; } fst.readNextArc(arc); } } } // Emit state ranking information. if (sameRank && sameLevelStates.size() > 1) { out.write(" {rank=same; "); for (int state : sameLevelStates) { out.write(state + "; "); } out.write(" }\n"); } sameLevelStates.clear(); } // Emit terminating state (always there anyway). out.write(" -1 [style=filled, color=black, shape=circle, label=\"\"]\n\n"); out.write(" {rank=sink; -1 }\n"); out.write("}\n"); out.flush(); }
public static void main(String arg[]) { Hashtable ignoreList = new Hashtable(); Class cl = null; Model model = null; System.out.println("Synchronizing forms with database..."); Db.init(); try { DatabaseMetaData meta = Db.getCon().getMetaData(); String[] types = {"TABLE"}; ResultSet rs = meta.getTables(null, null, "%", types); // read ignore.list ignoreList = AutogenerateUtil.readIgnoreList(); // prepare directory File fDir = new File("../../web/WEB-INF/views/crud_form"); if (!fDir.exists()) fDir.mkdir(); while (rs.next()) { // proper file name generationm String className = ""; String tableName = rs.getString("TABLE_NAME"); className = StringUtil.toProperClassName(tableName); // space allowed... // tableName = tableName.toUpperCase(); //If Oracle that need uppercase tablename. In MySQL // in Mac OS X (and probably Linux), it mustbe case sensitive // open table String sql = "select * from " + tableName; PreparedStatement pstmt = Db.getCon() .prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY); ResultSet resultSet = pstmt.executeQuery(); ResultSetMetaData metaColumn = resultSet.getMetaData(); int nColoumn = metaColumn.getColumnCount(); // get foreign keys,and stored it in hashtable ResultSet rsFk = meta.getImportedKeys(Db.getCon().getCatalog(), null, tableName); Hashtable hashFk = new Hashtable(); System.out.println("FK Infos for table " + tableName); while (rsFk.next()) { String pkTableName = rsFk.getString("PKTABLE_NAME"); String pkColumnName = rsFk.getString("PKCOLUMN_NAME"); String fkColumnName = rsFk.getString("FKCOLUMN_NAME"); int fkSequence = rsFk.getInt("KEY_SEQ"); System.out.println( tableName + "." + fkColumnName + " => " + pkTableName + "." + pkColumnName); hashFk.put(fkColumnName, pkColumnName); hashFk.put(fkColumnName + "_table", pkTableName); } rsFk.close(); // create form page System.out.println( "Creating form page for " + tableName + " from table + " + application.config.Database.DB + "." + tableName); fDir = new File("../../web/WEB-INF/views/" + tableName); if (!fDir.exists()) fDir.mkdir(); File f = new File("../../web/WEB-INF/views/" + tableName + "/form_" + tableName + ".jsp"); if (ignoreList.get("form_" + tableName + ".jsp") != null) { Logger.getLogger(GenerateForm.class.getName()) .log(Level.INFO, "Ignoring creation of form_" + tableName + ".jsp"); } else { Writer out = new FileWriter(f); out.write( "<%@ page contentType=\"text/html; charset=UTF-8\" language=\"java\" import=\"java.sql.*,recite18th.library.Db,application.config.Config,recite18th.library.Pagination\" %>"); out.write("<%@ taglib uri=\"http://java.sun.com/jsp/jstl/core\" prefix=\"c\" %>\n"); out.write("<%@ taglib uri=\"http://java.sun.com/jsp/jstl/functions\" prefix=\"fn\" %>\n"); // create model for this class, use in detecting its PK Field cl = Class.forName("application.models." + className + "Model"); model = (Model) cl.newInstance(); // iterate all columns resultSet.beforeFirst(); resultSet.next(); out.write( "<table border=\"1\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\" bordercolor=\"#E8EDFF\">\n"); out.write("<tr>\n"); out.write("<td>\n"); out.write( "<form action=\"<%=Config.base_url%>index/" + className + "/save\" method=\"post\" enctype=\"multipart/form-data\">\n"); // I hope it's // okay to // default it to // multipart data out.write("<table id=\"hor-zebra\" summary=\"Form " + className + "\">\n"); out.write("<thead>\n"); out.write("<tr>\n"); out.write("<th colspan=\"2\" class=\"odd\" scope=\"col\">Form " + className + " </th>\n"); out.write("</tr>\n"); out.write("</thead>\n"); out.write("<tbody>\n"); for (int i = 1; i <= nColoumn; i++) { String columnName = metaColumn.getColumnName(i); String dataType = metaColumn.getColumnClassName(i); out.write("<tr>\n"); // if(!columnName.equals(model.getPkFieldName())) // implementing the case of PK not // AutoIncrement // if(!metaColumn.isAutoIncrement(i)) // { // varying field input for type // foreign field, as chooser page view if (hashFk.get(columnName) != null) // TODO: what if PK is chooser also?? :) CUrrently I add it manually the // hidden_*Pk_nama* field { String fkTableName = hashFk.get(columnName + "_table") + ""; String fkColumnName = hashFk.get(columnName) + ""; String fkController = StringUtil.toProperClassName(fkTableName); out.write("<td>" + columnName + "</td>\n"); out.write("<td>\n"); out.write( "<input name=\"" + columnName + "\" type=\"hidden\" id=\"" + columnName + "\" value=\"${model." + columnName + "}\"/>\n"); out.write( "<input name=\"label_" + columnName + "\" readonly=\"true\" type=\"text\" id=\"label_" + columnName + "\" value=\"${model." + columnName + "}\"/>\n"); // TODO : translate I out.write( "<a href=\"<%=Config.base_url%>index/" + fkController + "/chooseView?height=220&width=700\" class=\"thickbox\">Pilih</a>"); out.write("</td>\n"); } else { // regular field input, not foreign key case if (!columnName.equals(model.getPkFieldName())) { out.write("<td>" + columnName + "</td>\n"); out.write("<td>\n"); // ENUM Column, displayed as HTML SELECT. May will only work for mysql only... Logger.getLogger(GenerateForm.class.getName()) .log(Level.INFO, columnName + " type is " + metaColumn.getColumnType(i)); if (metaColumn.getColumnType(i) == 1) { String enum_content[][] = Db.getDataSet( "SELECT SUBSTRING(COLUMN_TYPE,6,length(SUBSTRING(COLUMN_TYPE,6))-1) as enum_content " + " FROM information_schema.COLUMNS " + " WHERE TABLE_NAME='" + tableName + "' " + " AND COLUMN_NAME='" + columnName + "'"); if (enum_content.length > 0) { // Logger.getLogger(Model.class.getName()).log(Level.INFO, "Enum Content = " + // enum_content[0][0]); String enum_list[] = enum_content[0][0].split(","); out.write("<select name=\"" + columnName + "\" id=\"" + columnName + "\">\n"); for (int ienum_list = 0; ienum_list < enum_list.length; ienum_list++) out.write( "\t<option <c:if test=\"${model." + columnName + "=='" + enum_list[ienum_list].substring( 1, enum_list[ienum_list].length() - 1) + "'}\"> selected=\"selected\" </c:if> value=\"" + enum_list[ienum_list].substring( 1, enum_list[ienum_list].length() - 1) + "\">" + enum_list[ienum_list].substring( 1, enum_list[ienum_list].length() - 1) + "</option>\n"); out.write("</select>\n\n"); } else { // no enum content detected.. :) out.write( "<input name=\"" + columnName + "\" type=\"text\" id=\"" + columnName + "\" value=\"${model." + columnName + "}\"/>\n"); } } else if (metaColumn.getColumnType(i) == 91) { out.write( "<input name=\"" + columnName + "\" type=\"text\" id=\"" + columnName + "\" value=\"${model." + columnName + "}\"/>\n"); out.write("<script>\n"); out.write( " if(!isValidDate($('#" + columnName + "').val())) $('#" + columnName + "').val('1980-1-1');\n"); // TODO: default value out.write(" (function($){\n"); out.write(" $('#" + columnName + "').click(function() {\n"); out.write(" $('#" + columnName + "').DatePickerShow();\n"); out.write(" });\n"); out.write(" $('#" + columnName + "').DatePicker({\n"); out.write(" format:'Y-m-d',\n"); out.write(" date: $('#" + columnName + "').val(),\n"); out.write(" current: $('#" + columnName + "').val(),\n"); out.write(" starts: 1,\n"); out.write(" position: 'r',\n"); out.write(" onBeforeShow: function(){\n"); out.write( " $('#" + columnName + "').DatePickerSetDate($('#" + columnName + "').val(), true);\n"); out.write(" },\n"); out.write(" onChange: function(formated, dates){\n"); out.write(" $('#" + columnName + "').DatePickerHide();\n"); out.write(" $('#" + columnName + "').val(formated);\n"); out.write(" }\n"); out.write(" });\n"); out.write(" })(jQuery)\n"); out.write(" </script>\n"); } else { out.write( "<input name=\"" + columnName + "\" type=\"text\" id=\"" + columnName + "\" value=\"${model." + columnName + "}\"/>\n"); out.write("${" + columnName + "_error}\n"); // regular input field } } else { // PK case if (metaColumn.isAutoIncrement(i)) { out.write( "<input name=\"hidden_" + columnName + "\" type=\"hidden\" id=\"hidden_" + columnName + "\" value=\"${model." + columnName + "}\"/>\n"); } else { out.write("<td>" + columnName + "</td>\n"); out.write("<td>\n"); out.write( "<input name=\"" + columnName + "\" type=\"text\" id=\"" + columnName + "\" value=\"${model." + columnName + "}\"/>\n"); out.write("${" + columnName + "_error}\n"); out.write( "<input name=\"hidden_" + columnName + "\" type=\"hidden\" id=\"hidden_" + columnName + "\" value=\"${model." + columnName + "}\"/>\n"); } } out.write("</td>\n"); } out.write("</tr>\n"); } out.write("<tr class=\"odd\">\n"); out.write("<td> </td>\n"); out.write("<td><input type=\"submit\" name=\"Submit\" value=\"Simpan\">"); out.write( "<input name=\"Button\" type=\"button\" id=\"Submit\" value=\"Batal\" onClick=\"javascript:history.back(-1);\"></td>\n"); out.write("</tr>\n"); out.write("</tbody>\n"); out.write("</table>\n"); out.write("</form></td>\n"); out.write("</tr>\n"); out.write("</table>\n"); out.flush(); out.close(); } // create viewPage if (ignoreList.get("view_" + tableName + ".jsp") != null) { Logger.getLogger(GenerateForm.class.getName()) .log(Level.INFO, "Ignoring creation of view_" + tableName + ".jsp"); } else { System.out.println("Creating view page " + tableName); fDir = new File("../../web/WEB-INF/views/" + tableName); if (!fDir.exists()) fDir.mkdir(); File fView = new File("../../web/WEB-INF/views/" + tableName + "/view_" + tableName + ".jsp"); Writer outView = new FileWriter(fView); outView.write( "<%@ page contentType=\"text/html; charset=UTF-8\" language=\"java\" import=\"java.sql.*,recite18th.library.Db,application.config.Config,recite18th.library.Pagination\" %>"); outView.write("<%@ taglib uri=\"http://java.sun.com/jsp/jstl/core\" prefix=\"c\" %>\n"); outView.write( "<%@ taglib uri=\"http://java.sun.com/jsp/jstl/functions\" prefix=\"fn\" %>\n"); outView.write("<% int pagenum = 0; %>\n"); // outView.write("<%@ include file=\"/WEB-INF/views/header.jsp\" %>"); outView.write( "<a href=\"<%=Config.base_url%>index/" + className + "/input/-1\">Tambah Data</a>\n"); outView.write( "|| <a href=\"<%=Config.base_url%>index/" + className + "/print\">Cetak</a>\n"); outView.write("<table width=\"100%\" id=\"rounded-corner\">\n"); outView.write("<thead>\n"); // iterate all columns : table header outView.write(" <tr>\n"); outView.write(" <th scope=\"col\" class=\"rounded-company\">No.</th>\n"); resultSet.beforeFirst(); resultSet.next(); // get Primary Key Field Name : often use String pkFieldName = ""; try { Class params[] = null; Method objMethod = cl.getMethod("getPkFieldName", params); pkFieldName = "" + objMethod.invoke(model); } catch (Exception ex) { Logger.getLogger(Model.class.getName()).log(Level.SEVERE, null, ex); } // ALL Lower Case pkFieldName = pkFieldName.toLowerCase(); // customize column view page for (int i = 1; i <= nColoumn; i++) { String columnName = metaColumn.getColumnName(i).toLowerCase(); // Caution : ALL LowerCase String dataType = metaColumn.getColumnClassName(i); String thClass = "rounded-q1"; String thTitle = StringUtil.toProperFieldTitle(columnName); if (TableCustomization.getTable(tableName) != null) // there is customization for this table { if (TableCustomization.getTable(tableName).get(columnName) != null) { thTitle = TableCustomization.getTable(tableName).get(columnName) + ""; outView.write( " <th scope=\"col\" class=\"" + thClass + "\">" + thTitle + "</th>\n"); } } else { // standard view for this table : hide PK, because mostly is auto increment if (!metaColumn.isAutoIncrement(i)) outView.write( " <th scope=\"col\" class=\"" + thClass + "\">" + thTitle + "</th>\n"); } } outView.write(" <th scope=\"col\" class=\"rounded-q4\">Aksi</th>\n"); outView.write(" </tr>\n"); outView.write("</thead>\n"); outView.write("<tfoot>\n"); outView.write(" <tr>\n"); outView.write( " <td colspan=\"" + (nColoumn + 1) + "\" class=\"rounded-foot-left\"><%=Pagination.createLinks(pagenum)%></td>\n"); outView.write(" <td class=\"rounded-foot-right\"> </td>\n"); outView.write(" </tr>\n"); outView.write("</tfoot>\n"); outView.write("<tbody>\n"); outView.write(" <c:forEach items=\"${row}\" var=\"item\" varStatus=\"status\" >\n"); outView.write(" <tr>\n"); outView.write(" <td>${status.count}</td>\n"); // iterate all columns : table data resultSet.beforeFirst(); resultSet.next(); for (int i = 1; i <= nColoumn; i++) { String columnName = metaColumn.getColumnName(i); // if(!columnName.equals(pkFieldName)) //TOFIX : currently, PK Field is not shown if (TableCustomization.getTable(tableName) != null) { if (TableCustomization.getTable(tableName).get(columnName) != null) { outView.write(" <td>${item." + columnName + "}</td>\n"); } } else { if (!metaColumn.isAutoIncrement(i)) outView.write(" <td>${item." + columnName + "}</td>\n"); } } outView.write(" <td>\n"); outView.write( " <a href=\"<%=Config.base_url%>index/" + className + "/input/${item." + pkFieldName + "}\">Ubah</a>\n"); outView.write( " <a href=\"<%=Config.base_url%>index/" + className + "/delete/${item." + pkFieldName + "}\" onClick=\"return confirm('Apakah Anda yakin?');\">Hapus</a>\n"); outView.write(" </td>\n"); outView.write(" </tr>\n"); outView.write(" </c:forEach>\n"); outView.write("</tbody>\n"); outView.write("</table>\n"); // outView.write("<%@ include file=\"/WEB-INF/views/footer.jsp\" %>"); outView.flush(); outView.close(); } } } catch (Exception e) { e.printStackTrace(); } }
/** Flushes the output stream to the process. */ public synchronized void flush() throws IOException { out.flush(); }
public void flush() throws IOException { wrappedWriter.flush(); }
public Collection<IStoredJob> listStoredJobs( final IStoredJobsQuery iStoredJobsQuery, final OutputStream output, final JobDefinitionFileFormat fformat) throws CentralDispatcherException { final HashMap<String, String> params = new HashMap<String, String>(); final String nameMatch = iStoredJobsQuery.getNameMatch(); String groupMatch = iStoredJobsQuery.getGroupMatch(); final String projectFilter = iStoredJobsQuery.getProjectFilter(); final String idlistFilter = iStoredJobsQuery.getIdlist(); final String expectedContentType; if (null != fformat) { params.put("format", fformat.getName()); expectedContentType = fformat == JobDefinitionFileFormat.xml ? "text/xml" : "text/yaml"; } else { params.put("format", JobDefinitionFileFormat.xml.getName()); expectedContentType = "text/xml"; } if (null != nameMatch) { params.put("jobFilter", nameMatch); } if (null != groupMatch) { final Matcher matcher = Pattern.compile("^/*(.+?)/*$").matcher(groupMatch); if (matcher.matches()) { // strip leading and trailing slashes groupMatch = matcher.group(1); } params.put("groupPath", groupMatch); } if (null != projectFilter) { params.put("project", projectFilter); } if (null != idlistFilter) { params.put("idlist", idlistFilter); } // 2. send request via ServerService final WebserviceResponse response; try { response = serverService.makeRundeckRequest( RUNDECK_API_JOBS_EXPORT_PATH, params, null, null, expectedContentType); } catch (MalformedURLException e) { throw new CentralDispatcherServerRequestException("Failed to make request", e); } checkErrorResponse(response); // if xml, do local validation and listing if (null == fformat || fformat == JobDefinitionFileFormat.xml) { validateJobsResponse(response); //////////////////// // parse result list of queued items, return the collection of QueuedItems /////////////////// final Document resultDoc = response.getResultDoc(); final Node node = resultDoc.selectSingleNode("/joblist"); final ArrayList<IStoredJob> list = new ArrayList<IStoredJob>(); if (null == node) { return list; } final List items = node.selectNodes("job"); if (null != items && items.size() > 0) { for (final Object o : items) { final Node node1 = (Node) o; final Node uuid = node1.selectSingleNode("uuid"); final Node id1 = node1.selectSingleNode("id"); final String id = null != uuid ? uuid.getStringValue() : id1.getStringValue(); final String name = node1.selectSingleNode("name").getStringValue(); final String url = createJobURL(id); final Node gnode = node1.selectSingleNode("group"); final String group = null != gnode ? gnode.getStringValue() : null; final String description = node1.selectSingleNode("description").getStringValue(); list.add(StoredJobImpl.create(id, name, url, group, description, projectFilter)); } } if (null != output) { // write output doc to the outputstream final OutputFormat format = OutputFormat.createPrettyPrint(); try { final XMLWriter writer = new XMLWriter(output, format); writer.write(resultDoc); writer.flush(); } catch (IOException e) { throw new CentralDispatcherServerRequestException(e); } } return list; } else if (fformat == JobDefinitionFileFormat.yaml) { // do rought yaml parse final Collection<Map> mapCollection = validateJobsResponseYAML(response); final ArrayList<IStoredJob> list = new ArrayList<IStoredJob>(); if (null == mapCollection || mapCollection.size() < 1) { return list; } for (final Map map : mapCollection) { final Object uuidobj = map.get("uuid"); final Object idobj = map.get("id"); final String id = null != uuidobj ? uuidobj.toString() : idobj.toString(); final String name = (String) map.get("name"); final String group = map.containsKey("group") ? (String) map.get("group") : null; final String desc = map.containsKey("description") ? (String) map.get("description") : ""; final String url = createJobURL(id); list.add(StoredJobImpl.create(id, name, url, group, desc, projectFilter)); } if (null != output) { // write output doc to the outputstream try { final Writer writer = new OutputStreamWriter(output); writer.write(response.getResults()); writer.flush(); } catch (IOException e) { throw new CentralDispatcherServerRequestException(e); } } return list; } return null; }