KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > extractor > PropertyExtractorTrigger


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/share/org/apache/slide/extractor/PropertyExtractorTrigger.java,v 1.5.2.1 2004/09/29 15:01:26 unico Exp $
3  * $Revision: 1.5.2.1 $
4  * $Date: 2004/09/29 15:01:26 $
5  *
6  * ====================================================================
7  *
8  * Copyright 2004 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */

23
24 package org.apache.slide.extractor;
25
26 import java.io.ByteArrayInputStream JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.Map JavaDoc;
29
30 import org.apache.slide.common.PropertyName;
31 import org.apache.slide.content.NodeRevisionContent;
32 import org.apache.slide.content.NodeRevisionDescriptor;
33 import org.apache.slide.content.NodeRevisionDescriptors;
34 import org.apache.slide.event.ContentAdapter;
35 import org.apache.slide.event.ContentEvent;
36 import org.apache.slide.event.VetoException;
37
38 /**
39  * @version $Revision: 1.5.2.1 $
40  */

41 public class PropertyExtractorTrigger extends ContentAdapter {
42     public void create(ContentEvent event) throws VetoException {
43         store(event);
44     }
45
46     public void store(ContentEvent event) throws VetoException {
47         try {
48             String JavaDoc namespaceName = event.getNamespace() == null ? null : event.getNamespace().getName();
49             NodeRevisionDescriptors descriptors = event.getRevisionDescriptors();
50             NodeRevisionDescriptor descriptor = event.getRevisionDescriptor();
51             NodeRevisionContent content = event.getRevisionContent();
52             if ( content != null && descriptor != null ) {
53                 PropertyExtractor[] extractor = ExtractorManager.getInstance().getPropertyExtractors(namespaceName, descriptors, descriptor);
54                 for ( int i = 0; i < extractor.length; i++ ) {
55                     Map JavaDoc extractedProperties = extractor[i].extract(new ByteArrayInputStream JavaDoc(content.getContentBytes()));
56                     for ( Iterator JavaDoc j = extractedProperties.entrySet().iterator(); j.hasNext(); ) {
57                         Map.Entry JavaDoc entry = (Map.Entry JavaDoc) j.next();
58                         final Object JavaDoc key = entry.getKey();
59                         if (key instanceof PropertyName) {
60                             final String JavaDoc name = ((PropertyName) key).getName();
61                             final String JavaDoc namespace = ((PropertyName) key).getNamespace();
62                             descriptor.setProperty(name, namespace, entry.getValue());
63                         }
64                         else {
65                             descriptor.setProperty((String JavaDoc) entry.getKey(), entry.getValue());
66                         }
67                     }
68                 }
69             }
70         } catch ( ExtractorException e ) {
71             throw new VetoException(e.getMessage());
72         }
73     }
74 }
Popular Tags