void newResB_actionPerformed(ActionEvent e) { AddResourceDialog dlg = new AddResourceDialog(App.getFrame(), Local.getString("New resource")); Dimension frmSize = App.getFrame().getSize(); Point loc = App.getFrame().getLocation(); dlg.setLocation( (frmSize.width - dlg.getSize().width) / 2 + loc.x, (frmSize.height - dlg.getSize().height) / 2 + loc.y); dlg.setVisible(true); if (dlg.CANCELLED) return; if (dlg.localFileRB.isSelected()) { String fpath = dlg.pathField.getText(); MimeType mt = MimeTypesList.getMimeTypeForFile(fpath); if (mt.getMimeTypeId().equals("__UNKNOWN")) { mt = addResourceType(fpath); if (mt == null) return; } if (!checkApp(mt)) return; // if file if projectFile, than copy the file and change url. if (dlg.projectFileCB.isSelected()) { fpath = copyFileToProjectDir(fpath); CurrentProject.getResourcesList().addResource(fpath, false, true); } else CurrentProject.getResourcesList().addResource(fpath); resourcesTable.tableChanged(); } else { if (!Util.checkBrowser()) return; CurrentProject.getResourcesList().addResource(dlg.urlField.getText(), true, false); resourcesTable.tableChanged(); } }
/** * Detect Unicode BOM in a source file. * * @param data Binary data of source file. * @param path The canonical path of source file. */ private void detectBOM(byte[] data, String path) { if (data.length > 2 && (byte) (data[0] ^ 0xEF) == 0 && (byte) (data[1] ^ 0xBB) == 0 && (byte) (data[2] ^ 0xBF) == 0) { App.exit("UTF8 BOM was found in " + path); } else if (data.length > 1 && (byte) (data[0] ^ 0xFE) == 0 && (byte) (data[1] ^ 0xFF) == 0) { App.exit("UTF16BE BOM was found in " + path); } else if (data.length > 1 && (byte) (data[0] ^ 0xFF) == 0 && (byte) (data[1] ^ 0xFE) == 0) { App.exit("UTF16LE BOM was found in " + path); } }
public static void main(String[] args) throws Exception { App app = new App(); byte[] res = app.get(); MessagePack msgpack = new MessagePack(); List<String> list = new ArrayList<String>(); ByteArrayInputStream in = new ByteArrayInputStream(res); Template<List<String>> listTemplate = tList(TString); Unpacker unpacker = msgpack.createUnpacker(in); list = unpacker.read(listTemplate); System.out.println(list.get(0)); }
public App spineForm(Context ctxt, boolean drop_annos, boolean spec, boolean expand_defs) { Expr h = head; Expr prev = null; if (expand_defs) { Expr prev2 = null; while (h != prev) { prev2 = prev; prev = h; h = h.defExpandOne(ctxt, drop_annos, spec); } if (prev2 != null) prev = prev2; if (h.construct != construct && h.construct != CONST && h.construct != VAR) /* we are trying to keep these constructs in the head. */ h = prev; } if (ctxt.getFlag("debug_spine_form")) { ctxt.w.println("Computing spine form of " + toString(ctxt)); ctxt.w.println("{"); ctxt.w.println("Head expands to " + h.toString(ctxt)); ctxt.w.flush(); } App ret = this; if (h.construct == construct) { TermApp e = (TermApp) ((TermApp) h).spineForm(ctxt, drop_annos, spec, expand_defs); int eXlen = e.X.length; int newlen = X.length + eXlen; Expr[] X2 = new Expr[newlen]; boolean[] specarg2 = new boolean[newlen]; for (int i = 0; i < eXlen; i++) { X2[i] = e.X[i]; specarg2[i] = e.specarg[i]; } for (int i = 0, iend = X.length; i < iend; i++) { X2[i + eXlen] = X[i]; specarg2[i + eXlen] = specarg[i]; } ret = new TermApp(e.head, X2, specarg2); } else if (h != head) ret = new TermApp(h, X, specarg); if (ctxt.getFlag("debug_spine_form")) { ret.print(ctxt.w, ctxt); ctxt.w.println("\n}"); ctxt.w.flush(); } return ret; }
public static void copyFile(File sourceFile, File destinationFile) { try { FileInputStream fileInputStream = null; FileOutputStream fileOutputStream = null; // Inner try block is for streams and readers that needs closing. try { fileInputStream = new FileInputStream(sourceFile); fileOutputStream = new FileOutputStream(destinationFile); byte[] buffer = new byte[1024]; int length = 0; while ((length = fileInputStream.read(buffer)) != -1) { fileOutputStream.write(buffer, 0, length); } } finally { if (fileInputStream != null) fileInputStream.close(); if (fileOutputStream != null) fileOutputStream.close(); } } catch (Throwable throwable) { App.e(FileUtilities.class, throwable); } }
private static void convertInsToCatInner( String baseDir, String fileName, BufferedWriter writer, Map<String, String> insToCat) throws IOException { BufferedReader reader = new BufferedReader(new FileReader(new File(baseDir + fileName))); int lineNumber = 0; int totalLineNumber = 0; String inputLine = null; System.out.println("Start reading: " + baseDir + fileName); while ((inputLine = reader.readLine()) != null) { // check progress if (lineNumber >= 500000) { totalLineNumber += lineNumber; lineNumber = 0; System.out.print(totalLineNumber + ", "); } lineNumber++; // ignore comment lines. if (inputLine.startsWith("#")) continue; // tokenize String[] strArr = inputLine.split(" "); String ins = App.removePrefix(strArr[0], "/resource/"); try { writer.write(insToCat.get(ins)); writer.newLine(); } catch (NullPointerException e) { continue; } } reader.close(); System.out.println("Done"); }
void removeResB_actionPerformed(ActionEvent e) { int[] toRemove = resourcesTable.getSelectedRows(); String msg = ""; if (toRemove.length == 1) msg = Local.getString("Remove the shortcut to resource") + "\n'" + resourcesTable.getModel().getValueAt(toRemove[0], 0) + "'"; else msg = Local.getString("Remove") + " " + toRemove.length + " " + Local.getString("shortcuts"); msg += "\n" + Local.getString("Are you sure?"); int n = JOptionPane.showConfirmDialog( App.getFrame(), msg, Local.getString("Remove resource"), JOptionPane.YES_NO_OPTION); if (n != JOptionPane.YES_OPTION) return; for (int i = 0; i < toRemove.length; i++) { CurrentProject.getResourcesList() .removeResource( ((Resource) resourcesTable.getModel().getValueAt(toRemove[i], ResourcesTable._RESOURCE)) .getPath()); } resourcesTable.tableChanged(); }
/** * Get dependencies of a source file. * * @param path The canonical path of source file. * @return Path of dependencies. */ private ArrayList<String> getDependencies(String path) { if (!dependenceMap.containsKey(path)) { ArrayList<String> dependencies = new ArrayList<String>(); Matcher m = PATTERN_REQUIRE.matcher(read(path, charset)); while (m.find()) { // Decide which root path to use. // Path wrapped in <> is related to root path. // Path wrapped in "" is related to parent folder of the source file. String root = null; if (m.group(1).equals("<")) { root = this.root; } else { root = new File(path).getParent(); } // Get path of required file. String required = m.group(2); File f = new File(root, required); if (f.exists()) { dependencies.add(canonize(f)); } else { App.exit("Cannot find required file " + required + " in " + path); } } dependenceMap.put(path, dependencies); } return dependenceMap.get(path); }
/* * (non-Javadoc) * * @see * org.apache.mina.core.service.IoHandlerAdapter#exceptionCaught(org.apache * .mina.core.session.IoSession, java.lang.Throwable) */ @Override public void exceptionCaught(IoSession session, Throwable cause) throws Exception { TConn d = (TConn) session.getAttribute("conn"); if (d != null && d.valid()) { App.bye(d); } }
MimeType addResourceType(String fpath) { ResourceTypeDialog dlg = new ResourceTypeDialog(App.getFrame(), Local.getString("Resource type")); Dimension dlgSize = new Dimension(420, 300); dlg.setSize(dlgSize); Dimension frmSize = App.getFrame().getSize(); Point loc = App.getFrame().getLocation(); dlg.setLocation( (frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y); dlg.ext = MimeTypesList.getExtension(fpath); dlg.setVisible(true); if (dlg.CANCELLED) return null; int ix = dlg.getTypesList().getSelectedIndex(); MimeType mt = (MimeType) MimeTypesList.getAllMimeTypes().toArray()[ix]; mt.addExtension(MimeTypesList.getExtension(fpath)); CurrentStorage.get().storeMimeTypesList(); return mt; }
boolean checkApp(MimeType mt) { String appId = mt.getAppId(); AppList appList = MimeTypesList.getAppList(); File d; if (appId == null) { appId = Util.generateId(); d = new File("/"); } else { File exe = new File(appList.getFindPath(appId) + "/" + appList.getExec(appId)); if (exe.isFile()) return true; d = new File(exe.getParent()); while (!d.exists()) d = new File(d.getParent()); } SetAppDialog dlg = new SetAppDialog( App.getFrame(), Local.getString( Local.getString("Select the application to open files of type") + " '" + mt.getLabel() + "'")); Dimension dlgSize = new Dimension(420, 300); dlg.setSize(dlgSize); Dimension frmSize = App.getFrame().getSize(); Point loc = App.getFrame().getLocation(); dlg.setLocation( (frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y); dlg.setDirectory(d); dlg.appPanel.argumentsField.setText("$1"); dlg.setVisible(true); if (dlg.CANCELLED) return false; File f = new File(dlg.appPanel.applicationField.getText()); appList.addOrReplaceApp( appId, f.getParent().replace('\\', '/'), f.getName().replace('\\', '/'), dlg.appPanel.argumentsField.getText()); mt.setApp(appId); /*appList.setFindPath(appId, chooser.getSelectedFile().getParent().replace('\\','/')); appList.setExec(appId, chooser.getSelectedFile().getName().replace('\\','/'));*/ CurrentStorage.get().storeMimeTypesList(); return true; }
public static Drawable getDrawableFromResource(Context context, int drawable) { try { return context.getResources().getDrawable(drawable); } catch (Exception exception) { App.e(FileUtilities.class, exception); } return null; }
public static Bitmap getBitmapFromResource(Context context, int drawable) { try { return BitmapFactory.decodeResource(context.getResources(), drawable); } catch (Exception exception) { App.e(FileUtilities.class, exception); } return null; }
public static Bitmap getBitmapFromFile(File imageFile) { try { return BitmapFactory.decodeFile(imageFile.getPath()); } catch (Exception exception) { App.e(FileUtilities.class, exception); } return null; }
public static Bitmap getBitmapFromStream(InputStream inputStream) { try { return BitmapFactory.decodeStream(inputStream); } catch (Exception exception) { App.e(FileUtilities.class, exception); } return null; }
/** * Get the canonical path of a file. * * @param f The file. * @return The canonical path. */ private String canonize(File f) { String path = null; try { path = f.getCanonicalPath(); } catch (IOException e) { App.exit(e); } return path; }
/** * Get text content of a source file. * * @param path The canonical path of source file. * @param charset Source file encoding. * @return Source file content. */ public String read(String path, String charset) { String str = null; byte[] bin = read(path); try { str = Charset.forName(charset).newDecoder().decode(ByteBuffer.wrap(bin)).toString(); } catch (CharacterCodingException e) { App.exit("Cannot read " + path + " as " + charset + " encoded file"); } return str; }
/** * Get binary data of a source file. * * @param path The canonical path of source file. * @return Source file data. */ public byte[] read(String path) { if (!binaryCache.containsKey(path)) { try { BufferedInputStream bf = new BufferedInputStream(new FileInputStream(new File(path))); try { byte[] data = new byte[bf.available()]; bf.read(data); detectBOM(data, path); binaryCache.put(path, data); } finally { bf.close(); } } catch (IOException e) { App.exit(e); } } return binaryCache.get(path); }
/** * Locate root path. * * @param root The user-specified root path. */ private void locateRoot(String root) { // Locate default root folder. if (root == null) { File pwd = new File(".").getAbsoluteFile(); File f = pwd; String[] l = null; // Detect intl-style/xxx/htdocs by finding "js" and "css" in sub folders. do { f = f.getParentFile(); if (f == null) { break; } l = f.list( new FilenameFilter() { private Pattern pattern = Pattern.compile("^(?:js|css)$"); public boolean accept(File dir, String name) { return pattern.matcher(name).matches(); } }); } while (l.length != 2); // If present, use intl-style/xxx/htdocs as root folder for Alibaba. if (f != null) { this.root = canonize(f); // Else use present working folder as root folder. } else { this.root = canonize(pwd); } // Use user-specified root folder. } else { File f = new File(root); if (f.exists()) { this.root = canonize(f); } else { App.exit("The user-specified root folder " + root + " does not exist."); } } }
public String runApp(String[] args, String input) throws Exception { // Save normal System.in and System.out InputStream sysin = System.in; PrintStream sysout = System.out; // Replace System.in with input text if (input == null) { input = ""; } System.setIn(new ByteArrayInputStream(input.getBytes("UTF-8"))); // Replace System.out with OutputStream we can capture ByteArrayOutputStream out = new ByteArrayOutputStream(); System.setOut(new PrintStream(out)); // Run the app with the List of arguments App.main(args); // Replace normal System.in and System.out System.setIn(sysin); System.setOut(sysout); // Return the captured output return out.toString("UTF-8").trim(); }
/** * Travel dependencies tree by DFS and Post-Order algorithm. * * @param tree The initial tree which contains root node only. * @param footprint The footprint of the traversal. * @param output Output queue of combined files. */ private void travel( Stack<ArrayList<String>> tree, Stack<String> footprint, ArrayList<String> output) { for (String node : tree.peek()) { // Detect circular dependences by looking back footprint. if (footprint.contains(node)) { String msg = "Circular dependences was found\n"; for (String path : footprint) { msg += " " + path + " ->\n"; } msg += " " + node; App.exit(msg); } // Skip visited node. if (output.contains(node)) { continue; } // Move forward. footprint.push(node); // Add sub nodes. tree.push(getDependencies(node)); // Travel sub nodes. travel(tree, footprint, output); // Clean visited nodes. tree.pop(); // Move backward. footprint.pop(); // Add first visited node to output queue. output.add(node); } }
public static void convertInsToCat(String baseDir, ArrayList<String> infoboxFileList) throws IOException { BufferedWriter writer; Map<String, String> insToCat; for (String fileName : infoboxFileList) { String[] strArr = fileName.split("/"); String output = "output/" + strArr[0] + "/" + strArr[2]; if (App.checkFile(output)) continue; writer = new BufferedWriter(new FileWriter(new File(output))); // get Map of instance to categories insToCat = Category.getInsToCat(strArr[0]); // convert convertInsToCatInner(baseDir, fileName, writer, insToCat); insToCat = null; writer.close(); System.out.println("File is created: " + output); System.out.println(); } }
public void do_print(java.io.PrintStream w, Context ctxt) { w.print("("); super.do_print(w, ctxt); w.print(")"); }
protected void print_arg(java.io.PrintStream w, Context ctxt, int i) { if (ctxt.getFlag("show_spec_args")) if (specarg[i]) w.print("spec "); super.print_arg(w, ctxt, i); }