org.netbeans.api.queries.FileEncodingQuery Java Examples
The following examples show how to use
org.netbeans.api.queries.FileEncodingQuery.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CodeSniffer.java From netbeans with Apache License 2.0 | 6 votes |
private List<String> getParameters(String standard, FileObject file, boolean noRecursion) { Charset encoding = FileEncodingQuery.getEncoding(file); List<String> params = new ArrayList<>(); // NETBEANS-3243 the path of Code Sniffer may have --standard parameter if (StringUtils.hasText(standard) && !codeSnifferPath.contains(STANDARD_PARAM + "=") // NOI18N && !codeSnifferPath.contains(STANDARD_PARAM + " ")) { // NOI18N // #270987 use --standard params.add(String.format(STANDARD_PARAM_FORMAT, standard)); } params.add(REPORT_PARAM); params.add(String.format(EXTENSIONS_PARAM, StringUtils.implode(FileUtil.getMIMETypeExtensions(FileUtils.PHP_MIME_TYPE), ","))); // NOI18N params.add(String.format(ENCODING_PARAM, encoding.name())); addIgnoredFiles(params, file); if (noRecursion) { params.add(NO_RECURSION_PARAM); } params.add(FileUtil.toFile(file).getAbsolutePath()); return params; }
Example #2
Source File: ScriptingCreateFromTemplateTest.java From netbeans with Apache License 2.0 | 6 votes |
public void testCreateFromTemplateEncodingProperty() throws Exception { FileObject root = FileUtil.createMemoryFileSystem().getRoot(); FileObject fo = FileUtil.createData(root, "simpleObject.txt"); OutputStream os = fo.getOutputStream(); os.write("${encoding}".getBytes()); os.close(); assertEquals("content/unknown", fo.getMIMEType()); fo.setAttribute ("template", Boolean.TRUE); assertEquals("content/unknown", fo.getMIMEType()); fo.setAttribute("javax.script.ScriptEngine", "freemarker"); assertEquals("text/x-freemarker", fo.getMIMEType()); DataObject obj = DataObject.find(fo); DataFolder folder = DataFolder.findFolder(FileUtil.createFolder(root, "target")); Map<String,String> parameters = Collections.emptyMap(); DataObject inst = obj.createFromTemplate(folder, "complex", parameters); FileObject instFO = inst.getPrimaryFile(); Charset targetEnc = FileEncodingQuery.getEncoding(instFO); assertNotNull("Template encoding is null", targetEnc); assertEquals("Encoding in template doesn't match", targetEnc.name(), instFO.asText()); }
Example #3
Source File: GuardedBlockTest.java From netbeans with Apache License 2.0 | 6 votes |
@Override protected void loadFromStreamToKit(StyledDocument doc, InputStream stream, EditorKit kit) throws IOException, BadLocationException { if (guardedEditor == null) { guardedEditor = new FormGEditor(); GuardedSectionsFactory gFactory = GuardedSectionsFactory.find("text/x-java"); if (gFactory != null) { guardedProvider = gFactory.create(guardedEditor); } } if (guardedProvider != null) { guardedEditor.doc = doc; Charset c = FileEncodingQuery.getEncoding(this.getDataObject().getPrimaryFile()); Reader reader = guardedProvider.createGuardedReader(stream, c); try { kit.read(reader, doc, 0); } finally { reader.close(); } } else { super.loadFromStreamToKit(doc, stream, kit); } }
Example #4
Source File: ModuleOraculum.java From netbeans with Apache License 2.0 | 6 votes |
@NonNull private static String parsePackage(FileObject file) { String pkg = ""; //NOI18N final JavacTaskImpl jt = JavacParser.createJavacTask( new ClasspathInfo.Builder(ClassPath.EMPTY).build(), null, null, null, null, null, null, null, Collections.singletonList(FileObjects.fileObjectFileObject( file, file.getParent(), null, FileEncodingQuery.getEncoding(file)))); final CompilationUnitTree cu = jt.parse().iterator().next(); pkg = Optional.ofNullable(cu.getPackage()) .map((pt) -> pt.getPackageName()) .map((xt) -> xt.toString()) .orElse(pkg); return pkg; }
Example #5
Source File: FileUtil.java From jeddict with Apache License 2.0 | 6 votes |
public static void expandTemplate(InputStream template, FileObject toFile, Map<String, Object> values) throws IOException { Charset targetEncoding = FileEncodingQuery.getEncoding(toFile); if (toFile.isLocked()) { LOG.log(Level.SEVERE, "File {0} is locked", new Object[]{toFile.getName()}); return; } FileLock lock = toFile.lock(); try (Writer writer = new OutputStreamWriter(toFile.getOutputStream(lock), targetEncoding)) { expandTemplate(new InputStreamReader(template), writer, values, targetEncoding); } finally { lock.releaseLock(); } DataObject dob = DataObject.find(toFile); if (dob != null) { reformat(dob); } }
Example #6
Source File: BasicSourceFileObject.java From netbeans with Apache License 2.0 | 6 votes |
public synchronized @Override void close() throws IOException { try { LineDocumentUtils.as(doc, AtomicLockDocument.class).runAtomic(new Runnable () { @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); } } }); } finally { resetCaches(); } }
Example #7
Source File: ResolveConflictsExecutor.java From netbeans with Apache License 2.0 | 6 votes |
public MergeResultWriterInfo(File tempf1, File tempf2, File tempf3, File outputFile, String mimeType, String leftFileRevision, String rightFileRevision, FileObject fo, FileLock lock, Charset encoding, String newLineString) { 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; this.newLineString = newLineString; if (encoding == null) { encoding = FileEncodingQuery.getEncoding(FileUtil.toFileObject(tempf1)); } this.encoding = encoding; }
Example #8
Source File: JpaControllerUtil.java From netbeans with Apache License 2.0 | 6 votes |
/** * Get the encoding of the supplied project as a String (by performing a lookup and invoking Charset.name). * @param project The project * @param file A file in the project * @return The project encoding, or a suitable default if the project encoding cannot be determined. Never null */ public static String getProjectEncodingAsString(Project project, FileObject file) { Charset encoding = project.getLookup().lookup(FileEncodingQueryImplementation.class).getEncoding(file); if (encoding == null) { encoding = FileEncodingQuery.getDefaultEncoding(); if (encoding == null) { return "UTF-8"; } else { return encoding.name(); } } else { return encoding.name(); } }
Example #9
Source File: XmlFileEncodingQueryImplTest.java From netbeans with Apache License 2.0 | 6 votes |
private void copyFile (final FileObject from, final FileObject to) throws IOException { final Charset ci = FileEncodingQuery.getEncoding(from); final Reader in = new BufferedReader (new InputStreamReader (from.getInputStream(),ci)); try { final FileLock lck = to.lock(); try { final Charset co = FileEncodingQuery.getEncoding(to); final Writer out = new BufferedWriter (new OutputStreamWriter (to.getOutputStream(lck),co)); try { final char[] data = new char[1024]; int len; while ((len=in.read(data, 0, data.length))>0) { out.write(data, 0, len); } } finally { out.close(); } } finally { lck.releaseLock(); } } finally { in.close(); } }
Example #10
Source File: BIEditorSupport.java From netbeans with Apache License 2.0 | 6 votes |
@Override protected void loadFromStreamToKit(StyledDocument doc, InputStream stream, EditorKit kit) throws IOException, BadLocationException { if (guardedEditor == null) { guardedEditor = new BIGES(); GuardedSectionsFactory gFactory = GuardedSectionsFactory.find(((DataEditorSupport.Env) env).getMimeType()); if (gFactory != null) { guardedProvider = gFactory.create(guardedEditor); } } if (guardedProvider != null) { guardedEditor.doc = doc; Charset c = FileEncodingQuery.getEncoding(this.getDataObject().getPrimaryFile()); Reader reader = guardedProvider.createGuardedReader(stream, c); try { kit.read(reader, doc, 0); } finally { reader.close(); } } else { super.loadFromStreamToKit(doc, stream, kit); } }
Example #11
Source File: BIEditorSupport.java From netbeans with Apache License 2.0 | 6 votes |
@Override protected void saveFromKitToStream(StyledDocument doc, EditorKit kit, OutputStream stream) throws IOException, BadLocationException { if (guardedProvider != null) { Charset c = FileEncodingQuery.getEncoding(this.getDataObject().getPrimaryFile()); Writer writer = guardedProvider.createGuardedWriter(stream, c); try { kit.write(writer, doc, 0, doc.getLength()); } finally { writer.close(); } } else { super.saveFromKitToStream(doc, kit, stream); } }
Example #12
Source File: FileEncodingQueryTest.java From netbeans with Apache License 2.0 | 6 votes |
public void testFileEncodingQuery () throws Exception { final Charset UTF8 = Charset.forName("UTF-8"); final Charset ISO15 = Charset.forName("ISO-8859-15"); final Charset CP1252 = Charset.forName("CP1252"); FileObject java = sources.createData("a.java"); Charset enc = FileEncodingQuery.getEncoding(java); assertEquals(UTF8,enc); FileObject xml = sources.createData("b.xml"); enc = FileEncodingQuery.getEncoding(xml); assertEquals(ISO15,enc); ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction<Void>() { public Void run() throws Exception { EditableProperties ep = helper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH); ep.setProperty(ProjectProperties.SOURCE_ENCODING, CP1252.name()); helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, ep); ProjectManager.getDefault().saveProject(prj); return null; } }); enc = FileEncodingQuery.getEncoding(java); assertEquals(CP1252,enc); FileObject standAloneJava = scratch.createData("b.java"); enc = FileEncodingQuery.getEncoding(standAloneJava); assertEquals(Charset.defaultCharset(), enc); }
Example #13
Source File: FormEditorSupport.java From netbeans with Apache License 2.0 | 6 votes |
@Override protected void loadFromStreamToKit(StyledDocument doc, InputStream stream, EditorKit kit) throws IOException, BadLocationException { if (guardedEditor == null) { guardedEditor = new FormGEditor(); GuardedSectionsFactory gFactory = GuardedSectionsFactory.find(((DataEditorSupport.Env) env).getMimeType()); if (gFactory != null) { guardedProvider = gFactory.create(guardedEditor); } } if (guardedProvider != null) { guardedEditor.doc = doc; Charset c = FileEncodingQuery.getEncoding(this.getDataObject().getPrimaryFile()); Reader reader = guardedProvider.createGuardedReader(stream, c); try { kit.read(reader, doc, 0); } finally { reader.close(); } } else { super.loadFromStreamToKit(doc, stream, kit); } }
Example #14
Source File: JpaControllerUtil.java From netbeans with Apache License 2.0 | 6 votes |
/** * Get the encoding of the supplied project as a Charset object * @param project The project * @param file A file in the project * @return The project encoding, or a suitable default if the project encoding cannot be determined. Never null */ public static Charset getProjectEncoding(Project project, FileObject file) { Charset encoding = project.getLookup().lookup(FileEncodingQueryImplementation.class).getEncoding(file); if (encoding == null) { encoding = FileEncodingQuery.getDefaultEncoding(); if (encoding == null) { return Charset.forName("UTF-8"); } else { return encoding; } } else { return encoding; } }
Example #15
Source File: DataEditorSupport.java From netbeans with Apache License 2.0 | 6 votes |
@Override public StyledDocument openDocument() throws IOException { DataObject tmpObj = getDataObject(); Charset c = charsets.get(tmpObj); if (c == null) { c = FileEncodingQuery.getEncoding(tmpObj.getPrimaryFile()); } try { charsets.put(tmpObj, c); incrementCacheCounter(tmpObj); return super.openDocument(); } finally { if (decrementCacheCounter(tmpObj) == 0) { charsets.remove(tmpObj); } ERR.finest("openDocument - charset removed"); } }
Example #16
Source File: ScriptingCreateFromTemplateTest.java From netbeans with Apache License 2.0 | 6 votes |
public void testCreateFromTemplateEncodingProperty() throws Exception { FileObject root = FileUtil.createMemoryFileSystem().getRoot(); FileObject fo = FileUtil.createData(root, "simpleObject.txt"); OutputStream os = fo.getOutputStream(); os.write("print(encoding)".getBytes()); os.close(); assertEquals("content/unknown", fo.getMIMEType()); fo.setAttribute ("template", Boolean.TRUE); assertEquals("content/unknown", fo.getMIMEType()); fo.setAttribute(ScriptingCreateFromTemplateHandler.SCRIPT_ENGINE_ATTR, "js"); DataObject obj = DataObject.find(fo); DataFolder folder = DataFolder.findFolder(FileUtil.createFolder(root, "target")); Map<String,String> parameters = Collections.emptyMap(); DataObject inst = obj.createFromTemplate(folder, "complex", parameters); FileObject instFO = inst.getPrimaryFile(); Charset targetEnc = FileEncodingQuery.getEncoding(instFO); assertNotNull("Template encoding is null", targetEnc); String instText = stripNewLines(instFO.asText()); assertEquals("Encoding in template doesn't match", targetEnc.name(), instText); }
Example #17
Source File: Bug138973Test.java From netbeans with Apache License 2.0 | 6 votes |
@Override public FileObject createFromTemplate(FileObject template, FileObject targetFolder, String name, Map<String, Object> parameters) throws IOException { String nameUniq = FileUtil.findFreeFileName(targetFolder, name, template.getExt()); FileObject newFile = FileUtil.createData(targetFolder, nameUniq + '.' + template.getExt()); Charset templateEnc = FileEncodingQuery.getEncoding(template); Charset newFileEnc = FileEncodingQuery.getEncoding(newFile); InputStream is = template.getInputStream(); Reader reader = new BufferedReader(new InputStreamReader(is, templateEnc)); OutputStream os = newFile.getOutputStream(); Writer writer = new BufferedWriter(new OutputStreamWriter(os, newFileEnc)); int cInt; while ((cInt = reader.read()) != -1) { writer.write(cInt); } writer.close(); reader.close(); return newFile; }
Example #18
Source File: MatchingObjectTest.java From netbeans with Apache License 2.0 | 6 votes |
public void testRead() throws IOException { MockServices.setServices(Utf8FileEncodingQueryImpl.class); FileObject dir = FileUtil.toFileObject(getDataDir()); assertNotNull(dir); FileObject file = dir.getFileObject( "textFiles/utf8file.txt"); Charset c = FileEncodingQuery.getEncoding(file); MatchingObject mo = new MatchingObject(new ResultModel( new BasicSearchCriteria(), "", null), file, c, null); StringBuilder text = mo.text(true); String textStr = text.toString(); int lineBreakSize = textStr.charAt(textStr.length() - 1) == '\n' && textStr.charAt(textStr.length() - 2) == '\r' ? 2 : 1; assertEquals('.', textStr.charAt(textStr.length() - lineBreakSize - 1)); }
Example #19
Source File: MultiLineMappedMatcherBig.java From netbeans with Apache License 2.0 | 6 votes |
@Override protected Def checkMeasuredInternal(FileObject file, SearchListener listener) { Charset charset = FileEncodingQuery.getEncoding(file); LongCharSequence longSequence = null; try { File f = FileUtil.toFile(file); longSequence = new LongCharSequence(f, charset); List<TextDetail> textDetails = matchWholeFile(longSequence, file); if (textDetails == null) { return null; } else { Def def = new Def(file, charset, textDetails); return def; } } catch (Exception ex) { listener.generalError(ex); return null; } finally { if (longSequence != null) { longSequence.close(); } } }
Example #20
Source File: ResolveConflictsExecutor.java From netbeans with Apache License 2.0 | 6 votes |
public MergeResultWriterInfo(File tempf1, File tempf2, File tempf3, File outputFile, String mimeType, String leftFileRevision, String rightFileRevision, FileObject fo, FileLock lock, Charset encoding, String newLineString) { 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; this.newLineString = newLineString; if (encoding == null) { encoding = FileEncodingQuery.getEncoding(FileUtil.toFileObject(tempf1)); } this.encoding = encoding; }
Example #21
Source File: ResolveConflictsExecutor.java From netbeans with Apache License 2.0 | 6 votes |
public MergeResultWriterInfo(File tempf1, File tempf2, File tempf3, File outputFile, String mimeType, String leftFileRevision, String rightFileRevision, FileObject fo, FileLock lock, Charset encoding, String newLineString) { 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; this.newLineString = newLineString; if (encoding == null) { encoding = FileEncodingQuery.getEncoding(FileUtil.toFileObject(tempf1)); } this.encoding = encoding; }
Example #22
Source File: Utils.java From netbeans with Apache License 2.0 | 6 votes |
/** * 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(File referenceFile, File file) { FileObject refFO = FileUtil.toFileObject(referenceFile); if (refFO == null || refFO.isFolder()) { return; } FileObject fo = FileUtil.toFileObject(file); if (fo == null || fo.isFolder()) { return; } Charset c = FileEncodingQuery.getEncoding(refFO); if (c != null) { synchronized(ENCODING_LOCK) { if (fileToFileObject == null) { fileToFileObject = new WeakHashMap<File, FileObject>(); } fileToFileObject.put(file, fo); } associateEncoding(fo, c); } }
Example #23
Source File: SourceFileObject.java From netbeans with Apache License 2.0 | 6 votes |
public synchronized @Override void close() throws IOException { try { NbDocument.runAtomic(this.doc, new Runnable () { @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); } } }); } finally { resetCaches(); } }
Example #24
Source File: JSFPaletteUtilities.java From netbeans with Apache License 2.0 | 5 votes |
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); } }
Example #25
Source File: FileRunner.java From netbeans with Apache License 2.0 | 5 votes |
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; }
Example #26
Source File: ConfigureProjectPanel.java From netbeans with Apache License 2.0 | 5 votes |
private Charset getEncoding() { Charset enc = (Charset) descriptor.getProperty(ENCODING); if (enc == null) { // #136917 enc = FileEncodingQuery.getDefaultEncoding(); } return enc; }
Example #27
Source File: EncodingTest.java From netbeans with Apache License 2.0 | 5 votes |
private void read(final FileObject data) throws IOException { final Charset cs = FileEncodingQuery.getEncoding(data); final BufferedReader in = new BufferedReader(new InputStreamReader(data.getInputStream(), cs)); try { CharBuffer buffer = CharBuffer.allocate(1024); while (in.read(buffer)>0) { buffer.clear(); } } finally { in.close(); } }
Example #28
Source File: TplEditorSupport.java From netbeans with Apache License 2.0 | 5 votes |
@Override @NbBundle.Messages({ "# {0} document name", "# {1} encoding", "# {2} alternative encoding", "MSG_unsupportedEncodingLoad=<html>The encoding {1} specified in meta tag of the document {0} is invalid.<br>Do you want to load the file using <b>{2}</b> encoding?</html>"}) public void open() { String encoding = ((TplDataObject) getDataObject()).getFileEncoding(); String feqEncoding = FileEncodingQuery.getEncoding(getDataObject().getPrimaryFile()).name(); if (encoding != null && !isSupportedEncoding(encoding)) { Integer showEncodingWarnings = getTplDO().getShowEncodingWarnings(); if (showEncodingWarnings == null) { String message = MSG_unsupportedEncodingLoad(getDataObject().getPrimaryFile().getNameExt(), encoding, feqEncoding); SaveConfirmationPanel panel = new SaveConfirmationPanel(message); DialogDescriptor dd = new DialogDescriptor(panel, Bundle.warning(), true, DialogDescriptor.YES_NO_OPTION, DialogDescriptor.YES_OPTION, null); DialogDisplayer.getDefault().notify(dd); showEncodingWarnings = (Integer) dd.getValue(); if (panel.isDoNotShowAgainCheckBox() && showEncodingWarnings == NotifyDescriptor.YES_OPTION) { getTplDO().setShowEncodingWarnings(showEncodingWarnings); } } if (!showEncodingWarnings.equals(NotifyDescriptor.YES_OPTION)) { return; // do not open the file } } super.open(); }
Example #29
Source File: ProjectTemplateAttributesProviderTest.java From netbeans with Apache License 2.0 | 5 votes |
@Override protected void setUp() throws Exception { super.setUp(); scratch = TestUtil.makeScratchDir(this); folder = scratch.createFolder("folder"); projdir = scratch.createFolder("proj"); createProject(projdir); MockLookup.setInstances(new FEQImpl(), new TestProjectFactory()); assertEquals(FEQImpl.ENCODING, FileEncodingQuery.getEncoding(folder).name()); }
Example #30
Source File: JFXApplicationClassChooserTest.java From netbeans with Apache License 2.0 | 5 votes |
private FileObject createApplication(String name) throws IOException { int index = name.lastIndexOf('.'); final String pkg; final String sn; if (index >= 0) { pkg = name.substring(0, index); sn = name.substring(index+1); } else { pkg = null; sn = name; } final FileObject file = FileUtil.createData(srcRoot, name.replace('.', '/') + ".java"); //NOI18N final FileLock lock = file.lock(); try (final PrintWriter out = new PrintWriter(new OutputStreamWriter( file.getOutputStream(lock), FileEncodingQuery.getEncoding(file)))) { out.println( pkg == null ? "" :"package " + pkg +";\n" + //NOI18N "import javafx.application.Application;\n" + //NOI18N "import javafx.stage.Stage;\n" + //NOI18N "public class " + sn + " extends Application {\n" + //NOI18N " public void start(Stage primaryStage) {}\n"+ //NOI18N "}" //NOI18N ); } finally { lock.releaseLock(); } return file; }