/**
* <a href="http://www.openolat.org">
* OpenOLAT - Online Learning and Training</a><br>
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); <br>
* you may not use this file except in compliance with the License.<br>
* You may obtain a copy of the License at the
* <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache homepage</a>
* <p>
* Unless required by applicable law or agreed to in writing,<br>
* software distributed under the License is distributed on an "AS IS" BASIS, <br>
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br>
* See the License for the specific language governing permissions and <br>
* limitations under the License.
* <p>
* Initial code contributed and copyrighted by<br>
* frentix GmbH, http://www.frentix.com
* <p>
*/
package org.olat.modules.forms.handler;
import java.io.File;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import org.olat.core.logging.OLATRuntimeException;
import org.olat.core.logging.OLog;
import org.olat.core.logging.Tracing;
import org.olat.core.util.PathUtils;
import org.olat.core.util.xml.XStreamHelper;
import org.olat.fileresource.types.FileResource;
import org.olat.fileresource.types.ResourceEvaluation;
import org.olat.modules.forms.model.xml.Form;
import org.olat.modules.forms.model.xml.FormXStream;
/**
*
* Initial date: 6 déc. 2016<br>
* @author srosse, stephane.rosse@frentix.com, http://www.frentix.com
*
*/
public class EvaluationFormResource extends FileResource {
private static final OLog log = Tracing.createLoggerFor(EvaluationFormResource.class);
public static final String TYPE_NAME = "FileResource.FORM";
public static final String FORM_XML_FILE = "form.xml";
public EvaluationFormResource() {
super(TYPE_NAME);
}
public static ResourceEvaluation evaluate(File file, String filename) {
ResourceEvaluation eval = new ResourceEvaluation();
try {
FormFileFilter visitor = new FormFileFilter();
Path fPath = PathUtils.visit(file, filename, visitor);
if(visitor.isValid()) {
Path formPath = fPath.resolve(FORM_XML_FILE);
if(validateForm(formPath)) {
eval.setValid(true);
} else {
eval.setValid(false);
}
} else {
eval.setValid(false);
}
} catch (IOException | IllegalArgumentException e) {
log.error("", e);
eval.setValid(false);
}
return eval;
}
private static boolean validateForm(Path formPath) {
try {
Form form = (Form)XStreamHelper.readObject(FormXStream.getXStream(), formPath);
return form != null;
} catch (OLATRuntimeException e) {
//
}
return false;
}
private static class FormFileFilter extends SimpleFileVisitor<Path> {
private boolean formFile;
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
throws IOException {
String filename = file.getFileName().toString();
if(EvaluationFormResource.FORM_XML_FILE.equals(filename)) {
formFile = true;
}
return formFile ? FileVisitResult.TERMINATE : FileVisitResult.CONTINUE;
}
public boolean isValid() {
return formFile;
}
}
}