下面列出了java.io.File#separatorChar ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static void main(String[] args) throws Exception {
if (File.separatorChar == '\\' || // Windows
!new File(TEE).exists()) // no tee
return;
Process p = Runtime.getRuntime().exec(TEE);
OutputStream out = p.getOutputStream();
InputStream in = p.getInputStream();
Thread t1 = new WriterThread(out, in);
t1.start();
Thread t2 = new WriterThread(out, in);
t2.start();
t1.join();
t2.join();
if (savedException != null)
throw savedException;
}
/**
* Constructs an encoded version of the specified path string suitable
* for use in the construction of a URL.
*
* A path separator is replaced by a forward slash. The string is UTF8
* encoded. The % escape sequence is used for characters that are above
* 0x7F or those defined in RFC2396 as reserved or excluded in the path
* component of a URL.
*/
public static String encodePath(String path) {
StringBuffer sb = new StringBuffer();
int n = path.length();
for (int i=0; i<n; i++) {
char c = path.charAt(i);
if (c == File.separatorChar)
sb.append('/');
else {
if (c <= 0x007F) {
if (encodedInPath.get(c))
escape(sb, c);
else
sb.append(c);
} else if (c > 0x07FF) {
escape(sb, (char)(0xE0 | ((c >> 12) & 0x0F)));
escape(sb, (char)(0x80 | ((c >> 6) & 0x3F)));
escape(sb, (char)(0x80 | ((c >> 0) & 0x3F)));
} else {
escape(sb, (char)(0xC0 | ((c >> 6) & 0x1F)));
escape(sb, (char)(0x80 | ((c >> 0) & 0x3F)));
}
}
}
return sb.toString();
}
void test(String... args) throws Exception {
fm.setLocation(CLASS_OUTPUT, null); // no class files are
// generated, so this will
// not leave clutter in
// the source directory
Iterable<File> files = Arrays.asList(new File(test_src, "package-info.java"));
JavaFileObject src = fm.getJavaFileObjectsFromFiles(files).iterator().next();
char sep = File.separatorChar;
FileObject cls = fm.getFileForOutput(CLASS_OUTPUT,
"com.sun.foo.bar.baz",
"package-info.class",
src);
File expect = new File(test_src, "package-info.class");
File got = fm.asPath(cls).toFile();
if (!got.equals(expect))
throw new AssertionError(String.format("Expected: %s; got: %s", expect, got));
System.err.println("Expected: " + expect);
System.err.println("Got: " + got);
}
/**
* Constructs an encoded version of the specified path string suitable
* for use in the construction of a URL.
*
* A path separator is replaced by a forward slash. The string is UTF8
* encoded. The % escape sequence is used for characters that are above
* 0x7F or those defined in RFC2396 as reserved or excluded in the path
* component of a URL.
*/
public static String encodePath(String path) {
StringBuffer sb = new StringBuffer();
int n = path.length();
for (int i=0; i<n; i++) {
char c = path.charAt(i);
if (c == File.separatorChar)
sb.append('/');
else {
if (c <= 0x007F) {
if (encodedInPath.get(c))
escape(sb, c);
else
sb.append(c);
} else if (c > 0x07FF) {
escape(sb, (char)(0xE0 | ((c >> 12) & 0x0F)));
escape(sb, (char)(0x80 | ((c >> 6) & 0x3F)));
escape(sb, (char)(0x80 | ((c >> 0) & 0x3F)));
} else {
escape(sb, (char)(0xC0 | ((c >> 6) & 0x1F)));
escape(sb, (char)(0x80 | ((c >> 0) & 0x3F)));
}
}
}
return sb.toString();
}
/**
* @see ResourceListSourceSupport#getResources(String)
*/
protected String[] getResources(String basePath)
{
String root = (basePath == null) ? "" : basePath;
List resourceNames = new ArrayList();
List directories = getDirectories();
for (Iterator i = directories.iterator(); i.hasNext();)
{
String cpDirName = (String) i.next();
String dirName = null;
if (root.startsWith(File.separator) || cpDirName.endsWith(File.separator))
{
dirName = cpDirName + root;
}
else
{
dirName = cpDirName + File.separatorChar + root;
}
File dir = new File(dirName);
if (dir.exists() && dir.isDirectory())
{
File[] files = dir.listFiles(directoryFilter);
for (int j = 0; j < files.length; j++)
{
String fileName = files[j].getName();
String resourceName = root + File.separator + fileName;
resourceName = new File(resourceName).getPath();
resourceNames.add(resourceName);
}
}
}
return (String[]) resourceNames.toArray(new String[resourceNames.size()]);
}
protected String platformDependentStr(String s) {
if (File.separatorChar != '/') {
return org.python.pydev.shared_core.string.StringUtils
.replaceAll(s, "/", (File.separator + File.separator));
} else {
return s;
}
}
private String cleanFilePath(String path) {
// Remove leading and trailing whitespace
path = path.trim();
// Make all / and \ chars one
if (File.separatorChar == '/') {
path = path.replace('\\', '/');
} else {
path = path.replace('/', '\\');
}
// Remove leading ./
if (path.startsWith("." + File.separator)) {
path = path.substring(2);
}
return path;
}
/**
* Get the build directory
* @param file The java file.
* @return Returns the build directory.
*/
public static File getBuildDirectory(File file){
while(!"src".equals(file.getName())){
file = file.getParentFile();
}
return new File(file.getParentFile().getAbsolutePath() + File.separatorChar+ "build");
}
public static String slashify(String path, boolean isDirectory) {
String p = path;
if (File.separatorChar != '/')
p = p.replace(File.separatorChar, '/');
if (!p.startsWith("/"))
p = "/" + p;
if (!p.endsWith("/") && isDirectory)
p = p + "/";
return p;
}
private File getMdxTemplate() throws IOException {
final File mdxFileTemplate = new File(
localPropertiesService.getProjectFolder() + File.separatorChar + MDX_TEMPLATE);
if (!mdxFileTemplate.exists()) {
throw new IOException(
"The file containing the MDX Cube definition Template does not exist in expected location " //$NON-NLS-1$
+ mdxFileTemplate.getAbsolutePath());
}
return mdxFileTemplate;
}
public static String encodePath(String path, boolean flag) {
if (flag && File.separatorChar != '/') {
return encodePath(path, 0, File.separatorChar);
} else {
int index = firstEncodeIndex(path);
if (index > -1) {
return encodePath(path, index, '/');
} else {
return path;
}
}
}
public Package(Module m, String n) {
int c = n.indexOf(":");
assert(c != -1);
String mn = n.substring(0,c);
assert(m.name().equals(m.name()));
name = n;
dirname = n.replace('.', File.separatorChar);
if (m.name().length() > 0) {
// There is a module here, prefix the module dir name to the path.
dirname = m.dirname()+File.separatorChar+dirname;
}
}
public void logClassFile(boolean generatePackagesStructure, String outputPath, String relativeFileName) {
if ((this.tagBits & Logger.XML) != 0) {
String fileName = null;
if (generatePackagesStructure) {
fileName = buildFileName(outputPath, relativeFileName);
} else {
char fileSeparatorChar = File.separatorChar;
String fileSeparator = File.separator;
// First we ensure that the outputPath exists
outputPath = outputPath.replace('/', fileSeparatorChar);
// To be able to pass the mkdirs() method we need to remove the extra file separator at the end of the outDir name
int indexOfPackageSeparator = relativeFileName.lastIndexOf(fileSeparatorChar);
if (indexOfPackageSeparator == -1) {
if (outputPath.endsWith(fileSeparator)) {
fileName = outputPath + relativeFileName;
} else {
fileName = outputPath + fileSeparator + relativeFileName;
}
} else {
int length = relativeFileName.length();
if (outputPath.endsWith(fileSeparator)) {
fileName = outputPath + relativeFileName.substring(indexOfPackageSeparator + 1, length);
} else {
fileName = outputPath + fileSeparator + relativeFileName.substring(indexOfPackageSeparator + 1, length);
}
}
}
File f = new File(fileName);
try {
this.parameters.put(Logger.PATH, f.getCanonicalPath());
printTag(Logger.CLASS_FILE, this.parameters, true, true);
} catch (IOException e) {
logNoClassFileCreated(outputPath, relativeFileName, e);
}
}
}
/**
* Returns a file object corresponding to a built-in profile
* specified by fileName.
* If there is no built-in profile with such name, then the method
* returns null.
*/
private static File getStandardProfileFile(String fileName) {
String dir = System.getProperty("java.home") +
File.separatorChar + "lib" + File.separatorChar + "cmm";
String fullPath = dir + File.separatorChar + fileName;
File f = new File(fullPath);
return (f.isFile() && isChildOf(f, dir)) ? f : null;
}
public void setPattern(String globPattern) {
char[] gPat = globPattern.toCharArray();
char[] rPat = new char[gPat.length * 2];
boolean isWin32 = (File.separatorChar == '\\');
boolean inBrackets = false;
int j = 0;
this.globPattern = globPattern;
if (isWin32) {
// On windows, a pattern ending with *.* is equal to ending with *
int len = gPat.length;
if (globPattern.endsWith("*.*")) {
len -= 2;
}
for (int i = 0; i < len; i++) {
if (gPat[i] == '*') {
rPat[j++] = '.';
}
rPat[j++] = gPat[i];
}
} else {
for (int i = 0; i < gPat.length; i++) {
switch(gPat[i]) {
case '*':
if (!inBrackets) {
rPat[j++] = '.';
}
rPat[j++] = '*';
break;
case '?':
rPat[j++] = inBrackets ? '?' : '.';
break;
case '[':
inBrackets = true;
rPat[j++] = gPat[i];
if (i < gPat.length - 1) {
switch (gPat[i+1]) {
case '!':
case '^':
rPat[j++] = '^';
i++;
break;
case ']':
rPat[j++] = gPat[++i];
break;
}
}
break;
case ']':
rPat[j++] = gPat[i];
inBrackets = false;
break;
case '\\':
if (i == 0 && gPat.length > 1 && gPat[1] == '~') {
rPat[j++] = gPat[++i];
} else {
rPat[j++] = '\\';
if (i < gPat.length - 1 && "*?[]".indexOf(gPat[i+1]) >= 0) {
rPat[j++] = gPat[++i];
} else {
rPat[j++] = '\\';
}
}
break;
default:
//if ("+()|^$.{}<>".indexOf(gPat[i]) >= 0) {
if (!Character.isLetterOrDigit(gPat[i])) {
rPat[j++] = '\\';
}
rPat[j++] = gPat[i];
break;
}
}
}
this.pattern = Pattern.compile(new String(rPat, 0, j), Pattern.CASE_INSENSITIVE);
}
@Nonnull
public String getLogPath() {
return getSystemPath() + File.separatorChar + "logs";
}
private static String inferPackageName(String inputPath, String outputPath) {
// Normalize file names
inputPath = new File(inputPath).getPath();
outputPath = new File(outputPath).getPath();
// Split into components
String sep;
if (File.separatorChar == '\\') {
sep = "\\\\";
} else {
sep = File.separator;
}
String[] inputs = inputPath.split(sep);
String[] outputs = outputPath.split(sep);
// Match common names, eliminating first "classes" entry from
// each if present
int inStart = 0;
int inEnd = inputs.length - 2;
int outEnd = outputs.length - 2;
int i = inEnd;
int j = outEnd;
while (i >= 0 && j >= 0) {
if (!inputs[i].equals(outputs[j]) ||
(inputs[i].equals("gensrc") && inputs[j].equals("gensrc"))) {
++i;
++j;
break;
}
--i;
--j;
}
String result;
if (i < 0 || j < 0 || i >= inEnd || j >= outEnd) {
result = "";
} else {
if (inputs[i].equals("classes") && outputs[j].equals("classes")) {
++i;
}
inStart = i;
StringBuffer buf = new StringBuffer();
for (i = inStart; i <= inEnd; i++) {
buf.append(inputs[i]);
if (i < inEnd) {
buf.append('.');
}
}
result = buf.toString();
}
return result;
}
private static String inferPackageName(String inputPath, String outputPath) {
// Normalize file names
inputPath = new File(inputPath).getPath();
outputPath = new File(outputPath).getPath();
// Split into components
String sep;
if (File.separatorChar == '\\') {
sep = "\\\\";
} else {
sep = File.separator;
}
String[] inputs = inputPath.split(sep);
String[] outputs = outputPath.split(sep);
// Match common names, eliminating first "classes" entry from
// each if present
int inStart = 0;
int inEnd = inputs.length - 2;
int outEnd = outputs.length - 2;
int i = inEnd;
int j = outEnd;
while (i >= 0 && j >= 0) {
if (!inputs[i].equals(outputs[j]) ||
(inputs[i].equals("gensrc") && inputs[j].equals("gensrc"))) {
++i;
++j;
break;
}
--i;
--j;
}
String result;
if (i < 0 || j < 0 || i >= inEnd || j >= outEnd) {
result = "";
} else {
if (inputs[i].equals("classes") && outputs[j].equals("classes")) {
++i;
}
inStart = i;
StringBuffer buf = new StringBuffer();
for (i = inStart; i <= inEnd; i++) {
buf.append(inputs[i]);
if (i < inEnd) {
buf.append('.');
}
}
result = buf.toString();
}
return result;
}
private static String getStorageDir(String sandboxName, String directory) {
return System.getProperty("atlas.data") +
File.separatorChar + sandboxName +
File.separatorChar + directory;
}
@Override
public void processor(Blade blade) {
JetbrickTemplateEngine templateEngine = new JetbrickTemplateEngine();
List<String> macros = new ArrayList<>(8);
macros.add(File.separatorChar + "comm" + File.separatorChar + "macros.html");
// 扫描主题下面的所有自定义宏
String themeDir = AttachController.CLASSPATH + "templates" + File.separatorChar + "themes";
File[] dir = new File(themeDir).listFiles();
for (File f : dir) {
if (f.isDirectory() && Files.exists(Paths.get(f.getPath() + File.separatorChar + "macros.html"))) {
String macroName = File.separatorChar + "themes" + File.separatorChar + f.getName() + File.separatorChar + "macros.html";
macros.add(macroName);
}
}
StringBuffer sbuf = new StringBuffer();
macros.forEach(s -> sbuf.append(',').append(s));
templateEngine.addConfig("jetx.import.macros", sbuf.substring(1));
GlobalResolver resolver = templateEngine.getGlobalResolver();
resolver.registerFunctions(Commons.class);
resolver.registerFunctions(Theme.class);
resolver.registerFunctions(AdminCommons.class);
resolver.registerTags(JetTag.class);
JetGlobalContext context = templateEngine.getGlobalContext();
context.set("version", environment.get("app.version", "v1.0"));
context.set("enableCdn", environment.getBoolean("app.enableCdn", false));
blade.templateEngine(templateEngine);
TaleConst.ENABLED_CDN = environment.getBoolean("app.enableCdn", false);
TaleConst.MAX_FILE_SIZE = environment.getInt("app.max-file-size", 20480);
TaleConst.AES_SALT = environment.get("app.salt", "012c456789abcdef");
TaleConst.OPTIONS.addAll(optionsService.getOptions());
String ips = TaleConst.OPTIONS.get(Types.BLOCK_IPS, "");
if (StringKit.isNotBlank(ips)) {
TaleConst.BLOCK_IPS.addAll(Arrays.asList(ips.split(",")));
}
if (Files.exists(Paths.get(AttachController.CLASSPATH + "install.lock"))) {
TaleConst.INSTALLED = Boolean.TRUE;
}
BaseController.THEME = "themes/" + Commons.site_option("site_theme");
TaleConst.BCONF = environment;
}