Skip to content

Commit

Permalink
XFF-10: Remove index file
Browse files Browse the repository at this point in the history
* Create a core for XFF
* Add a folder explorer (to read files in the correct order)
* Create a XFF packager (create a ZIP with entries in the correct order)
* Rely more on NIO instead of IO
  • Loading branch information
woshilapin committed Nov 27, 2015
1 parent 16dcd0e commit f0e32aa
Show file tree
Hide file tree
Showing 63 changed files with 1,439 additions and 392 deletions.
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
</repositories>

<modules>
<module>xff-core</module>
<module>xff-filter-stream</module>
<module>xff-rest-api</module>
</modules>
Expand Down
63 changes: 63 additions & 0 deletions xff-core/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.xwiki.contrib</groupId>
<artifactId>xff</artifactId>
<version>0.3.2-SNAPSHOT</version>
</parent>
<artifactId>xff-core</artifactId>
<packaging>jar</packaging>
<name>XFF - Core</name>
<description>Core of the XFF packaging system which contains the model.</description>

<scm>
<connection>scm:git:git://github.com/xwiki-contrib/api-xff.git</connection>
<developerConnection>scm:git:git@github.com:xwiki-contrib/api-xff.git</developerConnection>
<url>https://github.com/xwiki-contrib/api-xff/tree/master/xff-core</url>
<tag>HEAD</tag>
</scm>

<properties>
<!-- Name to display by the Extension Manager -->
<xwiki.extension.name>XFF Core</xwiki.extension.name>
</properties>

<dependencies>
<!-- Test dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<!-- Apply the Checkstyle configurations defined in the top level pom.xml file -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
123 changes: 123 additions & 0 deletions xff-core/src/main/java/org/xwiki/xff/core/XFFExplorer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.xwiki.xff.core;

import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.FileVisitor;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.xwiki.xff.core.model.XFF;

/**
* This explorer will go through the folders of the package and provide interface to get the files in a correct order.
*
* @version $Id$
* @since 7.1
*/
public class XFFExplorer extends SimpleFileVisitor<Path> implements Iterator<Path>, Iterable<Path>
{
/**
* Root path to remind because XFF is only working with relative paths.
*/
private Path rootPath;

/**
* List of paths of the XFF package (only referring to files).
*/
private List<Path> paths;

/**
* Current index, used for the iterator.
*/
private int index = -1;

/**
* Initialize the Walker by parsing the folder and ordering the files.
*
* @param path is the root path where to look for a XFF package.
* @throws IOException whenever there is problems in reading files or walking through the folders.
*/
public XFFExplorer(Path path) throws IOException
{
this.init(path);
}

private void init(Path path) throws IOException
{
this.rootPath = path;
this.paths = new ArrayList<Path>();
FileVisitor<Path> visitor = new SimpleFileVisitor<Path>()
{
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException
{
paths.add(rootPath.relativize(file));
return FileVisitResult.CONTINUE;
}
};
Files.walkFileTree(path, visitor);
XFF xff = new XFF();
for (Path p : this.paths) {
xff.store(p);
}
this.paths = xff.orderedPaths();
}

@Override
public boolean hasNext()
{
return index + 1 < this.paths.size();
}

@Override
public Path next()
{
this.index += 1;
return this.paths.get(this.index);
}

@Override
public Iterator<Path> iterator()
{
return this;
}

@Override
public void remove()
{
throw new UnsupportedOperationException("The XFF Walker does not implement the remove method");
}

/**
* Get the root path of the XFF package (a folder).
*
* @return the root path of the XFF package
*/
public Path getPath() {
return this.rootPath;
}
}
84 changes: 84 additions & 0 deletions xff-core/src/main/java/org/xwiki/xff/core/XFFZipper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.xwiki.xff.core;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.HashMap;
import java.util.Map;

/**
* Create zip file out of a XFF folder structure.
*
* @version $Id$
* @since 7.1
*/
public class XFFZipper
{
/**
* To explore into XFF package.
*/
private XFFExplorer packageExplorer;

/**
* Initialize the Walker by parsing the folder and ordering the files.
*
* @param path is the root path where to look for a XFF package.
* @throws IOException whenever there is problems in reading files or walking through the folders.
*/
public XFFZipper(Path path) throws IOException
{
this.packageExplorer = new XFFExplorer(path);
}

/**
* Create an XFF package from the folder.
*
* @param path to the file you want to write the package into.
* @throws IOException if file path is incorrect or if there is problem to find files of the package.
* @throws URISyntaxException only if path is incorrect
*/
public void xff(Path path) throws IOException, URISyntaxException
{
Map<String, String> options = new HashMap<>();
options.put("create", "true");
options.put("encoding", "UTF-8");
Path rootPath = this.packageExplorer.getPath();
URI xffURI = URI.create(String.format("jar:file:%s", path.toString()));
try (FileSystem xff = FileSystems.newFileSystem(xffURI, options)) {
for (Path p : this.packageExplorer) {
Path filePath = rootPath.resolve(p);
Path xffPath = xff.getPath(p.toString());
Path dirPath = xff.getPath(xffPath.subpath(0, xffPath.getNameCount() - 1).toString());
if (!Files.isDirectory(dirPath)) {
Files.createDirectories(dirPath);
}
Files.copy(filePath, xffPath, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES);
}
}
// zos.close();
}
}
72 changes: 72 additions & 0 deletions xff-core/src/main/java/org/xwiki/xff/core/model/Attachment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.xwiki.xff.core.model;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

/**
* Represent the structure of an attachment.
*
* @version $Id$
* @since 7.1
*/
public class Attachment implements XElement
{
/**
* Prefix used in the path for attachments.
*/
public static final String ATTACHMENTS_HINT = "attachments";

/**
* List of paths for attachments.
*/
private Set<String> attachments;

/**
* Empty constructors initializing attributes.
*/
public Attachment()
{
this.attachments = new TreeSet<String>();
}

@Override
public void store(Path path)
{
if (path.getNameCount() == 1) {
this.attachments.add(path.toString());
}
}

@Override
public List<Path> orderedPaths()
{
List<Path> paths = new ArrayList<Path>();
for (String attachmentName : this.attachments) {
paths.add(Paths.get(attachmentName));
}
return paths;
}
}
Loading

0 comments on commit f0e32aa

Please sign in to comment.