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 other triple handlers and dispatches all events to
31   * 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
50       *            list of decorated handlers.
51       */
52      public CompositeTripleHandler(Collection<TripleHandler> children) {
53          this.children.addAll(children);
54      }
55  
56      /**
57       * Adds a decorated handler.
58       *
59       * @param child
60       *            the decorated handler.
61       */
62      public void addChild(TripleHandler child) {
63          children.add(child);
64      }
65  
66      public Collection<TripleHandler> getChilds() {
67          return children;
68      }
69  
70      public void startDocument(IRI documentIRI) throws TripleHandlerException {
71          for (TripleHandler handler : children) {
72              handler.startDocument(documentIRI);
73          }
74      }
75  
76      public void openContext(ExtractionContext context) throws TripleHandlerException {
77          for (TripleHandler handler : children) {
78              handler.openContext(context);
79          }
80      }
81  
82      public void closeContext(ExtractionContext context) throws TripleHandlerException {
83          for (TripleHandler handler : children) {
84              handler.closeContext(context);
85          }
86      }
87  
88      public void receiveTriple(Resource s, IRI p, Value o, IRI g, ExtractionContext context)
89              throws TripleHandlerException {
90          for (TripleHandler handler : children) {
91              handler.receiveTriple(s, p, o, g, context);
92          }
93      }
94  
95      public void receiveNamespace(String prefix, String uri, ExtractionContext context) throws TripleHandlerException {
96          for (TripleHandler handler : children) {
97              handler.receiveNamespace(prefix, uri, context);
98          }
99      }
100 
101     public void close() throws TripleHandlerException {
102         for (TripleHandler handler : children) {
103             handler.close();
104         }
105     }
106 
107     public void endDocument(IRI documentIRI) throws TripleHandlerException {
108         for (TripleHandler handler : children) {
109             handler.endDocument(documentIRI);
110         }
111     }
112 
113     public void setContentLength(long contentLength) {
114         for (TripleHandler handler : children) {
115             handler.setContentLength(contentLength);
116         }
117     }
118 
119 }