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  
18  package org.apache.any23.writer;
19  
20  import org.apache.any23.extractor.ExtractionContext;
21  import org.eclipse.rdf4j.model.Resource;
22  import org.eclipse.rdf4j.model.IRI;
23  import org.eclipse.rdf4j.model.Value;
24  
25  import java.util.ArrayList;
26  import java.util.Collection;
27  import java.util.Collections;
28  
29  /**
30   * A {@link TripleHandler} multi decorator, that wraps zero or more
31   * other triple handlers and dispatches all events to each of them.
32   *
33   * @author Richard Cyganiak (richard@cyganiak.de)
34   */
35  public class CompositeTripleHandler implements TripleHandler {
36  
37      private Collection<TripleHandler> children = new ArrayList<TripleHandler>();
38  
39      /**
40       * Constructor with empty decorated list.
41       */
42      public CompositeTripleHandler() {
43          this(Collections.<TripleHandler>emptyList());
44      }
45  
46      /**
47       * Constructor with initial list of decorated handlers.
48       * 
49       * @param children list of decorated handlers. 
50       */
51      public CompositeTripleHandler(Collection<TripleHandler> children) {
52          this.children.addAll(children);
53      }
54  
55      /**
56       * Adds a decorated handler.
57       *
58       * @param child the decorated handler.
59       */
60      public void addChild(TripleHandler child) {
61          children.add(child);
62      }
63  
64      public Collection<TripleHandler> getChilds() {
65          return children;
66      }
67  
68      public void startDocument(IRI documentIRI) throws TripleHandlerException {
69          for (TripleHandler handler : children) {
70              handler.startDocument(documentIRI);
71          }
72      }
73  
74      public void openContext(ExtractionContext context) throws TripleHandlerException {
75          for (TripleHandler handler : children) {
76              handler.openContext(context);
77          }
78      }
79  
80      public void closeContext(ExtractionContext context) throws TripleHandlerException {
81          for (TripleHandler handler : children) {
82              handler.closeContext(context);
83          }
84      }
85  
86      public void receiveTriple(Resource s, IRI p, Value o, IRI g, ExtractionContext context)
87      throws TripleHandlerException {
88          for (TripleHandler handler : children) {
89              handler.receiveTriple(s, p, o, g, context);
90          }
91      }
92  
93      public void receiveNamespace(String prefix, String uri, ExtractionContext context)
94      throws TripleHandlerException {
95          for (TripleHandler handler : children) {
96              handler.receiveNamespace(prefix, uri, context);
97          }
98      }
99  
100     public void close() throws TripleHandlerException {
101         for (TripleHandler handler : children) {
102             handler.close();
103         }
104     }
105 
106     public void endDocument(IRI documentIRI) throws TripleHandlerException {
107         for (TripleHandler handler : children) {
108             handler.endDocument(documentIRI);
109         }
110     }
111 
112     public void setContentLength(long contentLength) {
113         for (TripleHandler handler : children) {
114             handler.setContentLength(contentLength);
115         }
116     }
117     
118 }