Commit 24e19fb5 authored by Maxime Lefrançois's avatar Maxime Lefrançois
Browse files

pull repository saref-portal-static

parent 67ecd2b7
Loading
Loading
Loading
Loading
+0 −21
Original line number Diff line number Diff line
@@ -175,27 +175,6 @@
	<build>
		<finalName>${project.artifactId}</finalName>
		<plugins>
			<plugin>
				<artifactId>maven-assembly-plugin</artifactId>
				<version>3.2.0</version>
				<configuration>
					<finalName>static</finalName>
					<appendAssemblyId>false</appendAssemblyId>
  					<descriptors>
						<descriptor>src/main/static/assembly.xml</descriptor>
					</descriptors>
					<outputDirectory>${project.build.outputDirectory}</outputDirectory>
				</configuration>
				<executions>
					<execution>
						<id>static</id>
						<phase>process-resources</phase>
						<goals>
							<goal>single</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-shade-plugin</artifactId>
+5 −1
Original line number Diff line number Diff line
@@ -7,10 +7,14 @@ import java.util.regex.Pattern;

public class Constants {
	public final static String BASE = "https://saref.etsi.org/";
	public final static String BASE_DOC = BASE + "documentation/";

	public final static String LOGGER_BASE = "fr.emse.gitlab.saref.logger";
	
	public static final String REGEX_EXT = "(?<ext>core|saref4[a-z]{4})";
	public final static String SAREF_PORTAL_STATIC_GIT = "https://gitlab.emse.fr/saref/saref-portal-static.git";
//	public final static String SAREF_PORTAL_STATIC_GIT = "https://forge.etsi.org/rep/SAREF/saref-portal-static.git"; // uncomment when the project is public
	
	public static final String REGEX_EXT = "(?<ext>core|saref4[a-z0-9]{4})";
	public static final String REGEX_VERSION = "v(?<major>[1-9][0-9]*)\\.(?<minor>[0-9]+)\\.(?<patch>[0-9]+)";

