Skip to content
Merged
253 changes: 129 additions & 124 deletions CHANGES.txt

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,21 @@ public FileNode dereference(OneNoteDocument document) throws TikaException {
if (nodeListPositions.isEmpty()) {
return null;
}
if (nodeListPositions.get(0) >= document.root.children.size()) {
int rootPosition = nodeListPositions.get(0);
if (rootPosition < 0 || rootPosition >= document.root.children.size()) {
throw new TikaException("Exceeded root child size");
}
FileNode cur = document.root.children.get(nodeListPositions.get(0));
FileNode cur = document.root.children.get(rootPosition);
for (int i = 1, ie = nodeListPositions.size(); i < ie; ++i) {
cur = cur.childFileNodeList.children.get(nodeListPositions.get(i));
if (cur == null || cur.childFileNodeList == null ||
cur.childFileNodeList.children == null) {
throw new TikaException("Missing child file node list");
}
int childPosition = nodeListPositions.get(i);
if (childPosition < 0 || childPosition >= cur.childFileNodeList.children.size()) {
throw new TikaException("Exceeded child file node size");
}
cur = cur.childFileNodeList.children.get(childPosition);
}
return cur;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.List;
import java.util.Locale;

import org.apache.tika.exception.TikaException;
import org.apache.tika.parser.microsoft.onenote.fsshttpb.util.BitConverter;
import org.apache.tika.utils.StringUtils;

Expand All @@ -41,13 +42,26 @@ public GUID(int[] guid) {
* -EEEEEEEEEEEE}
* @return GUID object parsed from guid bytes.
*/
public static GUID fromCurlyBraceUTF16Bytes(byte[] guid) {
public static GUID fromCurlyBraceUTF16Bytes(byte[] guid) throws TikaException {
int[] intGuid = new int[16];
String utf16Str = new String(guid, StandardCharsets.UTF_16LE).replaceAll("\\{", "")
.replaceAll("-", "").replaceAll("}", "");
for (int i = 0; i < utf16Str.length(); i += 2) {
intGuid[i / 2] =
Integer.parseUnsignedInt("" + utf16Str.charAt(i) + utf16Str.charAt(i + 1), 16);
String utf16Str = new String(guid, StandardCharsets.UTF_16LE);
if (utf16Str.length() != 38 || utf16Str.charAt(0) != '{' ||
utf16Str.charAt(9) != '-' || utf16Str.charAt(14) != '-' ||
utf16Str.charAt(19) != '-' || utf16Str.charAt(24) != '-' ||
utf16Str.charAt(37) != '}') {
throw new TikaException("Invalid GUID string");
}
String hex = utf16Str.substring(1, 37).replace("-", "");
if (hex.length() != 32) {
throw new TikaException("Invalid GUID string");
}
for (int i = 0; i < hex.length(); i += 2) {
int high = Character.digit(hex.charAt(i), 16);
int low = Character.digit(hex.charAt(i + 1), 16);
if (high < 0 || low < 0) {
throw new TikaException("Invalid GUID string");
}
intGuid[i / 2] = (high << 4) | low;
}
return new GUID(intGuid);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,23 @@
import java.io.IOException;
import java.nio.file.Files;
import java.time.Instant;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.tuple.Pair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;

import org.apache.tika.annotation.TikaComponent;
import org.apache.tika.exception.EmbeddedLimitReachedException;
import org.apache.tika.exception.TikaException;
import org.apache.tika.exception.WriteLimitReachedException;
import org.apache.tika.io.TikaInputStream;
import org.apache.tika.metadata.Metadata;
import org.apache.tika.metadata.OneNote;
Expand All @@ -52,6 +57,7 @@
public class OneNoteParser implements Parser {

public static final String ONE_NOTE_PREFIX = "onenote:";
private static final Logger LOG = LoggerFactory.getLogger(OneNoteParser.class);
private static final Map<MediaType, List<String>> TYPES_MAP = new HashMap<>();
/**
* Serial version UID
Expand Down Expand Up @@ -131,15 +137,15 @@ public void parse(TikaInputStream tis, ContentHandler handler, Metadata metadata

if (!oneNoteTreeWalker.getAuthors().isEmpty()) {
metadata.set(TikaCoreProperties.CREATOR,
oneNoteTreeWalker.getAuthors().toArray(new String[]{}));
sortedValues(oneNoteTreeWalker.getAuthors()));
}
if (!oneNoteTreeWalker.getMostRecentAuthors().isEmpty()) {
metadata.set(OneNote.MOST_RECENT_AUTHORS,
oneNoteTreeWalker.getMostRecentAuthors().toArray(new String[]{}));
sortedValues(oneNoteTreeWalker.getMostRecentAuthors()));
}
if (!oneNoteTreeWalker.getOriginalAuthors().isEmpty()) {
metadata.set(OneNote.ORIGINAL_AUTHORS,
oneNoteTreeWalker.getOriginalAuthors().toArray(new String[]{}));
sortedValues(oneNoteTreeWalker.getOriginalAuthors()));
}
if (!Instant.MAX.equals(
Instant.ofEpochMilli(oneNoteTreeWalker.getCreationTimestamp()))) {
Expand All @@ -165,8 +171,20 @@ public void parse(TikaInputStream tis, ContentHandler handler, Metadata metadata
MSOneStorePackage pkg =
onenoteParser.parse(alternatePackageOneStoreFile.dataElementPackage);

pkg.walkTree(options, metadata, xhtml);
pkg.walkTree(options, metadata, xhtml, context);
} catch (Exception e) {
WriteLimitReachedException.throwIfWriteLimitReached(e);
if (e instanceof EmbeddedLimitReachedException) {
throw (EmbeddedLimitReachedException) e;
}
String failure = e.getMessage() == null ? e.getClass().getSimpleName() :
e.getMessage();
LOG.warn("OneNote FSSHTTPB parse failed; falling back to legacy text dump: {}",
failure);
LOG.debug("OneNote FSSHTTPB parse failure", e);
metadata.add(TikaCoreProperties.TIKA_META_EXCEPTION_WARNING,
"OneNote FSSHTTPB parse failed; falling back to legacy text dump: " +
failure);
OneNoteLegacyDumpStrings dumpStrings =
new OneNoteLegacyDumpStrings(oneNoteDirectFileResource, xhtml);
dumpStrings.dump();
Expand All @@ -180,6 +198,12 @@ public void parse(TikaInputStream tis, ContentHandler handler, Metadata metadata

}

private static String[] sortedValues(Set<String> values) {
String[] sorted = values.toArray(new String[0]);
Arrays.sort(sorted);
return sorted;
}

/**
* Create a OneNoteDocument object.
* <p>
Expand Down
Loading
Loading