Skip to content

Commit

Permalink
SLING-11421 Use FileVault serialization classes for generating enhanc…
Browse files Browse the repository at this point in the history
…ed (#161)

Remove obsolete stax-utils dependency
SLING-11785 add test case validating escaping strings with backslashes

Co-authored-by: Stefan Seifert <stefanseifert@users.noreply.github.com>
  • Loading branch information
kwin and stefanseifert committed Mar 2, 2023
1 parent 3ee954a commit 084e751
Show file tree
Hide file tree
Showing 19 changed files with 386 additions and 538 deletions.
15 changes: 8 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
<sling.java.version>8</sling.java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<picocli.version>3.6.0</picocli.version>
<org.apache.jackrabbit.vault.version>3.6.0</org.apache.jackrabbit.vault.version>
<org.apache.jackrabbit.vault.version>3.6.8</org.apache.jackrabbit.vault.version>
<jackrabbit-api.version>1.42.0</jackrabbit-api.version>
<jackrabbit-spi-commons.version>2.20.4</jackrabbit-spi-commons.version>
<license-maven-plugin.version>1.16</license-maven-plugin.version>
Expand Down Expand Up @@ -115,12 +115,6 @@
<scope>compile</scope>
</dependency>

<dependency>
<groupId>stax-utils</groupId>
<artifactId>stax-utils</artifactId>
<version>snapshot-20040917</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.jackrabbit</groupId>
<artifactId>jackrabbit-spi-commons</artifactId>
Expand All @@ -138,6 +132,12 @@
<version>2.11.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.6</version>
<scope>compile</scope>
</dependency>

<!--
| Sling Feature Model libraries
Expand Down Expand Up @@ -379,6 +379,7 @@
<exclude>src/main/resources/META-INF/services/org.apache.sling.feature.cpconverter.handlers.EntryHandler</exclude>
<exclude>src/main/legal/NOTICE-with-deps</exclude>
<exclude>src/test/resources/org/apache/sling/feature/cpconverter/handlers/bundle-entry-xmls/**</exclude>
<exclude>src/test/resources/org/apache/sling/feature/cpconverter/handlers/escaping-test/**</exclude>
<exclude>src/test/resources/org/apache/sling/feature/cpconverter/handlers/i18n-jsonfile-xml-descriptor-test/**</exclude>
</excludes>
</configuration>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ void extractAndAddToAssembler(@NotNull BundleSlingInitialContentExtractContext c
@NotNull Set<SlingInitialContentBundleEntryMetaData> collectedSlingInitialContentBundleEntries) throws IOException, ConverterException {

String repositoryPath = slingInitialContentBundleEntryMetaData.getRepositoryPath();
File file = slingInitialContentBundleEntryMetaData.getTargetFile();
File file = slingInitialContentBundleEntryMetaData.getSourceFile();
PathEntry pathEntryValue = slingInitialContentBundleEntryMetaData.getPathEntry();
// all entry paths used by entry handlers start with "/"
String contentPackageEntryPath = SLASH + org.apache.jackrabbit.vault.util.Constants.ROOT_DIR + PlatformNameFormat.getPlatformPath(repositoryPath);
Expand All @@ -92,16 +92,12 @@ void extractAndAddToAssembler(@NotNull BundleSlingInitialContentExtractContext c

repositoryPath = FilenameUtils.removeExtension(repositoryPath);
boolean isFileDescriptorEntry = isFileDescriptor(collectedSlingInitialContentBundleEntries, contentPackageEntryPath);
VaultContentXMLContentCreator contentCreator = new VaultContentXMLContentCreator(StringUtils.substringBeforeLast(repositoryPath, "/"), docViewOutput, context.getNamespaceRegistry(), packageAssembler, isFileDescriptorEntry);


if (file.getName().endsWith(".xml")) {
contentCreator.setIsXmlProcessed();
}
VaultContentXMLContentCreator contentCreator = new VaultContentXMLContentCreator(repositoryPath, docViewOutput, context.getNamespaceRegistry(), packageAssembler, isFileDescriptorEntry);

contentReader.parse(file.toURI().toURL(), contentCreator);
contentPackageEntryPath = new ContentPackageEntryPathComputer(collectedSlingInitialContentBundleEntries, contentPackageEntryPath, contentCreator).compute();
// finish is not always called through the ContentReader unfortunately
contentCreator.finish();
contentPackageEntryPath = contentCreator.getContentPackageEntryPath();

} catch (IOException e) {
throw new IOException("Can not parse " + file, e);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with this
* work for additional information regarding copyright ownership. The ASF
* licenses this file to You under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package org.apache.sling.feature.cpconverter.handlers.slinginitialcontent;

import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import javax.jcr.NamespaceException;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;

import org.apache.jackrabbit.spi.Name;
import org.apache.jackrabbit.spi.commons.conversion.NameResolver;
import org.apache.jackrabbit.spi.commons.name.NameConstants;
import org.apache.jackrabbit.spi.commons.namespace.NamespaceResolver;
import org.apache.jackrabbit.vault.util.DocViewNode2;
import org.apache.jackrabbit.vault.util.DocViewProperty2;

/**
* Represents a serializable docview node inside a tree hierarchy.
*
*/
public class DocViewTreeNode {

private final List<DocViewTreeNode> children;
private final Name name;
private final String parentPath;
private final Collection<DocViewProperty2> properties;

public DocViewTreeNode(String parentPath, Name name, Collection<DocViewProperty2> properties) {
children = new LinkedList<>();
this.name = name;
this.properties = properties;
this.parentPath = parentPath;
}

public void addChild(DocViewTreeNode child) {
children.add(child);
}

void write(XMLStreamWriter writer, NamespaceResolver nsResolver) throws NamespaceException, XMLStreamException {
write(writer, nsResolver, Collections.emptyList());
}

public void write(XMLStreamWriter writer, NamespaceResolver nsResolver, Iterable<String> nsPrefixes) throws NamespaceException, XMLStreamException {
DocViewNode2 docViewNode = new DocViewNode2(name, properties);
docViewNode.writeStart(writer, nsResolver, nsPrefixes);
for (DocViewTreeNode child : children) {
child.write(writer, nsResolver);
}
DocViewNode2.writeEnd(writer);
}

public Collection<DocViewProperty2> getProperties() {
return properties;
}

/**
*
* @param nameResolver
* @return the absolute JCR path (in qualified form) of the node represented by this class
* @throws NamespaceException
*/
public String getPath(NameResolver nameResolver) throws NamespaceException {
if (NameConstants.JCR_ROOT.equals(name)) {
return parentPath;
} else {
return parentPath + "/" + nameResolver.getJCRName(name);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,33 +23,45 @@
import java.util.Objects;

/**
* Holds BundleEntry MetaData.
* Holds Initial Content meta data for a single file
*/
class SlingInitialContentBundleEntryMetaData {

private final File targetFile;
private final File sourceFile;
private final PathEntry pathEntry;
private final String repositoryPath;

SlingInitialContentBundleEntryMetaData(
@NotNull File targetFile,
@NotNull File sourceFile,
@NotNull PathEntry pathEntry,
@NotNull String repositoryPath) {
this.targetFile = targetFile;
this.sourceFile = sourceFile;
this.pathEntry = pathEntry;
this.repositoryPath = repositoryPath;
}

/**
*
* @return the initial content file which is supposed to be installed to {@link #getRepositoryPath()}
*/
@NotNull
File getTargetFile() {
return targetFile;
File getSourceFile() {
return sourceFile;
}

/**
*
* @return the path entry covering the file {@link #sourceFile} (maybe for a parent)
*/
@NotNull
PathEntry getPathEntry() {
return pathEntry;
}

/**
*
* @return the absolute target repository root path of the initial content inside {@link #sourceFile}
*/
@NotNull
String getRepositoryPath() {
return repositoryPath;
Expand All @@ -65,7 +77,7 @@ public boolean equals(Object o) {

@Override
public int hashCode() {
return Objects.hash(targetFile, pathEntry, repositoryPath);
return Objects.hash(sourceFile, pathEntry, repositoryPath);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,15 +181,15 @@ private boolean jarEntryIsSlingInitialContent(@NotNull BundleSlingInitialContent

@NotNull
private SlingInitialContentBundleEntryMetaData createSlingInitialContentBundleEntry(@NotNull BundleSlingInitialContentExtractContext context,
@NotNull File targetFile) throws UnsupportedEncodingException {
final String entryName = StringUtils.replace(StringUtils.substringAfter(targetFile.getPath(), basePath + File.separator), File.separator, ZIP_ENTRY_SEPARATOR);
@NotNull File sourceFile) throws UnsupportedEncodingException {
final String entryName = StringUtils.replace(StringUtils.substringAfter(sourceFile.getPath(), basePath + File.separator), File.separator, ZIP_ENTRY_SEPARATOR);
final PathEntry pathEntryValue = context.getPathEntryList().stream().filter(
pathEntry -> checkIfPathStartsWithOrIsEqual(pathEntry.getPath(), entryName, ZIP_ENTRY_SEPARATOR)
).findFirst().orElseThrow(NullPointerException::new);
final String target = pathEntryValue.getTarget();
// https://sling.apache.org/documentation/bundles/content-loading-jcr-contentloader.html#file-name-escaping
String repositoryPath = (target != null ? target : "/") + URLDecoder.decode(entryName.substring(pathEntryValue.getPath().length()), "UTF-8");
return new SlingInitialContentBundleEntryMetaData(targetFile, pathEntryValue, repositoryPath);
return new SlingInitialContentBundleEntryMetaData(sourceFile, pathEntryValue, repositoryPath);
}


Expand Down

0 comments on commit 084e751

Please sign in to comment.