	public static final String REGEX_ONTO_SERIES_URI = "^" + BASE + REGEX_EXT + "/$";
+76 −50
Original line number Diff line number Diff line
package fr.emse.gitlab.saref;

import static fr.emse.gitlab.saref.CMDConfigurations.*;
import static fr.emse.gitlab.saref.CMDConfigurations.ARG_DIRECTORY;
import static fr.emse.gitlab.saref.CMDConfigurations.ARG_DIRECTORY_DEFAULT;
import static fr.emse.gitlab.saref.CMDConfigurations.ARG_HELP;
import static fr.emse.gitlab.saref.CMDConfigurations.ARG_IGNORE_EXAMPLES;
import static fr.emse.gitlab.saref.CMDConfigurations.ARG_IGNORE_GIT;
import static fr.emse.gitlab.saref.CMDConfigurations.ARG_IGNORE_TERMS;
import static fr.emse.gitlab.saref.CMDConfigurations.ARG_INCLUDE_MASTER;
import static fr.emse.gitlab.saref.CMDConfigurations.ARG_NO_SITE;
import static fr.emse.gitlab.saref.CMDConfigurations.ARG_REMOTE_ONLY;
import static fr.emse.gitlab.saref.CMDConfigurations.ARG_VERBOSE;

import java.awt.Desktop;
import java.io.File;
@@ -9,7 +18,6 @@ import java.io.IOException;
import java.io.StringWriter;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
@@ -33,6 +41,7 @@ import org.apache.commons.cli.ParseException;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.jena.atlas.io.IndentedWriter;
import org.apache.jena.atlas.web.TypedInputStream;
import org.apache.jena.graph.NodeFactory;
import org.apache.jena.query.Dataset;
import org.apache.jena.query.ReadWrite;
@@ -97,7 +106,6 @@ public class Main {
	private static Dataset dataset;
	private static Var VAR_TEST_SUITES = VarUtils.allocVar("testsuites");


	static boolean remoteOnly;
	static boolean ignoreGit;
	static boolean includeMaster;
@@ -106,10 +114,9 @@ public class Main {
	static boolean ignoreExamples;
	static boolean verbose;

	
	
	public static void main(String[] args)
			throws IOException, InterruptedException, URISyntaxException, JAXBException, ParseException, RefAlreadyExistsException, RefNotFoundException, InvalidRefNameException, CheckoutConflictException, GitAPIException {
	public static void main(String[] args) throws IOException, InterruptedException, URISyntaxException, JAXBException,
			ParseException, RefAlreadyExistsException, RefNotFoundException, InvalidRefNameException,
			CheckoutConflictException, GitAPIException {

		CommandLine cl = CMDConfigurations.parseArguments(args);
		if (cl.getOptions().length == 0 || cl.hasOption(ARG_HELP)) {
@@ -140,7 +147,8 @@ public class Main {

		Repositories repositories;
		try {
			repositories = new ReadRepositories("Fetching required repositories").readRepositories(directory, remoteOnly, ignoreGit, includeMaster);
			repositories = new ReadRepositories("Fetching required repositories").readRepositories(directory,
					remoteOnly, ignoreGit, includeMaster);
		} catch (Exception ex) {
			reportAndExit(-1);
			return;
@@ -160,10 +168,10 @@ public class Main {
		new CheckConfig().doJob(dataset, ignoreExamples);

		if (generateSite) {
			new GeneratePortal("Generate static files for the portal", streamManager).doJob(dataset, directory, verbose, ignoreExamples, ignoreTerms);
			new GeneratePortal("Generate static files for the portal", streamManager).doJob(dataset, directory, verbose,
					ignoreExamples, ignoreTerms);
		}


		if (repositories.getDefaultRepository() != null && !ignoreGit) {
			Repository repository = repositories.getDefaultRepository();
			resetCheckout(repository);
@@ -190,17 +198,21 @@ public class Main {

	private static void prepareDirectory() throws IOException {
		target = new File(directory, Constants.TARGET_DIR);
		File site = new File(directory, Constants.SITE_DIR);
		FileUtils.forceMkdir(target);
		if (generateSite) {
			File staticTargetDir = new File(directory, Constants.STATIC_TARGET_DIR);
			File staticTargetZip = new File(directory, Constants.STATIC_TARGET_DIR + ".zip");
			URL url = Main.class.getClassLoader().getResource("static.zip");
			FileUtils.copyURLToFile(url, staticTargetZip);
	    	UnzipFile.unzip(staticTargetZip, staticTargetDir);
	    	FileUtils.deleteQuietly(staticTargetZip);
			try (Git git = Git.cloneRepository().setURI(Constants.SAREF_PORTAL_STATIC_GIT).setDirectory(site).call()) {
				LOG.info(String.format("Cloning site repository %s", Constants.SAREF_PORTAL_STATIC_GIT));
			} catch (Exception ex) {
				try (Git git = Git.open(site)) {
					git.pull().call();
					LOG.info(String.format("Pulling site repository %s", Constants.SAREF_PORTAL_STATIC_GIT));
				} catch (Exception e) {
					throw new IOException(String.format("Failed to clone or pull site repository %s", Constants.SAREF_PORTAL_STATIC_GIT), e);
				}
			}
		}
	}


	private static Dataset createFreshDataset() throws IOException {
		File datasetDir = new File(directory, Constants.DATASET_DIR);
@@ -210,7 +222,9 @@ public class Main {
		dataset.getDefaultModel().removeAll();
		List<String> toRemove = new ArrayList<>();
		Iterator<String> it = dataset.listNames();
		while(it.hasNext()) { toRemove.add(it.next()); }
		while (it.hasNext()) {
			toRemove.add(it.next());
		}
		for (String name : toRemove) {
			dataset.removeNamedModel(name);
		}
@@ -294,28 +308,39 @@ public class Main {
	}

	private static SPARQLExtStreamManager initializeStreamManager() throws IOException, URISyntaxException {
		SPARQLExtStreamManager sm = SPARQLExtStreamManager.makeStreamManager();
		LocationMapperAccept mapper = new LocationMapperAccept();

		// set mapper for saref-pipeline src/documentation directory
		URI uri = Main.class.getClassLoader().getResource("documentation").toURI();
		boolean isJar = uri.getScheme().equals("jar");
		Path dirPath;
		if(uri.getScheme().equals("jar")) {
		if (isJar) {
			FileSystem fileSystem = FileSystems.newFileSystem(uri, Collections.<String, Object>emptyMap());
			dirPath = fileSystem.getPath("/documentation");
		} else {
			dirPath = Paths.get(uri);
		}
		LocatorFileAccept locator = new LocatorFileAccept(uri.getPath());
		LocationMapperAccept mapper = new LocationMapperAccept();
		SPARQLExtStreamManager sm = SPARQLExtStreamManager.makeStreamManager(locator);
		walk(dirPath, isJar, mapper);
		
		// override with local files in documentation directory
		dirPath = Paths.get(new File(directory, "documentation").toURI());
		walk(dirPath, false, mapper);
		
		sm.setLocationMapper(mapper);
		return sm;
	}
	
	private static void walk(Path dirPath, boolean isJar, LocationMapperAccept mapper) throws IOException {
		Files.walk(dirPath).forEach((p) -> {
			String relativePath = dirPath.relativize(p).toString().replace("\\", "/");
			String fileurl = BASE_DOC + relativePath;
			if(uri.getScheme().equals("jar")) {
			if(isJar) {
				mapper.addAltEntry(fileurl, p.toString().substring(1));
			} else {
				mapper.addAltEntry(fileurl, p.toString());
			}
		});
		return sm;
	}


@@ -345,14 +370,15 @@ public class Main {
				// generate the report.html
				File reportHTML = new File(directory, Constants.SITE_DIR + File.separator + "report.html");
				try (IndentedWriter writer = new IndentedWriter(new FileOutputStream(reportHTML));) {
					Context context = ContextUtils.build(writer).setBase(Constants.BASE)
							.setDebugTemplate(verbose).setStreamManager(streamManager).build();
					Context context = ContextUtils.build(writer).setBase(Constants.BASE).setDebugTemplate(verbose)
							.setStreamManager(streamManager).build();
					BindingHashMap binding = new BindingHashMap();
					binding.add(VAR_TEST_SUITES, NodeFactory.createLiteral(sw.toString()));
					List<Binding> bindings = new ArrayList<>();
					bindings.add(binding);
					String query = IOUtils.toString(
							streamManager.open(new LookUpRequest("documentation/report/main.rqg", SPARQLExt.MEDIA_TYPE)),
							streamManager
									.open(new LookUpRequest("documentation/report/main.rqg", SPARQLExt.MEDIA_TYPE)),
							StandardCharsets.UTF_8);
					RootPlan reportPlan = PlanFactory.create(query, BASE_DOC);
					reportPlan.execTemplateStream(bindings, context);
+0 −48
Original line number Diff line number Diff line
package fr.emse.gitlab.saref;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import org.apache.commons.io.FileUtils;

public class UnzipFile {
	
    public static void unzip(File fileZip,  File destDir) throws IOException {
        byte[] buffer = new byte[1024];
        ZipInputStream zis = new ZipInputStream(new FileInputStream(fileZip));
        ZipEntry zipEntry = zis.getNextEntry();
        while (zipEntry != null) {
            File newFile = newFile(destDir, zipEntry);
        	if(zipEntry.isDirectory()) {
                FileUtils.forceMkdir(newFile);
        	} else {
	            FileOutputStream fos = new FileOutputStream(newFile);
	            int len;
	            while ((len = zis.read(buffer)) > 0) {
	                fos.write(buffer, 0, len);
	            }
	            fos.close();
        	}
            zipEntry = zis.getNextEntry();
        }
        zis.closeEntry();
        zis.close();
    }
     
    public static File newFile(File destinationDir, ZipEntry zipEntry) throws IOException {
        File destFile = new File(destinationDir, zipEntry.getName());
         
        String destDirPath = destinationDir.getCanonicalPath();
        String destFilePath = destFile.getCanonicalPath();
         
        if (!destFilePath.startsWith(destDirPath + File.separator)) {
            throw new IOException("Entry is outside of the target dir: " + zipEntry.getName());
        }
         
        return destFile;
    }
}
 No newline at end of file