/** * @return the current encoding in IANA form, or null for default (UTF8) */ public String getEncoding() { Charset encodingCharset = FileEncodingQuery.getEncoding(fobj); return (encodingCharset == null ? null : encodingCharset.name()); }
public Reader getReader(Object object, String encoding) throws IOException { Wrap w = (Wrap)object; if (w.reader == null) { Charset chset = FileEncodingQuery.getEncoding(w.fo); w.reader = new InputStreamReader(w.fo.getInputStream(), chset); } return w.reader; }
private Charset encoding() { Charset e = encoding; if (e == null) { FileObject file = FileUtil.toFileObject(root); if (file != null) { e = FileEncodingQuery.getEncoding(file); } else { // avoid further checks e = UNKNOWN_CHARSET; } encoding = e; } return e != UNKNOWN_CHARSET ? e : null; }
protected static Reader createReader(FileObject fileObject) throws FileNotFoundException { return new BufferedReader(new InputStreamReader(fileObject.getInputStream(), FileEncodingQuery.getEncoding(fileObject))); }
public MergeResultWriterInfo(File tempf1, File tempf2, File tempf3, File outputFile, String mimeType, String leftFileRevision, String rightFileRevision, FileObject fo, FileLock lock, Charset encoding) { this.tempf1 = tempf1; this.tempf2 = tempf2; this.tempf3 = tempf3; this.outputFile = outputFile; this.mimeType = mimeType; this.leftFileRevision = leftFileRevision; this.rightFileRevision = rightFileRevision; this.fo = fo; this.lock = lock; if (encoding == null) { encoding = FileEncodingQuery.getEncoding(FileUtil.toFileObject(tempf1)); } this.encoding = encoding; }
@CheckForNull Charset getEncoding() { if (!encodingInitialized) { encoding = rootNotNeeded ? null : FileEncodingQuery.getEncoding(ctx.getRoot()); encodingInitialized = true; } return encoding; }
@Override public void run() throws IOException { String text; try { text = doc.getText(0, doc.getLength()); } catch (BadLocationException x) { throw new IOException(x); } FileObject fo = fParentFo.getFileObject(file.getName()); if (fo == null) { fo = fParentFo.createData(file.getName()); } OutputStream os = fo.getOutputStream(); try { os.write(text.getBytes(FileEncodingQuery.getEncoding(fo))); } finally { os.close(); } } });
private String read(VCSFileProxy file) throws IOException { FileObject fo = file.toFileObject(); Charset encoding = null; if (fo != null) { encoding = FileEncodingQuery.getEncoding(fo); } if (encoding == null) { encoding = Charset.forName("UTF-8"); //NOI18N } StringBuilder sb = new StringBuilder(); BufferedReader r = null; try { r = new BufferedReader(new InputStreamReader(file.getInputStream(false), encoding)); String s = r.readLine(); if (s != null) { while( true ) { sb.append(s); s = r.readLine(); if (s == null) break; sb.append('\n'); } } } finally { if (r != null) { r.close(); } } return sb.toString(); }
protected final Charset getEncoding() { return FileEncodingQuery.getEncoding(getDataObject().getPrimaryFile()); }
private String getEncoding() { FileObject fo = getFileObject(); Charset cs = null; if (fo != null && fo.isValid()) { cs = FileEncodingQuery.getEncoding(fo); } if (cs == null) { cs = FileEncodingQuery.getDefaultEncoding(); } return cs.name(); }
protected final String getEncoding() { FileObject fo = getFileObject(); Charset cs = null; if (fo != null && fo.isValid()) { cs = FileEncodingQuery.getEncoding(fo); } if (cs == null) { cs = FileEncodingQuery.getDefaultEncoding(); } return cs.name(); }
/** * Retrieves the Charset for the referenceFile and associates it weakly with * the given file. A following getAssociatedEncoding() call for the file * will then return the referenceFile-s Charset. * * @param referenceFile the file which charset has to be used when encoding * file * @param file file to be encoded with the referenceFile-s charset * */ public static void associateEncoding(VCSFileProxy referenceFile, VCSFileProxy file) { FileObject fo = referenceFile.toFileObject(); if (fo == null || fo.isFolder()) { return; } Charset c = FileEncodingQuery.getEncoding(fo); if (c == null) { return; } synchronized(ENCODING_LOCK) { if (fileToCharset == null) { fileToCharset = new WeakHashMap<>(); } fileToCharset.put(file, c); } }
@Override @NonNull public final Writer openWriter() throws IOException { final FileObject file = handle.resolveFileObject(true); if (file == null) { throw new IOException("Cannot create file: " + toString()); //NOI18N } return new OutputStreamWriter ( this.openOutputStream(), FileEncodingQuery.getEncoding(file)); }
public static void expandJSFTemplate(FileObject template, Map<String, Object> values, FileObject target) throws IOException { Charset targetEncoding = FileEncodingQuery.getEncoding(target); Writer w = new OutputStreamWriter(target.getOutputStream(), targetEncoding); try { expandJSFTemplate(template, values, targetEncoding, w); } finally { w.close(); } DataObject dob = DataObject.find(target); if (dob != null) { JSFPaletteUtilities.reformat(dob); } }
public static void expandJSFTemplate(FileObject template, Map<String, Object> values, Charset targetEncoding, Writer w) throws IOException { Charset sourceEnc = FileEncodingQuery.getEncoding(template); ScriptEngine eng = getScriptEngine(template); Bindings bind = eng.getContext().getBindings(ScriptContext.ENGINE_SCOPE); bind.putAll(values); bind.put(ENCODING_PROPERTY_NAME, targetEncoding.name()); Reader is = null; try { eng.getContext().setWriter(w); is = new InputStreamReader(template.getInputStream(), sourceEnc); eng.eval(is); } catch (ScriptException ex) { throw new IOException(ex); } finally { if (is != null) is.close(); } }
@Override public void run () { try { doc.remove(0,doc.getLength()); doc.insertString(0,new String( data, 0, pos, FileEncodingQuery.getEncoding(getHandle().resolveFileObject(false))), null); } catch (BadLocationException e) { if (LOG.isLoggable(Level.SEVERE)) LOG.log(Level.SEVERE, e.getMessage(), e); } } });
private Runnable getPostExecution(PhpExecutable executable) { // open in browser or editor? if (getRedirectToFile()) { File tmpFile = createTempFile(); if (tmpFile != null) { String charset = FileEncodingQuery.getEncoding(FileUtil.toFileObject(file)).name(); executable.fileOutput(tmpFile, charset, false); return new PostExecution(tmpFile); } } return null; }
/** * Creates a FileObject for newly created file * @param root owning the file * @param path the path (separated by '/') to target folder relative to root * @param name of the file * @return {@link JavaFileObject} */ public static @NonNull JavaFileObject templateFileObject (final @NonNull FileObject root, final @NonNull String path, final @NonNull String name) { assert root != null; assert path != null; JavaFileFilterImplementation filter = JavaFileFilterQuery.getFilter(root); Charset encoding = FileEncodingQuery.getEncoding(root); File rootFile = FileUtil.toFile(root); if (rootFile == null) { throw new IllegalArgumentException (); } File file = FileUtil.normalizeFile(new File (new File (rootFile, path.replace(NBFS_SEPARATOR_CHAR, File.separatorChar)), name)); return new NewFromTemplateFileObject (file, convertFolder2Package(path), name, filter, encoding); }
@Override protected void saveFromKitToStream(StyledDocument doc, EditorKit kit, OutputStream stream) throws IOException, BadLocationException { GuardedSectionsProvider guardedProvider = getGuardedSectionsProvider(doc, kit); if (guardedProvider != null) { Charset cs = FileEncodingQuery.getEncoding(this.getDataObject().getPrimaryFile()); Writer writer = guardedProvider.createGuardedWriter(stream, cs); try { kit.write(writer, doc, 0, doc.getLength()); } finally { writer.close(); } } else { super.saveFromKitToStream(doc, kit, stream); } }
private static String readFromTemplate(String pathToTemplate, Map<String, Object> values) throws IOException, ScriptException { FileObject template = FileUtil.getConfigFile(pathToTemplate); Charset sourceEnc = FileEncodingQuery.getEncoding(template); ScriptEngine eng = engine(template); Bindings bind = eng.getContext().getBindings(ScriptContext.ENGINE_SCOPE); bind.putAll(values); Reader is = null; try { eng.getContext().setAttribute(FileObject.class.getName(), template, ScriptContext.ENGINE_SCOPE); eng.getContext().setAttribute(ScriptEngine.FILENAME, template.getNameExt(), ScriptContext.ENGINE_SCOPE); eng.getContext().setAttribute(STRING_OUTPUT_MODE_ATTR, true, ScriptContext.ENGINE_SCOPE); is = new InputStreamReader(template.getInputStream(), sourceEnc); return (String)eng.eval(is); } finally { if (is != null) { is.close(); } } }