mirror of
https://github.com/querydsl/querydsl.git
synced 2026-06-21 21:14:12 +08:00
extended File API
This commit is contained in:
parent
c73353c3d4
commit
09a2ccbeda
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2009 Mysema Ltd.
|
||||
* Copyright (c) 2010 Mysema Ltd.
|
||||
* All rights reserved.
|
||||
*
|
||||
*/
|
||||
|
||||
@ -1,3 +1,8 @@
|
||||
/*
|
||||
* Copyright (c) 2010 Mysema Ltd.
|
||||
* All rights reserved.
|
||||
*
|
||||
*/
|
||||
package com.mysema.query;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
@ -1,3 +1,8 @@
|
||||
/*
|
||||
* Copyright (c) 2010 Mysema Ltd.
|
||||
* All rights reserved.
|
||||
*
|
||||
*/
|
||||
package com.mysema.query;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@ -1,3 +1,8 @@
|
||||
/*
|
||||
* Copyright (c) 2010 Mysema Ltd.
|
||||
* All rights reserved.
|
||||
*
|
||||
*/
|
||||
package com.mysema.query.alias;
|
||||
|
||||
import com.mysema.query.types.Expr;
|
||||
|
||||
@ -1,3 +1,8 @@
|
||||
/*
|
||||
* Copyright (c) 2010 Mysema Ltd.
|
||||
* All rights reserved.
|
||||
*
|
||||
*/
|
||||
package com.mysema.query.alias;
|
||||
|
||||
import static com.mysema.query.alias.Alias.$;
|
||||
|
||||
@ -1,3 +1,8 @@
|
||||
/*
|
||||
* Copyright (c) 2010 Mysema Ltd.
|
||||
* All rights reserved.
|
||||
*
|
||||
*/
|
||||
package com.mysema.query.alias;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2009 Mysema Ltd.
|
||||
* Copyright (c) 2010 Mysema Ltd.
|
||||
* All rights reserved.
|
||||
*
|
||||
*/
|
||||
|
||||
@ -1,3 +1,8 @@
|
||||
/*
|
||||
* Copyright (c) 2010 Mysema Ltd.
|
||||
* All rights reserved.
|
||||
*
|
||||
*/
|
||||
package com.mysema.query.animal;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2009 Mysema Ltd.
|
||||
* Copyright (c) 2010 Mysema Ltd.
|
||||
* All rights reserved.
|
||||
*
|
||||
*/
|
||||
|
||||
@ -1,3 +1,8 @@
|
||||
/*
|
||||
* Copyright (c) 2010 Mysema Ltd.
|
||||
* All rights reserved.
|
||||
*
|
||||
*/
|
||||
package com.mysema.query.animal;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2009 Mysema Ltd.
|
||||
* Copyright (c) 2010 Mysema Ltd.
|
||||
* All rights reserved.
|
||||
*
|
||||
*/
|
||||
|
||||
@ -1,3 +1,8 @@
|
||||
/*
|
||||
* Copyright (c) 2010 Mysema Ltd.
|
||||
* All rights reserved.
|
||||
*
|
||||
*/
|
||||
package com.mysema.query.animal;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2009 Mysema Ltd.
|
||||
* Copyright (c) 2010 Mysema Ltd.
|
||||
* All rights reserved.
|
||||
*
|
||||
*/
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2009 Mysema Ltd.
|
||||
* Copyright (c) 2010 Mysema Ltd.
|
||||
* All rights reserved.
|
||||
*
|
||||
*/
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2009 Mysema Ltd.
|
||||
* Copyright (c) 2010 Mysema Ltd.
|
||||
* All rights reserved.
|
||||
*
|
||||
*/
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2009 Mysema Ltd.
|
||||
* Copyright (c) 2010 Mysema Ltd.
|
||||
* All rights reserved.
|
||||
*
|
||||
*/
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2009 Mysema Ltd.
|
||||
* Copyright (c) 2010 Mysema Ltd.
|
||||
* All rights reserved.
|
||||
*
|
||||
*/
|
||||
|
||||
@ -1,3 +1,8 @@
|
||||
/*
|
||||
* Copyright (c) 2010 Mysema Ltd.
|
||||
* All rights reserved.
|
||||
*
|
||||
*/
|
||||
package com.mysema.query.collections;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2009 Mysema Ltd.
|
||||
* Copyright (c) 2010 Mysema Ltd.
|
||||
* All rights reserved.
|
||||
*
|
||||
*/
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2009 Mysema Ltd.
|
||||
* Copyright (c) 2010 Mysema Ltd.
|
||||
* All rights reserved.
|
||||
*
|
||||
*/
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2009 Mysema Ltd.
|
||||
* Copyright (c) 2010 Mysema Ltd.
|
||||
* All rights reserved.
|
||||
*
|
||||
*/
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2009 Mysema Ltd.
|
||||
* Copyright (c) 2010 Mysema Ltd.
|
||||
* All rights reserved.
|
||||
*
|
||||
*/
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2009 Mysema Ltd.
|
||||
* Copyright (c) 2010 Mysema Ltd.
|
||||
* All rights reserved.
|
||||
*
|
||||
*/
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2009 Mysema Ltd.
|
||||
* Copyright (c) 2010 Mysema Ltd.
|
||||
* All rights reserved.
|
||||
*
|
||||
*/
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2009 Mysema Ltd.
|
||||
* Copyright (c) 2010 Mysema Ltd.
|
||||
* All rights reserved.
|
||||
*
|
||||
*/
|
||||
|
||||
@ -0,0 +1,32 @@
|
||||
/*
|
||||
* Copyright (c) 2010 Mysema Ltd.
|
||||
* All rights reserved.
|
||||
*
|
||||
*/
|
||||
package com.mysema.query.file;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Iterator;
|
||||
|
||||
/**
|
||||
* @author tiwe
|
||||
*
|
||||
*/
|
||||
public class DirectoryWalk implements Iterable<File>{
|
||||
|
||||
private final File directory;
|
||||
|
||||
public DirectoryWalk(String path) {
|
||||
this.directory = new File(path);
|
||||
}
|
||||
|
||||
public DirectoryWalk(File directory) {
|
||||
this.directory = directory;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<File> iterator() {
|
||||
return new DirectoryWalkIterator(directory);
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,57 @@
|
||||
/*
|
||||
* Copyright (c) 2010 Mysema Ltd.
|
||||
* All rights reserved.
|
||||
*
|
||||
*/
|
||||
package com.mysema.query.file;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.Deque;
|
||||
import java.util.Iterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
/**
|
||||
* @author tiwe
|
||||
*
|
||||
*/
|
||||
public class DirectoryWalkIterator implements Iterator<File> {
|
||||
|
||||
private final Deque<File> files = new ArrayDeque<File>();
|
||||
|
||||
public DirectoryWalkIterator(File directory) {
|
||||
File[] children = directory.listFiles();
|
||||
if (children != null){
|
||||
for (File file : children){
|
||||
files.add(file);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return !files.isEmpty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public File next() {
|
||||
if (!files.isEmpty()){
|
||||
File file = files.pop();
|
||||
File[] children = file.listFiles();
|
||||
if (children != null){
|
||||
for (File child : children){
|
||||
files.add(child);
|
||||
}
|
||||
}
|
||||
return file;
|
||||
}else{
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,22 @@
|
||||
/*
|
||||
* Copyright (c) 2010 Mysema Ltd.
|
||||
* All rights reserved.
|
||||
*
|
||||
*/
|
||||
package com.mysema.query.file;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class DirectoryWalkTest {
|
||||
|
||||
@Test
|
||||
public void test(){
|
||||
DirectoryWalk walk = new DirectoryWalk(new File("target"));
|
||||
for (File file : walk){
|
||||
System.out.println(file.getPath());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,22 +0,0 @@
|
||||
package com.mysema.query.file;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.mysema.query.collections.MiniApi;
|
||||
|
||||
public class FileTest {
|
||||
|
||||
@Test
|
||||
public void path_to_file_map(){
|
||||
Map<String,File> files = MiniApi
|
||||
.from(QFile.any, new File(".").listFiles())
|
||||
.map(QFile.any.absolutePath, QFile.any);
|
||||
|
||||
for (Map.Entry<String, File> entry : files.entrySet()){
|
||||
System.out.println(entry.getKey() + " : " + entry.getValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,3 +1,8 @@
|
||||
/*
|
||||
* Copyright (c) 2010 Mysema Ltd.
|
||||
* All rights reserved.
|
||||
*
|
||||
*/
|
||||
package com.mysema.query.file;
|
||||
|
||||
import static com.mysema.query.types.path.PathMetadataFactory.forProperty;
|
||||
@ -6,6 +11,9 @@ import static com.mysema.query.types.path.PathMetadataFactory.forVariable;
|
||||
import java.io.File;
|
||||
|
||||
import com.mysema.query.types.PathMetadata;
|
||||
import com.mysema.query.types.custom.CString;
|
||||
import com.mysema.query.types.expr.EString;
|
||||
import com.mysema.query.types.expr.EStringConst;
|
||||
import com.mysema.query.types.path.PBoolean;
|
||||
import com.mysema.query.types.path.PComparable;
|
||||
import com.mysema.query.types.path.PString;
|
||||
@ -16,10 +24,16 @@ import com.mysema.query.types.path.PString;
|
||||
*/
|
||||
public class QFile extends PComparable<File>{
|
||||
|
||||
private static final long serialVersionUID = -7703329992523284173L;
|
||||
|
||||
private static final String GET_CONTENT = "org.apache.commons.io.FileUtils.readFileToString({0}, {1})";
|
||||
|
||||
public static final QFile any = new QFile("any");
|
||||
|
||||
private static final long serialVersionUID = -7703329992523284173L;
|
||||
|
||||
public static Iterable<File> walk(File dir){
|
||||
return new DirectoryWalk(dir);
|
||||
}
|
||||
|
||||
public final PBoolean absolute = new PBoolean(this, "absolute");
|
||||
|
||||
private volatile QFile absoluteFile, canonicalFile, parentFile;
|
||||
@ -66,6 +80,10 @@ public class QFile extends PComparable<File>{
|
||||
return canonicalFile;
|
||||
}
|
||||
|
||||
public EString getContent(String encoding){
|
||||
return CString.create(GET_CONTENT, this, EStringConst.create(encoding));
|
||||
}
|
||||
|
||||
public QFile parentFile() {
|
||||
if (parentFile == null){
|
||||
parentFile = new QFile(this, "parentFile");
|
||||
|
||||
@ -0,0 +1,52 @@
|
||||
/*
|
||||
* Copyright (c) 2010 Mysema Ltd.
|
||||
* All rights reserved.
|
||||
*
|
||||
*/
|
||||
package com.mysema.query.file;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.mysema.query.collections.MiniApi;
|
||||
|
||||
public class QFileTest {
|
||||
|
||||
@Test
|
||||
public void path_to_file_map(){
|
||||
QFile anyFile = QFile.any;
|
||||
Map<String,File> files = MiniApi
|
||||
.from(anyFile, new File(".").listFiles())
|
||||
.map(anyFile.absolutePath, anyFile);
|
||||
|
||||
for (Map.Entry<String, File> entry : files.entrySet()){
|
||||
System.out.println(entry.getKey() + " : " + entry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void walk(){
|
||||
QFile anyFile = QFile.any;
|
||||
for (File file : MiniApi
|
||||
.from(anyFile, QFile.walk(new File("target")))
|
||||
.where(anyFile.name.endsWith(".class"))
|
||||
.list(anyFile)){
|
||||
System.out.println(file.getName());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void getContent(){
|
||||
// FIXME
|
||||
QFile anyFile = QFile.any;
|
||||
Map<File,String> rv = MiniApi
|
||||
.from(anyFile, QFile.walk(new File("src/test")))
|
||||
.where(anyFile.name.endsWith(".properties"))
|
||||
.map(anyFile, anyFile.getContent("ISO-8859-1"));
|
||||
System.out.println(rv);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user