Skip to content

Commit

Permalink
Merge pull request #174 from openpreserve/feat/detect-extended-conf
Browse files Browse the repository at this point in the history
FEAT: Record used namespaces
  • Loading branch information
carlwilson authored Aug 23, 2024
2 parents 61c1dd1 + 5955736 commit fed724e
Show file tree
Hide file tree
Showing 34 changed files with 395 additions and 86 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ pom.xml.next
release.properties
**/pom.xml.versionsBackup

# Ignore generated project documentation folders
odf-*/docs

# Ignore Eclipse artefacts
**/.settings
**/.project
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# ODF Validator

Latest version is 0.12.0.
Latest version is 0.13.0.

## About

Expand Down
2 changes: 1 addition & 1 deletion docs/developer/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ To include the core validation library in your project, add the following depend
<dependency>
<groupId>org.openpreservation.odf</groupId>
<artifactId>odf-core</artifactId>
<version>0.12.0</version>
<version>0.13.0</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion odf-apps/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.openpreservation.odf</groupId>
<artifactId>odf-validator</artifactId>
<version>0.12.0</version>
<version>0.13.0</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collection;
Expand Down
2 changes: 1 addition & 1 deletion odf-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.openpreservation.odf</groupId>
<artifactId>odf-validator</artifactId>
<version>0.12.0</version>
<version>0.13.0</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.openpreservation.format.xml;

import java.util.List;
import java.util.Set;

import org.openpreservation.messages.Message;

