blob: 6e912ed31fc2da98f4edf6e4005c8bca40a60d70 [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.netbeans.modules.xml.schema.completion;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import org.netbeans.editor.BaseDocument;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
/**
*
* @author Samaresh
*/
public class Util {
private Util() {
}
public static FileObject getResourceAsFileObject(String path) throws Exception {
URL url = Util.class.getResource(path);
File file = new File(url.toURI());
file = FileUtil.normalizeFile(file);
FileObject fileObj = FileUtil.toFileObject(file);
return fileObj;
}
public static BaseDocument getResourceAsDocument(String path) throws Exception {
InputStream in = Util.class.getResourceAsStream(path);
BaseDocument sd = new BaseDocument(true, "text/xml"); //NOI18N
BufferedReader br = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8));
StringBuffer sbuf = new StringBuffer();
try {
String line = null;
while ((line = br.readLine()) != null) {
sbuf.append(line);
sbuf.append(System.getProperty("line.separator"));
}
} finally {
br.close();
}
sd.insertString(0,sbuf.toString(),null);
return sd;
}
}