View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.tika.parser.xml;
18  
19  import org.apache.tika.metadata.DublinCore;
20  import org.apache.tika.metadata.Metadata;
21  import org.apache.tika.sax.TeeContentHandler;
22  import org.apache.tika.sax.xpath.CompositeMatcher;
23  import org.apache.tika.sax.xpath.Matcher;
24  import org.apache.tika.sax.xpath.MatchingContentHandler;
25  import org.apache.tika.sax.xpath.XPathParser;
26  import org.xml.sax.ContentHandler;
27  
28  /**
29   * Dublin Core metadata parser
30   */
31  public class DcXMLParser extends XMLParser {
32  
33      private static final XPathParser DC_XPATH = new XPathParser(
34              "dc", "http://purl.org/dc/elements/1.1/");
35  
36      private static ContentHandler getDublinCore(
37              ContentHandler ch, Metadata md, String name, String element) {
38          Matcher matcher = new CompositeMatcher(
39                  DC_XPATH.parse("//dc:" + element),
40                  DC_XPATH.parse("//dc:" + element + "//text()"));
41          ContentHandler branch =
42              new MatchingContentHandler(new MetadataHandler(md, name), matcher);
43          return new TeeContentHandler(ch, branch);
44      }
45  
46      protected ContentHandler getContentHandler(ContentHandler ch, Metadata md) {
47          ch = super.getContentHandler(ch, md);
48          ch = getDublinCore(ch, md, DublinCore.TITLE, "title");
49          ch = getDublinCore(ch, md, DublinCore.SUBJECT, "subject");
50          ch = getDublinCore(ch, md, DublinCore.CREATOR, "creator");
51          ch = getDublinCore(ch, md, DublinCore.DESCRIPTION, "description");
52          ch = getDublinCore(ch, md, DublinCore.PUBLISHER, "publisher");
53          ch = getDublinCore(ch, md, DublinCore.CONTRIBUTOR, "contributor");
54          ch = getDublinCore(ch, md, DublinCore.DATE, "date");
55          ch = getDublinCore(ch, md, DublinCore.TYPE, "type");
56          ch = getDublinCore(ch, md, DublinCore.FORMAT, "format");
57          ch = getDublinCore(ch, md, DublinCore.IDENTIFIER, "identifier");
58          ch = getDublinCore(ch, md, DublinCore.LANGUAGE, "language");
59          ch = getDublinCore(ch, md, DublinCore.RIGHTS, "rights");
60          return ch;
61      }
62  
63  }