001    /*
002     * JBoss DNA (http://www.jboss.org/dna)
003     * See the COPYRIGHT.txt file distributed with this work for information
004     * regarding copyright ownership.  Some portions may be licensed
005     * to Red Hat, Inc. under one or more contributor license agreements.
006     * See the AUTHORS.txt file in the distribution for a full listing of 
007     * individual contributors.
008     *
009     * Unless otherwise indicated, all code in JBoss DNA is licensed
010     * to you under the terms of the GNU Lesser General Public License as
011     * published by the Free Software Foundation; either version 2.1 of
012     * the License, or (at your option) any later version.
013     * 
014     * JBoss DNA is distributed in the hope that it will be useful,
015     * but WITHOUT ANY WARRANTY; without even the implied warranty of
016     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017     * Lesser General Public License for more details.
018     *
019     * You should have received a copy of the GNU Lesser General Public
020     * License along with this software; if not, write to the Free
021     * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
022     * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
023     */
024    package org.jboss.dna.sequencer.cnd;
025    
026    import java.io.IOException;
027    import java.io.InputStream;
028    import java.util.List;
029    import org.jboss.dna.cnd.CndImporter;
030    import org.jboss.dna.graph.ExecutionContext;
031    import org.jboss.dna.graph.io.Destination;
032    import org.jboss.dna.graph.property.Path;
033    import org.jboss.dna.graph.property.Property;
034    import org.jboss.dna.graph.sequencer.SequencerContext;
035    import org.jboss.dna.graph.sequencer.SequencerOutput;
036    import org.jboss.dna.graph.sequencer.StreamSequencer;
037    
038    /**
039     * 
040     */
041    public class CndSequencer implements StreamSequencer {
042    
043        /**
044         * {@inheritDoc}
045         * 
046         * @see org.jboss.dna.graph.sequencer.StreamSequencer#sequence(java.io.InputStream,
047         *      org.jboss.dna.graph.sequencer.SequencerOutput, org.jboss.dna.graph.sequencer.SequencerContext)
048         */
049        public void sequence( InputStream stream,
050                              SequencerOutput output,
051                              SequencerContext context ) {
052            // Create the destination that forwards to the sequencer output ...
053            Destination destination = new OutputDestination(output, context);
054            // Use the CND importer ...
055            Path root = context.getValueFactories().getPathFactory().createRootPath();
056            CndImporter importer = new CndImporter(destination, root);
057            String resourceName = context.getInputPath().getString(context.getNamespaceRegistry());
058            try {
059                importer.importFrom(stream, context.getProblems(), resourceName);
060            } catch (IOException e) {
061                context.getProblems().addError(e, CndSequencerI18n.errorSequencingCndContent, e.getLocalizedMessage());
062            }
063        }
064    
065        protected class OutputDestination implements Destination {
066            private final SequencerOutput output;
067            private final SequencerContext context;
068    
069            protected OutputDestination( SequencerOutput output,
070                                         SequencerContext context ) {
071                this.output = output;
072                this.context = context;
073            }
074    
075            /**
076             * {@inheritDoc}
077             * 
078             * @see org.jboss.dna.graph.io.Destination#getExecutionContext()
079             */
080            public ExecutionContext getExecutionContext() {
081                return context;
082            }
083    
084            /**
085             * {@inheritDoc}
086             * 
087             * @see org.jboss.dna.graph.io.Destination#create(org.jboss.dna.graph.property.Path, java.util.List)
088             */
089            public void create( Path path,
090                                List<Property> properties ) {
091                for (Property property : properties) {
092                    output.setProperty(path, property.getName(), property.getValues());
093                }
094            }
095    
096            /**
097             * {@inheritDoc}
098             * 
099             * @see org.jboss.dna.graph.io.Destination#create(org.jboss.dna.graph.property.Path,
100             *      org.jboss.dna.graph.property.Property, org.jboss.dna.graph.property.Property[])
101             */
102            public void create( Path path,
103                                Property firstProperty,
104                                Property... additionalProperties ) {
105                output.setProperty(path, firstProperty.getName(), firstProperty.getValues());
106                for (Property property : additionalProperties) {
107                    output.setProperty(path, property.getName(), property.getValues());
108                }
109            }
110    
111            /**
112             * {@inheritDoc}
113             * 
114             * @see org.jboss.dna.graph.io.Destination#setProperties(org.jboss.dna.graph.property.Path,
115             *      org.jboss.dna.graph.property.Property[])
116             */
117            public void setProperties( Path path,
118                                       Property... properties ) {
119                for (Property property : properties) {
120                    output.setProperty(path, property.getName(), property.getValues());
121                }
122            }
123    
124            /**
125             * {@inheritDoc}
126             * 
127             * @see org.jboss.dna.graph.io.Destination#submit()
128             */
129            public void submit() {
130                // nothing to call on the sequencer output ...
131            }
132        }
133    
134    }