Permalink
Browse files

#ODFTOOLKIT-50# Add ODF 1.2 RDF metadata support to ODF Toolkit - dev…

…eloper:Tao Lin, Svante Schubert, Dali Liu

git-svn-id: https://svn.apache.org/repos/asf/incubator/odf/trunk@1418326 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information...
1 parent 2ad0dae commit ebb9586e4043e418f24517674fbd4f5bc587a683 @svanteschubert svanteschubert committed Dec 7, 2012
Showing with 10,849 additions and 7,266 deletions.
  1. +638 −614 odfdom/pom.xml
  2. +496 −395 odfdom/src/main/java/org/odftoolkit/odfdom/dom/OdfSchemaDocument.java
  3. +526 −504 odfdom/src/main/java/org/odftoolkit/odfdom/dom/element/table/TableTableCellElementBase.java
  4. +232 −223 odfdom/src/main/java/org/odftoolkit/odfdom/dom/element/text/TextBookmarkStartElement.java
  5. +2,079 −2,067 odfdom/src/main/java/org/odftoolkit/odfdom/dom/element/text/TextMetaElement.java
  6. +302 −279 odfdom/src/main/java/org/odftoolkit/odfdom/dom/element/text/TextParagraphElementBase.java
  7. +320 −0 odfdom/src/main/java/org/odftoolkit/odfdom/dom/rdfa/BookmarkRDFMetadataExtractor.java
  8. +685 −591 odfdom/src/main/java/org/odftoolkit/odfdom/pkg/OdfFileDom.java
  9. +176 −152 odfdom/src/main/java/org/odftoolkit/odfdom/pkg/OdfFileSaxHandler.java
  10. +2,107 −2,106 odfdom/src/main/java/org/odftoolkit/odfdom/pkg/OdfPackage.java
  11. +141 −64 odfdom/src/main/java/org/odftoolkit/odfdom/pkg/OdfPackageDocument.java
  12. +377 −0 odfdom/src/main/java/org/odftoolkit/odfdom/pkg/OdfXMLHelper.java
  13. +104 −0 odfdom/src/main/java/org/odftoolkit/odfdom/pkg/manifest/OdfManifestDom.java
  14. +92 −0 odfdom/src/main/java/org/odftoolkit/odfdom/pkg/rdfa/DOMAttributes.java
  15. +92 −0 odfdom/src/main/java/org/odftoolkit/odfdom/pkg/rdfa/DOMRDFaParser.java
  16. +198 −0 odfdom/src/main/java/org/odftoolkit/odfdom/pkg/rdfa/EvalContext.java
  17. +165 −0 odfdom/src/main/java/org/odftoolkit/odfdom/pkg/rdfa/JenaSink.java
  18. +124 −0 odfdom/src/main/java/org/odftoolkit/odfdom/pkg/rdfa/MultiContentHandler.java
  19. +443 −0 odfdom/src/main/java/org/odftoolkit/odfdom/pkg/rdfa/RDFaParser.java
  20. +169 −0 odfdom/src/main/java/org/odftoolkit/odfdom/pkg/rdfa/SAXRDFaParser.java
  21. +77 −0 odfdom/src/main/java/org/odftoolkit/odfdom/pkg/rdfa/URIExtractor.java
  22. +217 −0 odfdom/src/main/java/org/odftoolkit/odfdom/pkg/rdfa/URIExtractorImpl.java
  23. +145 −0 odfdom/src/main/java/org/odftoolkit/odfdom/pkg/rdfa/Util.java
  24. +304 −0 odfdom/src/main/resources/grddl/odf2rdf.xsl
  25. +84 −0 odfdom/src/test/java/org/odftoolkit/odfdom/pkg/GRDDLTest.java
  26. +280 −0 odfdom/src/test/java/org/odftoolkit/odfdom/pkg/RDFMetadataTest.java
  27. +115 −113 odfdom/src/test/java/org/odftoolkit/odfdom/utils/ResourceUtilities.java
  28. BIN odfdom/src/test/resources/test_rdfmeta.odt
  29. +20 −20 pom.xml
  30. +141 −138 simple/src/test/java/org/odftoolkit/simple/utils/ResourceUtilities.java
View

