Java Code Examples for org.netbeans.api.queries.FileEncodingQuery

The following examples show how to use org.netbeans.api.queries.FileEncodingQuery. These examples are extracted from open source projects. 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 Project: netbeans   Source File: JpaControllerUtil.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 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 2
Source Project: netbeans   Source File: JpaControllerUtil.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 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 3
Source Project: netbeans   Source File: BasicSourceFileObject.java    License: Apache License 2.0 6 votes vote down vote up
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 4
Source Project: netbeans   Source File: ModuleOraculum.java    License: Apache License 2.0 6 votes vote down vote up
@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 Project: netbeans   Source File: GuardedBlockTest.java    License: Apache License 2.0 6 votes vote down vote up
@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 6
Source Project: netbeans   Source File: BIEditorSupport.java    License: Apache License 2.0 6 votes vote down vote up
@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 7
Source Project: netbeans   Source File: BIEditorSupport.java    License: Apache License 2.0 6 votes vote down vote up
@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 8
Source Project: netbeans   Source File: FileEncodingQueryTest.java    License: Apache License 2.0 6 votes vote down vote up
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 9
Source Project: netbeans   Source File: SourceFileObject.java    License: Apache License 2.0 6 votes vote down vote up
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 10
Source Project: netbeans   Source File: FormEditorSupport.java    License: Apache License 2.0 6 votes vote down vote up
@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 11
Source Project: netbeans   Source File: MultiLineMappedMatcherBig.java    License: Apache License 2.0 6 votes vote down vote up
@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 12
Source Project: netbeans   Source File: MatchingObjectTest.java    License: Apache License 2.0 6 votes vote down vote up
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 13
Source Project: netbeans   Source File: Bug138973Test.java    License: Apache License 2.0 6 votes vote down vote up
@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 14
Source Project: netbeans   Source File: ScriptingCreateFromTemplateTest.java    License: Apache License 2.0 6 votes vote down vote up
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 15
Source Project: netbeans   Source File: DataEditorSupport.java    License: Apache License 2.0 6 votes vote down vote up
@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 Project: netbeans   Source File: CodeSniffer.java    License: Apache License 2.0 6 votes vote down vote up
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 17
Source Project: netbeans   Source File: ResolveConflictsExecutor.java    License: Apache License 2.0 6 votes vote down vote up
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 18
Source Project: netbeans   Source File: ResolveConflictsExecutor.java    License: Apache License 2.0 6 votes vote down vote up
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 19
Source Project: netbeans   Source File: Utils.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 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 20
Source Project: netbeans   Source File: XmlFileEncodingQueryImplTest.java    License: Apache License 2.0 6 votes vote down vote up
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 21
Source Project: netbeans   Source File: ScriptingCreateFromTemplateTest.java    License: Apache License 2.0 6 votes vote down vote up
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 22
Source Project: netbeans   Source File: ResolveConflictsExecutor.java    License: Apache License 2.0 6 votes vote down vote up
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 23
Source Project: jeddict   Source File: FileUtil.java    License: Apache License 2.0 6 votes vote down vote up
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 24
Source Project: netbeans   Source File: ModuleNames.java    License: Apache License 2.0 5 votes vote down vote up
@CheckForNull
public static String parseModuleName(
        @NonNull final FileObject moduleInfo) {
    final JavacTaskImpl jt = JavacParser.createJavacTask(
            new ClasspathInfo.Builder(ClassPath.EMPTY).build(),
            null,
            "1.3",  //min sl to prevent validateSourceLevel warning
            null,
            null,
            null,
            null,
            null,
            Collections.singletonList(FileObjects.fileObjectFileObject(
                moduleInfo,
                moduleInfo.getParent(),
                null,
                FileEncodingQuery.getEncoding(moduleInfo))));
        final CompilationUnitTree cu =  jt.parse().iterator().next();
        final List<? extends Tree> typeDecls = cu.getTypeDecls();
        if (!typeDecls.isEmpty()) {
            final Tree typeDecl = typeDecls.get(0);
            if (typeDecl.getKind() == Tree.Kind.MODULE) {
                return ((ModuleTree)typeDecl).getName().toString();
            }
        }
    return null;
}
 
Example 25
Source Project: netbeans   Source File: JavaParsingContext.java    License: Apache License 2.0 5 votes vote down vote up
@CheckForNull
Charset getEncoding() {
    if (!encodingInitialized) {
        encoding = rootNotNeeded ? null : FileEncodingQuery.getEncoding(ctx.getRoot());
        encodingInitialized = true;
    }
    return encoding;
}
 
Example 26
Source Project: netbeans   Source File: AbstractSourceFileObject.java    License: Apache License 2.0 5 votes vote down vote up
@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));
}
 
Example 27
Source Project: netbeans   Source File: FolderArchive.java    License: Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 28
Source Project: netbeans   Source File: GuardedBlockTest.java    License: Apache License 2.0 5 votes vote down vote up
@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 29
Source Project: netbeans   Source File: SourceFoldersPanel.java    License: Apache License 2.0 5 votes vote down vote up
private void encodingComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_encodingComboBoxActionPerformed
        Object selItem = encodingComboBox.getModel().getSelectedItem();
        if (selItem instanceof Charset) {
            model.setEncoding(((Charset) selItem).name());
            // XXX Should we set this here?
            FileEncodingQuery.setDefaultEncoding((Charset) selItem);
        } else {
            model.setEncoding(selItem.toString());
        }
}
 
Example 30
Source Project: netbeans   Source File: BatchSearch.java    License: Apache License 2.0 5 votes vote down vote up
public CharSequence getSourceCode() {
    try {
        FileObject file = getResolvedFile();
        ByteBuffer bb = ByteBuffer.wrap(file.asBytes());

        return FileEncodingQuery.getEncoding(file).decode(bb);
    } catch (IOException ex) {
        Exceptions.printStackTrace(ex);
        return null;
    }
}