Expand Down Expand Up @@ -29,9 +30,16 @@ public interface ParseResult {
/**
* Get all of the declared <code>Namespace</code>s in the document
*
* @return the <code>List<Namespace></code> of the document's namespaces
* @return the <code>Set<Namespace></code> of the document's namespaces
*/
public List<Namespace> getNamespaces();
public Set<Namespace> getDeclaredNamespaces();

/**
* Get all of the declared <code>Namespace</code>s in the document
*
* @return the <code>Set<Namespace></code> of the document's namespaces
*/
public Set<Namespace> getUsedNamespaces();

/**
* Get the root element namespace prefix
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package org.openpreservation.format.xml;

import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;

import org.openpreservation.messages.Message;
import org.openpreservation.messages.Message.Severity;
Expand All @@ -11,28 +14,35 @@
final class ParseResultImpl implements ParseResult {
private static final String MESSAGES_NAME = "messages";
private static final String MESSAGES_TYPE = "List<Message>";
static ParseResult of(final boolean isWellFormed, final Namespace rootNamespace, final List<Namespace> namespaces,

static ParseResult of(final boolean isWellFormed, final Namespace rootNamespace,
final Collection<Namespace> declaredNamespaces, final Collection<Namespace> usedNamespaces,
final String rootPrefix, final String rootName,
final List<Attribute> rootAttributes, final List<Message> messages) {
Objects.requireNonNull(namespaces, String.format(Checks.NOT_NULL, "namespaces", "List<Namespace>"));
Objects.requireNonNull(declaredNamespaces, String.format(Checks.NOT_NULL, "declaredNamespaces", "List<Namespace>"));
Objects.requireNonNull(usedNamespaces, String.format(Checks.NOT_NULL, "usedNamespaces", "List<Namespace>"));
Objects.requireNonNull(messages, String.format(Checks.NOT_NULL, MESSAGES_NAME, MESSAGES_TYPE));
return new ParseResultImpl(isWellFormed, rootNamespace, namespaces, rootPrefix, rootName, rootAttributes,
return new ParseResultImpl(isWellFormed, rootNamespace, declaredNamespaces, usedNamespaces, rootPrefix,
rootName, rootAttributes,
messages);
}

static ParseResult of(final Namespace rootNamespace, final List<Namespace> namespaces,
static ParseResult of(final Namespace rootNamespace, final Collection<Namespace> declaredNamespaces,
final Collection<Namespace> usedNamespaces,
final String rootPrefix, final String rootName,
final List<Attribute> rootAttributes, final List<Message> messages) {
Objects.requireNonNull(messages, String.format(Checks.NOT_NULL, MESSAGES_NAME, MESSAGES_TYPE));
return ParseResultImpl.of(isWellFormed(messages), rootNamespace, namespaces, rootPrefix, rootName,
return ParseResultImpl.of(isWellFormed(messages), rootNamespace, declaredNamespaces, usedNamespaces, rootPrefix,
rootName,
rootAttributes,
messages);
}

static ParseResult invertWellFormed(final ParseResult parseResult) {
Objects.requireNonNull(parseResult, String.format(Checks.NOT_NULL, "parseResult", "ParseResult"));
return new ParseResultImpl(!parseResult.isWellFormed(), parseResult.getRootNamespace(),
parseResult.getNamespaces(), parseResult.getRootPrefix(), parseResult.getRootName(),
parseResult.getDeclaredNamespaces(), parseResult.getUsedNamespaces(), parseResult.getRootPrefix(),
parseResult.getRootName(),
parseResult.getRootAttributes(),
parseResult.getMessages());
}
Expand All @@ -48,19 +58,22 @@ private static final boolean isWellFormed(final List<Message> messages) {

private final boolean isWF;
private final Namespace rootNamespace;
private final List<Namespace> namespaces;
private final Set<Namespace> usedNamespaces;
private final Set<Namespace> declaredNamespaces;
private final String rootPrefix;
private final String rootName;
private final List<Attribute> rootAttributes;

private final List<Message> messages;

private ParseResultImpl(final boolean isWellFormed, final Namespace rootNamespace, final List<Namespace> namespaces,
private ParseResultImpl(final boolean isWellFormed, final Namespace rootNamespace,
final Collection<Namespace> declaredNamespaces, final Collection<Namespace> usedNamespaces,
final String rootPrefix, final String rootName, final List<Attribute> rootAttributes,
final List<Message> messages) {
this.isWF = isWellFormed;
this.rootNamespace = rootNamespace;
this.namespaces = Collections.unmodifiableList(namespaces);
this.declaredNamespaces = Collections.unmodifiableSet(new HashSet<>(declaredNamespaces));
this.usedNamespaces = Collections.unmodifiableSet(new HashSet<>(usedNamespaces));
this.rootPrefix = rootPrefix;
this.rootName = rootName;
this.rootAttributes = Collections.unmodifiableList(rootAttributes);
Expand All @@ -78,8 +91,13 @@ public Namespace getRootNamespace() {
}

@Override
public List<Namespace> getNamespaces() {
return this.namespaces;
public Set<Namespace> getDeclaredNamespaces() {
return this.declaredNamespaces;
}

@Override
public Set<Namespace> getUsedNamespaces() {
return this.usedNamespaces;
}

@Override
Expand All @@ -93,7 +111,8 @@ public boolean isRootName(final String name) {
if (this.rootName == null || (name.contains(":") && this.rootPrefix == null)) {
return false;
}
final String match = (name.contains(":")) ? String.format("%s:%s", this.rootPrefix, this.rootName) : this.rootName;
final String match = (name.contains(":")) ? String.format("%s:%s", this.rootPrefix, this.rootName)
: this.rootName;
return match.equals(name);
}

Expand All @@ -118,7 +137,8 @@ public int hashCode() {
int result = 1;
result = prime * result + (isWF ? 1231 : 1237);
result = prime * result + ((rootNamespace == null) ? 0 : rootNamespace.hashCode());
result = prime * result + ((namespaces == null) ? 0 : namespaces.hashCode());
result = prime * result + ((declaredNamespaces == null) ? 0 : declaredNamespaces.hashCode());
result = prime * result + ((usedNamespaces == null) ? 0 : usedNamespaces.hashCode());
result = prime * result + ((rootPrefix == null) ? 0 : rootPrefix.hashCode());
result = prime * result + ((rootName == null) ? 0 : rootName.hashCode());
result = prime * result + ((rootAttributes == null) ? 0 : rootAttributes.hashCode());
Expand All @@ -142,10 +162,15 @@ public boolean equals(final Object obj) {
return false;
} else if (!rootNamespace.equals(other.rootNamespace))
return false;
if (namespaces == null) {
if (other.namespaces != null)
if (declaredNamespaces == null) {
if (other.declaredNamespaces != null)
return false;
} else if (!declaredNamespaces.equals(other.declaredNamespaces))
return false;
if (usedNamespaces == null) {
if (other.usedNamespaces != null)
return false;
} else if (!namespaces.equals(other.namespaces))
} else if (!usedNamespaces.equals(other.usedNamespaces))
return false;
if (rootPrefix == null) {
if (other.rootPrefix != null)
Expand All @@ -172,7 +197,8 @@ public boolean equals(final Object obj) {

@Override
public String toString() {
return "ParseResultImpl [isWF=" + isWF + ", rootNamespace=" + rootNamespace + ", namespaces=" + namespaces
return "ParseResultImpl [isWF=" + isWF + ", rootNamespace=" + rootNamespace + ", namespaces="
+ declaredNamespaces
+ ", rootPrefix=" + rootPrefix + ", rootName=" + rootName + ", rootAttributes=" + rootAttributes
+ ", messages=" + messages + "]";
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package org.openpreservation.format.xml;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.openpreservation.messages.Message;
import org.xml.sax.Attributes;
import org.xml.sax.helpers.DefaultHandler;

public class ParsingHandler extends DefaultHandler {
private Namespace rootNamespace = null;
private List<Namespace> namespaces = new ArrayList<>();
private Set<Namespace> declaredNamespaces = new HashSet<>();
private Set<Namespace> usedNamespaces = new HashSet<>();
private String rootPrefix = "";
private String rootLocalName = "";
private List<Attribute> attributes = new ArrayList<>();
Expand All @@ -19,22 +22,29 @@ public ParsingHandler() {
}

public ParseResult getResult(final boolean isWellFormed, final List<Message> messages) {
return ParseResultImpl.of(isWellFormed, this.rootNamespace, this.namespaces, this.rootPrefix,
return ParseResultImpl.of(isWellFormed, this.rootNamespace, this.declaredNamespaces, this.usedNamespaces, this.rootPrefix,
this.rootLocalName, this.attributes, messages);
}

@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) {
if (this.rootLocalName.isEmpty()) {
this.rootLocalName = localName;
this.rootPrefix = qName.contains(":") ? qName.split(":")[0] : "";
this.rootPrefix = splitNamespace(qName);
this.attributes = AttributeImpl.of(attributes);
this.rootNamespace = NamespaceImpl.of(uri, this.rootPrefix);
this.usedNamespaces.add(NamespaceImpl.of(uri, this.rootPrefix));
} else {
this.usedNamespaces.add(NamespaceImpl.of(uri, splitNamespace(qName)));
}
}

private static final String splitNamespace(final String qName) {
return qName.contains(":") ? qName.split(":")[0] : "";
}

@Override
public void startPrefixMapping(String prefix, String uri) {
this.namespaces.add(NamespaceImpl.of(uri, prefix));
this.declaredNamespaces.add(NamespaceImpl.of(uri, prefix));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Set;

import org.openpreservation.messages.Message;
import org.openpreservation.utils.Checks;
Expand Down Expand Up @@ -58,8 +59,13 @@ public Namespace getRootNamespace() {
}

@Override
public List<Namespace> getNamespaces() {
return this.parseResult.getNamespaces();
public Set<Namespace> getDeclaredNamespaces() {
return this.parseResult.getDeclaredNamespaces();
}

@Override
public Set<Namespace> getUsedNamespaces() {
return this.parseResult.getUsedNamespaces();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ public final class ValidationResults {
* values
*/
public static final ParseResult parseResultOf(final boolean valid, final Namespace namespace,
final List<Namespace> namespaces,
final List<Namespace> declareNamespaces, final List<Namespace> usedNamespaces,
final String prefix, final String name, final List<Attribute> attributes, final List<Message> messages) {
return ParseResultImpl.of(valid, namespace, namespaces, prefix, name, attributes, messages);
return ParseResultImpl.of(valid, namespace, declareNamespaces, usedNamespaces, prefix, name, attributes, messages);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import java.util.function.Function;
import java.util.stream.Collectors;

import org.apache.commons.compress.archivers.zip.UnsupportedZipFeatureException;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipFile;
import org.openpreservation.utils.Checks;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import java.util.Date;

import org.openpreservation.messages.Message.Severity;

/**
* Defines behaviour of validation messages.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
import javax.xml.parsers.ParserConfigurationException;

import org.apache.commons.compress.archivers.zip.UnsupportedZipFeatureException;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipFile;
import org.openpreservation.format.xml.ParseResult;
import org.openpreservation.format.xml.XmlParser;
import org.openpreservation.format.zip.ZipArchiveCache;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package org.openpreservation.odf.validation;

import java.io.IOException;

import org.openpreservation.messages.MessageLog;
import org.openpreservation.messages.Message.Severity;
import org.openpreservation.messages.MessageLog;
import org.openpreservation.odf.pkg.OdfPackage;
import org.openpreservation.odf.pkg.PackageParser.ParseException;
import org.openpreservation.odf.xml.OdfXmlDocument;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ private EmbeddedObjectsRule(final String id, final String name, final String des

@Override
public MessageLog check(final OdfXmlDocument document) throws ParseException {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'check'");
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.openpreservation.odf.validation.rules;

import java.io.IOException;
import java.util.Objects;

import org.openpreservation.messages.Message.Severity;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.openpreservation.odf.validation.rules;

import java.io.IOException;
import java.util.Objects;

import org.openpreservation.messages.Message.Severity;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ private MacroRule(final String id, final String name, final String description,

@Override
public MessageLog check(final OdfXmlDocument document) throws ParseException {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'check'");
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.openpreservation.odf.validation.rules;

import java.io.IOException;
import java.util.Objects;

import org.openpreservation.messages.Message.Severity;
Expand Down
Loading

0 comments on commit fed724e

Please sign in to comment.