001    package net.sf.logdistiller;
002    
003    /*
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *     http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    import java.io.*;
018    import java.util.*;
019    
020    public class PublishHelper
021    {
022        private final LogDistillation ld;
023    
024        private final List publishers;
025    
026        public PublishHelper( LogDistillation ld )
027        {
028            this( ld, new ArrayList() );
029        }
030    
031        public PublishHelper( LogDistillation ld, List publishers )
032        {
033            this.ld = ld;
034            this.publishers = publishers;
035        }
036    
037        public int publish()
038            throws IOException, Publisher.PublishException
039        {
040            int published = publishGlobal();
041    
042            LogDistillation.Group[] groups = ld.getGroups();
043            int count = groups.length;
044            for ( int i = 0; i < count; i++ )
045            {
046                LogDistillation.Group group = groups[i];
047                if ( ( !group.isSkip() ) && ( group.getEventCount() > 0 ) )
048                {
049                    published += publishGroup( group );
050                }
051            }
052            return published;
053        }
054    
055        public int publishGlobal()
056            throws IOException, Publisher.PublishException
057        {
058            LogDistiller.Report[] reports = ld.getDefinition().getOutput().getReports();
059            int count = reports.length;
060            int published = 0;
061            for ( int i = 0; i < count; i++ )
062            {
063                LogDistiller.Report report = reports[i];
064                Publisher publisher = Publishers.getPublisher( report.getPublisher() );
065                if ( isPublisherEnabled( publisher ) )
066                {
067                    publisher.publish( ld, report );
068                    published++;
069                }
070            }
071            return published;
072        }
073    
074        public int publishGroup( LogDistillation.Group group )
075            throws IOException, Publisher.PublishException
076        {
077            LogDistiller.Report[] reports = group.getDefinition().getReports();
078            int count = reports.length;
079            int published = 0;
080            for ( int i = 0; i < count; i++ )
081            {
082                LogDistiller.Report report = reports[i];
083                Publisher publisher = Publishers.getPublisher( report.getPublisher() );
084                if ( isPublisherEnabled( publisher ) )
085                {
086                    publisher.publish( group, report );
087                    published++;
088                }
089            }
090            return published;
091        }
092    
093        private boolean isPublisherEnabled( Publisher publisher )
094        {
095            return ( publisher != null ) && ( ( publishers == null ) || publishers.contains( publisher.getId() ) );
096        }
097    }