KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > books > publisher > impl > cocooncomponents > MergePropsAndMetadataTransformer


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.outerj.daisy.books.publisher.impl.cocooncomponents;
17
18 import org.apache.cocoon.transformation.AbstractTransformer;
19 import org.apache.cocoon.environment.SourceResolver;
20 import org.apache.cocoon.ProcessingException;
21 import org.apache.cocoon.components.flow.FlowHelper;
22 import org.apache.avalon.framework.parameters.Parameters;
23 import org.apache.commons.jxpath.JXPathContext;
24 import org.xml.sax.SAXException JavaDoc;
25 import org.xml.sax.Attributes JavaDoc;
26 import org.outerj.daisy.books.publisher.impl.util.XMLPropertiesHelper;
27
28 import java.util.Map JavaDoc;
29 import java.io.IOException JavaDoc;
30
31 /**
32  * Merges publication properties and metadata into the SAX stream (right after the opening root element).
33  */

34 public class MergePropsAndMetadataTransformer extends AbstractTransformer {
35     private Map JavaDoc publicationProperties;
36     private Map JavaDoc bookMetadata;
37     private boolean rootElement;
38
39     public void setup(SourceResolver sourceResolver, Map JavaDoc objectModel, String JavaDoc string, Parameters parameters) throws ProcessingException, SAXException JavaDoc, IOException JavaDoc {
40         Object JavaDoc flowContext = FlowHelper.getContextObject(objectModel);
41         JXPathContext jxpc = JXPathContext.newContext(flowContext);
42
43         publicationProperties = (Map JavaDoc)jxpc.getValue("/pubProps");
44         if (publicationProperties == null)
45             throw new ProcessingException("Missing 'pubProps' in flow context.");
46
47         bookMetadata = (Map JavaDoc)jxpc.getValue("/bookMetadata");
48         if (bookMetadata == null)
49             throw new ProcessingException("Missing 'bookMetadata' in flow context.");
50
51         rootElement = true;
52     }
53
54     public void recycle() {
55         super.recycle();
56         this.publicationProperties = null;
57         this.bookMetadata = null;
58     }
59
60     public void startElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc attributes) throws SAXException JavaDoc {
61         super.startElement(namespaceURI, localName, qName, attributes);
62
63         if (rootElement) {
64             XMLPropertiesHelper.generateSaxFragment(publicationProperties, contentHandler);
65             XMLPropertiesHelper.generateSaxFragment(bookMetadata, "metadata", contentHandler);
66             rootElement = false;
67         }
68     }
69 }
70
Popular Tags