Large diffs are not rendered by default.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -0,0 +1,320 @@
+/************************************************************************
+ *
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
+ *
+ * Copyright 2008, 2010 Oracle and/or its affiliates. All rights reserved.
+ *
+ * Use is subject to license terms.
+ *
+ * Licensed 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. You can also
+ * obtain a copy of the License at http://odftoolkit.org/docs/license.txt
+ *
+ * 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.odftoolkit.odfdom.dom.rdfa;
+
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+
+import javax.xml.stream.XMLEventFactory;
+import javax.xml.stream.events.Attribute;
+import javax.xml.stream.events.StartElement;
+
+import org.odftoolkit.odfdom.dom.DefaultElementVisitor;
+import org.odftoolkit.odfdom.dom.OdfDocumentNamespace;
+import org.odftoolkit.odfdom.dom.element.text.TextBookmarkEndElement;
+import org.odftoolkit.odfdom.dom.element.text.TextBookmarkStartElement;
+import org.odftoolkit.odfdom.pkg.OdfElement;
+import org.odftoolkit.odfdom.pkg.OdfFileDom;
+import org.odftoolkit.odfdom.pkg.rdfa.DOMAttributes;
+import org.odftoolkit.odfdom.pkg.rdfa.JenaSink;
+import org.w3c.dom.Node;
+import org.xml.sax.Attributes;
+
+import com.hp.hpl.jena.rdf.model.Model;
+import com.hp.hpl.jena.rdf.model.ModelFactory;
+import com.hp.hpl.jena.rdf.model.Property;
+import com.hp.hpl.jena.rdf.model.Resource;
+
+/**
+ * This is a sub class of <code>DefaultElementVisitor</code>, which is used to
+ * extract metadata from {@odf.element text:bookmark-start} to
+ * {@odf.element text:bookmark-end} pair.
+ */
+public class BookmarkRDFMetadataExtractor extends DefaultElementVisitor {
+
+ protected static final char NewLineChar = '\n';
+ protected static final char TabChar = '\t';
+ private TextBookmarkStartElement bookmarkstart;
+ private boolean found;
+ protected final Map<TextBookmarkStartElement, ExtractorStringBuilder> builderMap;
+ protected final Map<TextBookmarkStartElement, String> stringMap;
+
+ private XMLEventFactory eventFactory = XMLEventFactory.newInstance();
+
+ private JenaSink sink;
+
+ /**
+ * This class is used to provide the string builder functions to extractor.
+ * It will automatically process the last NewLineChar.
+ *
+ * @since 0.3.5
+ */
+ protected static class ExtractorStringBuilder {
+ private StringBuilder mBuilder;
+ private boolean lastAppendNewLine;
+
+ ExtractorStringBuilder() {
+ mBuilder = new StringBuilder();
+ lastAppendNewLine = false;
+ }
+
+ /**
+ * Append a string
+ *
+ * @param str
+ * - the string
+ */
+ public void append(String str) {
+ mBuilder.append(str);
+ }
+
+ /**
+ * Append a character
+ *
+ * @param ch
+ * - the character
+ */
+ public void append(char ch) {
+ mBuilder.append(ch);
+ }
+
+ /**
+ * Append a new line character at the end
+ */
+ public void appendLine() {
+ mBuilder.append(NewLineChar);
+ lastAppendNewLine = true;
+ }
+
+ /**
+ * Return the string value.
+ * <p>
+ * If the last character is a new line character and is appended with
+ * appendLine(), the last new line character will be removed.
+ */
+ public String toString() {
+ if (lastAppendNewLine) {
+ mBuilder.deleteCharAt(mBuilder.length() - 1);
+ }
+ return mBuilder.toString();
+ }
+ }
+
+ /**
+ * Create a BookmarkRDFMetadataExtractor instance, which RDF metadata
+ * content of bookmarks can be extracted by
+ * <code>getBookmarkRDFMetadata()</code>.
+ *
+ * @param element
+ * the ODF element whose text will be extracted.
+ * @return an instance of BookmarkRDFMetadataExtractor
+ */
+ public static BookmarkRDFMetadataExtractor newBookmarkTextExtractor() {
+ return new BookmarkRDFMetadataExtractor();
+ }
+
+ /**
+ * Return the RDF metadata of specified ODF element as a Jena Model.
+ *
+ * @return the text content as a string
+ */
+ public Model getBookmarkRDFMetadata(OdfFileDom dom) {
+ this.bookmarkstart = null;
+ this.found = false;
+ this.sink = dom.getSink();
+ visit(dom.getRootElement());
+ return getModel();
+ }
+
+ public Model getBookmarkRDFMetadata(TextBookmarkStartElement bookmarkstart) {
+ this.bookmarkstart = bookmarkstart;
+ this.found = false;
+ this.sink = ((OdfFileDom) bookmarkstart.getOwnerDocument()).getSink();
+ visit(((OdfFileDom) bookmarkstart.getOwnerDocument()).getRootElement());
+ return getModel();
+ }
+
+ private Model getModel() {
+ Model m = ModelFactory.createDefaultModel();
+ for (Entry<TextBookmarkStartElement, String> entry : stringMap
+ .entrySet()) {
+ String xhtmlAbout = entry.getKey().getXhtmlAboutAttribute();
+ String xhtmlProperty = entry.getKey().getXhtmlPropertyAttribute();
+ String xhtmlContent = entry.getKey().getXhtmlContentAttribute();
+ if (xhtmlAbout != null && xhtmlProperty != null) {
+ String qname = entry.getKey().getNodeName();
+ String namespaceURI = entry.getKey().getNamespaceURI();
+ String localname = entry.getKey().getLocalName();
+ String prefix = (qname.indexOf(':') == -1) ? "" : qname
+ .substring(0, qname.indexOf(':'));
+
+ StartElement e = eventFactory.createStartElement(prefix,
+ namespaceURI, localname,
+ fromAttributes(new DOMAttributes(entry.getKey()
+ .getAttributes())), null, sink.getContext());
+
+ xhtmlAbout = sink.getExtractor().expandSafeCURIE(e, xhtmlAbout,
+ sink.getContext());
+ xhtmlProperty = sink.getExtractor().expandCURIE(e,
+ xhtmlProperty, sink.getContext());
+ Resource s = m.createResource(xhtmlAbout);
+ Property p = m.createProperty(xhtmlProperty);
+ if (xhtmlContent != null) {
+ s.addLiteral(p, xhtmlContent);
+ } else {
+ s.addLiteral(p, entry.getValue());
+ }
+
+ }
+ }
+ return m;
+ }
+
+ private Iterator fromAttributes(Attributes attributes) {
+ List toReturn = new LinkedList();
+
+ for (int i = 0; i < attributes.getLength(); i++) {
+ String qname = attributes.getQName(i);
+ String prefix = qname.contains(":") ? qname.substring(0,
+ qname.indexOf(":")) : "";
+ Attribute attr = eventFactory.createAttribute(prefix,
+ attributes.getURI(i), attributes.getLocalName(i),
+ attributes.getValue(i));
+
+ if (!qname.equals("xmlns") && !qname.startsWith("xmlns:"))
+ toReturn.add(attr);
+ }
+
+ return toReturn.iterator();
+ }
+
+ /**
+ * Constructor with an ODF element as parameter
+ *
+ * @param element
+ * the ODF element whose text would be extracted.
+ */
+ private BookmarkRDFMetadataExtractor() {
+ builderMap = new HashMap<TextBookmarkStartElement, ExtractorStringBuilder>();
+ stringMap = new HashMap<TextBookmarkStartElement, String>();
+ }
+
+ /**
+ * The end users needn't to care of this method, if you don't want to
+ * override the text content handling strategy of <code>OdfElement</code>.
+ *
+ * @see org.odftoolkit.odfdom.dom.DefaultElementVisitor#visit(org.odftoolkit.odfdom.pkg.OdfElement)
+ */
+ @Override
+ public void visit(OdfElement element) {
+ if (bookmarkstart != null && found) {
+ return;
+ }
+ if (this.bookmarkstart == null) {
+ if (element instanceof TextBookmarkStartElement) {
+ builderMap.put((TextBookmarkStartElement) element,
+ new ExtractorStringBuilder());
+ }
+
+ } else {
+ if (element == bookmarkstart) {
+ builderMap.put((TextBookmarkStartElement) element,
+ new ExtractorStringBuilder());
+ }
+ }
+ appendElementText(element);
+ if (element.getNamespaceURI()
+ .equals(OdfDocumentNamespace.META.getUri())
+ || element.getNamespaceURI().equals(
+ OdfDocumentNamespace.DC.getUri())) {
+ // textBuilderAppendLine();
+ }
+ }
+
+ /**
+ * Append the text content of this element to string buffer.
+ *
+ * @param ele
+ * the ODF element whose text will be appended.
+ */
+ private void appendElementText(OdfElement ele) {
+ Node node = ele.getFirstChild();
+ while (node != null) {
+ if (node.getNodeType() == Node.TEXT_NODE) {
+ textBuilderAppend(node.getNodeValue());
+ } else if (node.getNodeType() == Node.ELEMENT_NODE) {
+ if (node instanceof TextBookmarkEndElement) {
+ TextBookmarkEndElement end = (TextBookmarkEndElement) node;
+ endBookmark(end);
+ }
+
+ OdfElement element = (OdfElement) node;
+ element.accept(this);
+ }
+ node = node.getNextSibling();
+ }
+ }
+
+ private void textBuilderAppendLine() {
+ for (Entry<TextBookmarkStartElement, ExtractorStringBuilder> entry : builderMap
+ .entrySet()) {
+ entry.getValue().appendLine();
+ }
+ }
+
+ private void textBuilderAppend(char ch) {
+ for (Entry<TextBookmarkStartElement, ExtractorStringBuilder> entry : builderMap
+ .entrySet()) {
+ entry.getValue().append(ch);
+ }
+ }
+
+ private void textBuilderAppend(String str) {
+ for (Entry<TextBookmarkStartElement, ExtractorStringBuilder> entry : builderMap
+ .entrySet()) {
+ entry.getValue().append(str);
+ }
+ }
+
+ private void endBookmark(TextBookmarkEndElement end) {
+ TextBookmarkStartElement start = null;
+ for (Entry<TextBookmarkStartElement, ExtractorStringBuilder> entry : builderMap
+ .entrySet()) {
+ if (entry.getKey().getTextNameAttribute()
+ .equals(end.getTextNameAttribute())) {
+ start = entry.getKey();
+ break;
+ }
+ }
+ if (start != null) {
+ stringMap.put(start, builderMap.get(start).toString());
+ builderMap.remove(start);
+ if (bookmarkstart != null) {
+ found = true;
+ }
+ }
+ }
+}
Oops, something went wrong.

0 comments on commit ebb9586

Please sign in to comment.