Return-Path: Delivered-To: apmail-incubator-sling-commits-archive@minotaur.apache.org Received: (qmail 49602 invoked from network); 8 Jun 2009 08:05:58 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 8 Jun 2009 08:05:58 -0000 Received: (qmail 88445 invoked by uid 500); 8 Jun 2009 08:06:10 -0000 Delivered-To: apmail-incubator-sling-commits-archive@incubator.apache.org Received: (qmail 88389 invoked by uid 500); 8 Jun 2009 08:06:10 -0000 Mailing-List: contact sling-commits-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: sling-dev@incubator.apache.org Delivered-To: mailing list sling-commits@incubator.apache.org Received: (qmail 88380 invoked by uid 99); 8 Jun 2009 08:06:10 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 08 Jun 2009 08:06:10 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 08 Jun 2009 08:06:03 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 1BAF623888D1; Mon, 8 Jun 2009 08:05:42 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r782553 [1/2] - in /incubator/sling/whiteboard/rewriter/src/main/java/org/apache/sling/rewriter: ./ impl/ Date: Mon, 08 Jun 2009 08:05:41 -0000 To: sling-commits@incubator.apache.org From: cziegeler@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20090608080542.1BAF623888D1@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: cziegeler Date: Mon Jun 8 08:05:41 2009 New Revision: 782553 URL: http://svn.apache.org/viewvc?rev=782553&view=rev Log: Use real factory services instead of using the osgi component factories. This makes the whole rewriter component handling easier and more understandable. Added: incubator/sling/whiteboard/rewriter/src/main/java/org/apache/sling/rewriter/GeneratorFactory.java (with props) incubator/sling/whiteboard/rewriter/src/main/java/org/apache/sling/rewriter/ProcessorFactory.java (with props) incubator/sling/whiteboard/rewriter/src/main/java/org/apache/sling/rewriter/SerializerFactory.java (with props) incubator/sling/whiteboard/rewriter/src/main/java/org/apache/sling/rewriter/TransformerFactory.java (contents, props changed) - copied, changed from r782538, incubator/sling/whiteboard/rewriter/src/main/java/org/apache/sling/rewriter/RewriterTransformerFactory.java incubator/sling/whiteboard/rewriter/src/main/java/org/apache/sling/rewriter/impl/HtmlGeneratorFactory.java (contents, props changed) - copied, changed from r782538, incubator/sling/whiteboard/rewriter/src/main/java/org/apache/sling/rewriter/impl/HtmlGenerator.java incubator/sling/whiteboard/rewriter/src/main/java/org/apache/sling/rewriter/impl/HtmlSerializerFactory.java (contents, props changed) - copied, changed from r782538, incubator/sling/whiteboard/rewriter/src/main/java/org/apache/sling/rewriter/impl/HTMLSerializer.java Removed: incubator/sling/whiteboard/rewriter/src/main/java/org/apache/sling/rewriter/RewriterTransformerFactory.java incubator/sling/whiteboard/rewriter/src/main/java/org/apache/sling/rewriter/impl/HTMLSerializer.java incubator/sling/whiteboard/rewriter/src/main/java/org/apache/sling/rewriter/impl/HtmlGenerator.java Modified: incubator/sling/whiteboard/rewriter/src/main/java/org/apache/sling/rewriter/Generator.java incubator/sling/whiteboard/rewriter/src/main/java/org/apache/sling/rewriter/impl/FactoryCache.java incubator/sling/whiteboard/rewriter/src/main/java/org/apache/sling/rewriter/impl/PipelineImpl.java Modified: incubator/sling/whiteboard/rewriter/src/main/java/org/apache/sling/rewriter/Generator.java URL: http://svn.apache.org/viewvc/incubator/sling/whiteboard/rewriter/src/main/java/org/apache/sling/rewriter/Generator.java?rev=782553&r1=782552&r2=782553&view=diff ============================================================================== --- incubator/sling/whiteboard/rewriter/src/main/java/org/apache/sling/rewriter/Generator.java (original) +++ incubator/sling/whiteboard/rewriter/src/main/java/org/apache/sling/rewriter/Generator.java Mon Jun 8 08:05:41 2009 @@ -24,6 +24,9 @@ /** * The Generator interface defines the start of a rewriter pipeline. + * A generator is not a component managed by the container (OSGi). A + * {@link GeneratorFactory} is a service managed by the container which creates + * generator instances on demand. */ public interface Generator { Added: incubator/sling/whiteboard/rewriter/src/main/java/org/apache/sling/rewriter/GeneratorFactory.java URL: http://svn.apache.org/viewvc/incubator/sling/whiteboard/rewriter/src/main/java/org/apache/sling/rewriter/GeneratorFactory.java?rev=782553&view=auto ============================================================================== --- incubator/sling/whiteboard/rewriter/src/main/java/org/apache/sling/rewriter/GeneratorFactory.java (added) +++ incubator/sling/whiteboard/rewriter/src/main/java/org/apache/sling/rewriter/GeneratorFactory.java Mon Jun 8 08:05:41 2009 @@ -0,0 +1,39 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.sling.rewriter; + + +/** + * The GeneratorFactory is a service which creates + * {@link Generator}s on demand. The created generators are the + * starting point for the rewriter pipeline. + * + * The factories itself are not chained but the resulting generators + * are. On each pipeline call new instances are created. + * + * The factory is referenced using a service property named + * 'pipeline.type'. Each factory should have a unique value + * for this property. + */ +public interface GeneratorFactory { + + /** + * Create a new generator for the pipeline. + * @return A new generator. + */ + Generator createGenerator(); +} Propchange: incubator/sling/whiteboard/rewriter/src/main/java/org/apache/sling/rewriter/GeneratorFactory.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: incubator/sling/whiteboard/rewriter/src/main/java/org/apache/sling/rewriter/GeneratorFactory.java ------------------------------------------------------------------------------ svn:keywords = author date id revision rev url Propchange: incubator/sling/whiteboard/rewriter/src/main/java/org/apache/sling/rewriter/GeneratorFactory.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: incubator/sling/whiteboard/rewriter/src/main/java/org/apache/sling/rewriter/ProcessorFactory.java URL: http://svn.apache.org/viewvc/incubator/sling/whiteboard/rewriter/src/main/java/org/apache/sling/rewriter/ProcessorFactory.java?rev=782553&view=auto ============================================================================== --- incubator/sling/whiteboard/rewriter/src/main/java/org/apache/sling/rewriter/ProcessorFactory.java (added) +++ incubator/sling/whiteboard/rewriter/src/main/java/org/apache/sling/rewriter/ProcessorFactory.java Mon Jun 8 08:05:41 2009 @@ -0,0 +1,36 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.sling.rewriter; + + +/** + * The ProcessorFactory is a service which creates + * {@link Processor}s on demand. The processor is used to rewrite + * the generated content. + * + * The factory is referenced using a service property named + * 'pipeline.type'. Each factory should have a unique value + * for this property. + */ +public interface ProcessorFactory { + + /** + * Create a new processor. + * @return A new processor. + */ + Processor createProcessor(); +} Propchange: incubator/sling/whiteboard/rewriter/src/main/java/org/apache/sling/rewriter/ProcessorFactory.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: incubator/sling/whiteboard/rewriter/src/main/java/org/apache/sling/rewriter/ProcessorFactory.java ------------------------------------------------------------------------------ svn:keywords = author date id revision rev url Propchange: incubator/sling/whiteboard/rewriter/src/main/java/org/apache/sling/rewriter/ProcessorFactory.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: incubator/sling/whiteboard/rewriter/src/main/java/org/apache/sling/rewriter/SerializerFactory.java URL: http://svn.apache.org/viewvc/incubator/sling/whiteboard/rewriter/src/main/java/org/apache/sling/rewriter/SerializerFactory.java?rev=782553&view=auto ============================================================================== --- incubator/sling/whiteboard/rewriter/src/main/java/org/apache/sling/rewriter/SerializerFactory.java (added) +++ incubator/sling/whiteboard/rewriter/src/main/java/org/apache/sling/rewriter/SerializerFactory.java Mon Jun 8 08:05:41 2009 @@ -0,0 +1,39 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.sling.rewriter; + + +/** + * The SerializerFactory is a service which creates + * {@link Serializer}s on demand. The created serializers are the + * end point for the rewriter pipeline. + * + * The factories itself are not chained but the resulting serializers + * are. On each pipeline call new instances are created. + * + * The factory is referenced using a service property named + * 'pipeline.type'. Each factory should have a unique value + * for this property. + */ +public interface SerializerFactory { + + /** + * Create a new serializer for the pipeline. + * @return A new serializer. + */ + Serializer createSerializer(); +} Propchange: incubator/sling/whiteboard/rewriter/src/main/java/org/apache/sling/rewriter/SerializerFactory.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: incubator/sling/whiteboard/rewriter/src/main/java/org/apache/sling/rewriter/SerializerFactory.java ------------------------------------------------------------------------------ svn:keywords = author date id revision rev url Propchange: incubator/sling/whiteboard/rewriter/src/main/java/org/apache/sling/rewriter/SerializerFactory.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Copied: incubator/sling/whiteboard/rewriter/src/main/java/org/apache/sling/rewriter/TransformerFactory.java (from r782538, incubator/sling/whiteboard/rewriter/src/main/java/org/apache/sling/rewriter/RewriterTransformerFactory.java) URL: http://svn.apache.org/viewvc/incubator/sling/whiteboard/rewriter/src/main/java/org/apache/sling/rewriter/TransformerFactory.java?p2=incubator/sling/whiteboard/rewriter/src/main/java/org/apache/sling/rewriter/TransformerFactory.java&p1=incubator/sling/whiteboard/rewriter/src/main/java/org/apache/sling/rewriter/RewriterTransformerFactory.java&r1=782538&r2=782553&rev=782553&view=diff ============================================================================== --- incubator/sling/whiteboard/rewriter/src/main/java/org/apache/sling/rewriter/RewriterTransformerFactory.java (original) +++ incubator/sling/whiteboard/rewriter/src/main/java/org/apache/sling/rewriter/TransformerFactory.java Mon Jun 8 08:05:41 2009 @@ -18,18 +18,30 @@ /** - * The RewriterTransformerFactory is an optional component - * which can be used to enhance the rewriting pipeline. - * All available rewriting transformers with a service ranking below - * zero are chained before the default link rewriter. All available - * transformers with a service ranking higher or equal to zero are - * chained after the default link rewriter. Therefore the property - * "service.ranking" should be used for the factory. + * The TransformerFactory is a service which creates + * {@link Transformer}s on demand. The created transformers form + * the middle part of the rewriter pipeline. * * The factories itself are not chained but the resulting transformers * are. On each pipeline call new instances are created. + * + * The factory is referenced using a service property named + * 'pipeline.type'. Each factory should have a unique value + * for this property. + * + * With the optional property 'pipeline.mode' set to the value + * 'global' the transformer is used for each and every pipeline regardless + * of the actual configuration for this pipeline. + * All available global transformers with a service ranking below + * zero are chained right after the generator. All available global + * transformers with a service ranking higher or equal to zero are + * chained right before the serializer. Therefore the property + * "service.ranking" should be used for the factory in combination + * with 'pipeline.mode'. + * To be compatible with possible future uses of the 'pipeline.mode' + * property, it should only be used with the value 'global'. */ -public interface RewriterTransformerFactory { +public interface TransformerFactory { /** * Create a new transformer for the pipeline. Propchange: incubator/sling/whiteboard/rewriter/src/main/java/org/apache/sling/rewriter/TransformerFactory.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: incubator/sling/whiteboard/rewriter/src/main/java/org/apache/sling/rewriter/TransformerFactory.java ------------------------------------------------------------------------------ svn:keywords = author date id revision rev url Propchange: incubator/sling/whiteboard/rewriter/src/main/java/org/apache/sling/rewriter/TransformerFactory.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Modified: incubator/sling/whiteboard/rewriter/src/main/java/org/apache/sling/rewriter/impl/FactoryCache.java URL: http://svn.apache.org/viewvc/incubator/sling/whiteboard/rewriter/src/main/java/org/apache/sling/rewriter/impl/FactoryCache.java?rev=782553&r1=782552&r2=782553&view=diff ============================================================================== --- incubator/sling/whiteboard/rewriter/src/main/java/org/apache/sling/rewriter/impl/FactoryCache.java (original) +++ incubator/sling/whiteboard/rewriter/src/main/java/org/apache/sling/rewriter/impl/FactoryCache.java Mon Jun 8 08:05:41 2009 @@ -23,16 +23,17 @@ import java.util.Map; import org.apache.sling.rewriter.Generator; +import org.apache.sling.rewriter.GeneratorFactory; import org.apache.sling.rewriter.Processor; -import org.apache.sling.rewriter.RewriterTransformerFactory; +import org.apache.sling.rewriter.ProcessorFactory; import org.apache.sling.rewriter.Serializer; +import org.apache.sling.rewriter.SerializerFactory; import org.apache.sling.rewriter.Transformer; +import org.apache.sling.rewriter.TransformerFactory; import org.osgi.framework.BundleContext; import org.osgi.framework.Constants; -import org.osgi.framework.Filter; import org.osgi.framework.InvalidSyntaxException; import org.osgi.framework.ServiceReference; -import org.osgi.service.component.ComponentFactory; import org.osgi.service.component.ComponentInstance; import org.osgi.util.tracker.ServiceTracker; import org.slf4j.Logger; @@ -48,31 +49,27 @@ protected static final Logger LOGGER = LoggerFactory.getLogger(FactoryCache.class); /** The tracker for generator factories. */ - protected final HashingServiceTrackerCustomizer generatorTracker; + protected final HashingServiceTrackerCustomizer generatorTracker; /** The tracker for serializers factories. */ - protected final HashingServiceTrackerCustomizer serializerTracker; + protected final HashingServiceTrackerCustomizer serializerTracker; /** The tracker for transformer factories. */ - protected final HashingServiceTrackerCustomizer transformerTracker; - - /** The tracker for rewriting transformer factories. */ - protected final RewriterTransformerFactoryServiceTracker rewritingTransformerTracker; + protected final TransformerFactoryServiceTracker transformerTracker; /** The tracker for processor factories. */ - protected final HashingServiceTrackerCustomizer processorTracker; + protected final HashingServiceTrackerCustomizer processorTracker; public FactoryCache(final BundleContext context) throws InvalidSyntaxException { - this.generatorTracker = new HashingServiceTrackerCustomizer(context, - context.createFilter("(component.factory=" + Generator.class.getName() + "/*)")); - this.serializerTracker = new HashingServiceTrackerCustomizer(context, - context.createFilter("(component.factory=" + Serializer.class.getName() + "/*)")); - this.transformerTracker = new HashingServiceTrackerCustomizer(context, - context.createFilter("(component.factory=" + Transformer.class.getName() + "/*)")); - this.processorTracker = new HashingServiceTrackerCustomizer(context, - context.createFilter("(component.factory=" + Processor.class.getName() + "/*)")); - this.rewritingTransformerTracker = new RewriterTransformerFactoryServiceTracker(context); + this.generatorTracker = new HashingServiceTrackerCustomizer(context, + GeneratorFactory.class.getName()); + this.serializerTracker = new HashingServiceTrackerCustomizer(context, + SerializerFactory.class.getName()); + this.transformerTracker = new TransformerFactoryServiceTracker(context, + TransformerFactory.class.getName()); + this.processorTracker = new HashingServiceTrackerCustomizer(context, + ProcessorFactory.class.getName()); } /** @@ -82,7 +79,6 @@ this.generatorTracker.open(); this.serializerTracker.open(); this.transformerTracker.open(); - this.rewritingTransformerTracker.open(); this.processorTracker.open(); } @@ -93,72 +89,63 @@ this.generatorTracker.close(); this.serializerTracker.close(); this.transformerTracker.close(); - this.rewritingTransformerTracker.close(); this.processorTracker.close(); } /** - * Lookup a factory component. - */ - protected ComponentType getComponent(final List instanceList, - final Class typeClass, - final String type, - final HashingServiceTrackerCustomizer customizer) { - // get factory - final ComponentFactory factory = customizer.get(type); - if ( factory == null ) { - LOGGER.debug("Request component factory for class '{}' and type '{}' not found.", typeClass, type); - return null; - } - final ComponentInstance instance = factory.newInstance(null); - instanceList.add(instance); - @SuppressWarnings("unchecked") - final ComponentType component = (ComponentType)instance.getInstance(); - if ( component == null ) { - // this should never happen - LOGGER.debug("Unable to get instance '{}' of type '{}' from factory.", typeClass, type); - } - return component; - } - - /** * Get the generator of the given type. * @param type The generator type. - * @param instanceList The instance is added to this list for later disposal. * @return The generator or null if the generator is not available. */ - public Generator getGenerator(final String type, final List instanceList) { - return this.getComponent(instanceList, Generator.class, type, this.generatorTracker); + public Generator getGenerator(final String type) { + final GeneratorFactory factory = this.generatorTracker.getFactory(type); + if ( factory == null ) { + LOGGER.debug("Requested generator factory for type '{}' not found.", type); + return null; + } + return factory.createGenerator(); } /** * Get the serializer of the given type. * @param type The serializer type. - * @param instanceList The instance is added to this list for later disposal. * @return The serializer or null if the serializer is not available. */ - public Serializer getSerializer(final String type, final List instanceList) { - return this.getComponent(instanceList, Serializer.class, type, this.serializerTracker); + public Serializer getSerializer(final String type) { + final SerializerFactory factory = this.serializerTracker.getFactory(type); + if ( factory == null ) { + LOGGER.debug("Requested serializer factory for type '{}' not found.", type); + return null; + } + return factory.createSerializer(); } /** * Get the transformer of the given type. * @param type The transformer type. - * @param instanceList The instance is added to this list for later disposal. * @return The transformer or null if the transformer is not available. */ - public Transformer getTransformer(final String type, final List instanceList) { - return this.getComponent(instanceList, Transformer.class, type, this.transformerTracker); + public Transformer getTransformer(final String type) { + final TransformerFactory factory = this.transformerTracker.getFactory(type); + if ( factory == null ) { + LOGGER.debug("Requested transformer factory for type '{}' not found.", type); + return null; + } + return factory.createTransformer(); } /** * Get the processor of the given type. * @param type The processor type. - * @param instanceList The instance is added to this list for later disposal. * @return The processor or null if the processor is not available. */ public Processor getProcessor(final String type, final List instanceList) { - return this.getComponent(instanceList, Processor.class, type, this.processorTracker); + final ProcessorFactory factory = this.processorTracker.getFactory(type); + if ( factory == null ) { + LOGGER.debug("Requested processor factory for type '{}' not found.", type); + return null; + } + return factory.createProcessor(); } private static final Transformer[] EMPTY_ARRAY = new Transformer[0]; @@ -171,35 +158,32 @@ * all post transformers. */ public Transformer[][] getRewriterTransformers() { - return this.rewritingTransformerTracker.getTransformers(); + return this.transformerTracker.getTransformers(); } /** * This service tracker stores all services into a hash map. */ - protected final class HashingServiceTrackerCustomizer extends ServiceTracker { + protected static class HashingServiceTrackerCustomizer extends ServiceTracker { - private final Map services = new HashMap(); + /** The services hashed by their name property. */ + private final Map services = new HashMap(); + /** The bundle context. */ private final BundleContext context; - public HashingServiceTrackerCustomizer(final BundleContext bc, final Filter filter) { - super(bc, filter, null); + public HashingServiceTrackerCustomizer(final BundleContext bc, final String serviceClassName) { + super(bc, serviceClassName, null); this.context = bc; } - public ComponentFactory get(final String type) { + public T getFactory(final String type) { return services.get(type); } private String getType(final ServiceReference ref) { - final String factory = (String) ref.getProperty("component.factory"); - final int pos = factory.lastIndexOf('/'); - if ( pos != -1 ) { - final String type = factory.substring(pos + 1); - return type; - } - return null; + final String type = (String) ref.getProperty("pipeline.type"); + return type; } /** @@ -207,7 +191,8 @@ */ public Object addingService(final ServiceReference reference) { final String type = this.getType(reference); - final ComponentFactory factory = (type == null ? null : (ComponentFactory) this.context.getService(reference)); + @SuppressWarnings("unchecked") + final T factory = (type == null ? null : (T) this.context.getService(reference)); if ( factory != null ) { if ( LOGGER.isDebugEnabled() ) { LOGGER.debug("Found service {}, type={}.", factory, type); @@ -228,24 +213,36 @@ } } - protected static final class RewriterTransformerFactoryServiceTracker extends ServiceTracker { + protected static final class TransformerFactoryServiceTracker extends HashingServiceTrackerCustomizer { - public static final RewriterTransformerFactory[] EMPTY_ARRAY = new RewriterTransformerFactory[0]; - public static final RewriterTransformerFactory[][] EMPTY_DOUBLE_ARRAY = new RewriterTransformerFactory[][] {EMPTY_ARRAY, EMPTY_ARRAY}; + private String getMode(final ServiceReference ref) { + final String mode = (String) ref.getProperty("pipeline.mode"); + return mode; + } - private RewriterTransformerFactory[][] cached = EMPTY_DOUBLE_ARRAY; + private boolean isGlobal(final ServiceReference ref) { + return "global".equalsIgnoreCase(this.getMode(ref)); + } + + public static final TransformerFactory[] EMPTY_ARRAY = new TransformerFactory[0]; + public static final TransformerFactory[][] EMPTY_DOUBLE_ARRAY = new TransformerFactory[][] {EMPTY_ARRAY, EMPTY_ARRAY}; + private TransformerFactory[][] cached = EMPTY_DOUBLE_ARRAY; + + /** flag for cache. */ private boolean cacheIsValid = true; - public RewriterTransformerFactoryServiceTracker(final BundleContext bc) { - super(bc, RewriterTransformerFactory.class.getName(), null); + public TransformerFactoryServiceTracker(final BundleContext bc, final String serviceClassName) { + super(bc, serviceClassName); } /** * @see org.osgi.util.tracker.ServiceTracker#addingService(org.osgi.framework.ServiceReference) */ public Object addingService(ServiceReference reference) { - this.cacheIsValid = false; + if ( isGlobal(reference) ) { + this.cacheIsValid = false; + } return super.addingService(reference); } @@ -253,15 +250,13 @@ * @see org.osgi.util.tracker.ServiceTracker#removedService(org.osgi.framework.ServiceReference, java.lang.Object) */ public void removedService(ServiceReference reference, Object service) { - this.cacheIsValid = false; + if ( isGlobal(reference) ) { + this.cacheIsValid = false; + } super.removedService(reference, service); } - public boolean isCacheValid() { - return this.cacheIsValid; - } - - private RewriterTransformerFactory[][] getTransformerFactories() { + private TransformerFactory[][] getTransformerFactories() { if ( !this.cacheIsValid ) { synchronized ( this ) { if ( !this.cacheIsValid ) { @@ -274,33 +269,37 @@ int preCount = 0; int postCount = 0; for(final ServiceReference ref : refs) { - final Object r = ref.getProperty(Constants.SERVICE_RANKING); - int ranking = (r instanceof Integer ? (Integer)r : 0); - if ( ranking < 0 ) { - preCount++; - } else { - postCount++; + if ( isGlobal(ref) ) { + final Object r = ref.getProperty(Constants.SERVICE_RANKING); + int ranking = (r instanceof Integer ? (Integer)r : 0); + if ( ranking < 0 ) { + preCount++; + } else { + postCount++; + } } } - final RewriterTransformerFactory[][] rewriters = new RewriterTransformerFactory[2][]; + final TransformerFactory[][] rewriters = new TransformerFactory[2][]; if ( preCount == 0 ) { rewriters[0] = EMPTY_ARRAY; } else { - rewriters[0] = new RewriterTransformerFactory[preCount]; + rewriters[0] = new TransformerFactory[preCount]; } if ( postCount == 0) { rewriters[1] = EMPTY_ARRAY; } else { - rewriters[1] = new RewriterTransformerFactory[postCount]; + rewriters[1] = new TransformerFactory[postCount]; } int index = 0; for(final ServiceReference ref : refs) { - if ( index < preCount ) { - rewriters[0][index] = (RewriterTransformerFactory) this.getService(ref); - } else { - rewriters[1][index - preCount] = (RewriterTransformerFactory) this.getService(ref); + if ( isGlobal(ref) ) { + if ( index < preCount ) { + rewriters[0][index] = (TransformerFactory) this.getService(ref); + } else { + rewriters[1][index - preCount] = (TransformerFactory) this.getService(ref); + } + index++; } - index++; } this.cached = rewriters; } @@ -312,7 +311,7 @@ } public Transformer[][] getTransformers() { - final RewriterTransformerFactory[][] factories = this.getTransformerFactories(); + final TransformerFactory[][] factories = this.getTransformerFactories(); if ( factories == EMPTY_DOUBLE_ARRAY ) { return FactoryCache.EMPTY_DOUBLE_ARRAY; } Copied: incubator/sling/whiteboard/rewriter/src/main/java/org/apache/sling/rewriter/impl/HtmlGeneratorFactory.java (from r782538, incubator/sling/whiteboard/rewriter/src/main/java/org/apache/sling/rewriter/impl/HtmlGenerator.java) URL: http://svn.apache.org/viewvc/incubator/sling/whiteboard/rewriter/src/main/java/org/apache/sling/rewriter/impl/HtmlGeneratorFactory.java?p2=incubator/sling/whiteboard/rewriter/src/main/java/org/apache/sling/rewriter/impl/HtmlGeneratorFactory.java&p1=incubator/sling/whiteboard/rewriter/src/main/java/org/apache/sling/rewriter/impl/HtmlGenerator.java&r1=782538&r2=782553&rev=782553&view=diff ============================================================================== --- incubator/sling/whiteboard/rewriter/src/main/java/org/apache/sling/rewriter/impl/HtmlGenerator.java (original) +++ incubator/sling/whiteboard/rewriter/src/main/java/org/apache/sling/rewriter/impl/HtmlGeneratorFactory.java Mon Jun 8 08:05:41 2009 @@ -26,6 +26,7 @@ import org.apache.sling.commons.osgi.OsgiUtil; import org.apache.sling.rewriter.Generator; +import org.apache.sling.rewriter.GeneratorFactory; import org.apache.sling.rewriter.ProcessingComponentConfiguration; import org.apache.sling.rewriter.ProcessingContext; import org.xml.sax.ContentHandler; @@ -36,9 +37,10 @@ * starting point for html pipelines. * * @scr.component metatype="no" - * factory="org.apache.sling.rewriter.Generator/html-generator" + * @scr.service + * @scr.property name="pipeline.type" value="html-generator" */ -public class HtmlGenerator extends Writer implements Generator { +public class HtmlGeneratorFactory implements GeneratorFactory { public static String NAMESPACE = "http://org.apache.sling/rewriter"; @@ -48,618 +50,628 @@ public static final String INCLUDE_TAGS_PROPERTY = "includeTags"; - /** Internal character buffer */ - private final CharArrayWriter buffer = new CharArrayWriter(256); + private static final Set DEFAULT_INCLUSION_TAGS; + static { + DEFAULT_INCLUSION_TAGS = new HashSet(); + DEFAULT_INCLUSION_TAGS.add("A"); + DEFAULT_INCLUSION_TAGS.add("/A"); + DEFAULT_INCLUSION_TAGS.add("IMG"); + DEFAULT_INCLUSION_TAGS.add("AREA"); + DEFAULT_INCLUSION_TAGS.add("FORM"); + DEFAULT_INCLUSION_TAGS.add("BASE"); + DEFAULT_INCLUSION_TAGS.add("LINK"); + DEFAULT_INCLUSION_TAGS.add("SCRIPT"); + DEFAULT_INCLUSION_TAGS.add("/BODY"); + } - /** Tag tokenizer */ - private final TagTokenizer tokenizer = new TagTokenizer(); + /** + * @see org.apache.sling.rewriter.GeneratorFactory#createGenerator() + */ + public Generator createGenerator() { + return new HtmlGenerator(); + } - /** Tag name buffer */ - private final CharArrayWriter tagNameBuffer = new CharArrayWriter(30); + public static final class HtmlGenerator extends Writer implements Generator { - /** Tag name */ - private String tagName; + /** Internal character buffer */ + private final CharArrayWriter buffer = new CharArrayWriter(256); - /** Tag inclusion list */ - private Set tagInclusionSet; + /** Tag tokenizer */ + private final TagTokenizer tokenizer = new TagTokenizer(); - /** Registered content handler */ - private ContentHandler contentHandler; + /** Tag name buffer */ + private final CharArrayWriter tagNameBuffer = new CharArrayWriter(30); - /** Parse state constant */ - private final static int PS_OUTSIDE = 0; + /** Tag name */ + private String tagName; - /** Parse state constant */ - private final static int PS_TAG = PS_OUTSIDE + 1; + /** Tag inclusion list */ + private Set tagInclusionSet; - /** Parse state constant */ - private final static int PS_SCRIPT = PS_TAG + 1; + /** Registered content handler */ + private ContentHandler contentHandler; - /** Parse state constant */ - private final static int PS_COMMENT = PS_SCRIPT + 1; + /** Parse state constant */ + private final static int PS_OUTSIDE = 0; - /** Parse state constant */ - private final static int PS_STRING = PS_COMMENT + 1; + /** Parse state constant */ + private final static int PS_TAG = PS_OUTSIDE + 1; - /** Tag type constant */ - private final static int TT_NONE = 0; + /** Parse state constant */ + private final static int PS_SCRIPT = PS_TAG + 1; - /** Tag type constant */ - private final static int TT_MAYBE = 1; + /** Parse state constant */ + private final static int PS_COMMENT = PS_SCRIPT + 1; - /** Tag type constant */ - private final static int TT_TAG = 2; + /** Parse state constant */ + private final static int PS_STRING = PS_COMMENT + 1; - /** Parse state */ - private int parseState; + /** Tag type constant */ + private final static int TT_NONE = 0; - /** Parse substate */ - private int parseSubState; + /** Tag type constant */ + private final static int TT_MAYBE = 1; - /** Previous parse state */ - private int prevParseState; + /** Tag type constant */ + private final static int TT_TAG = 2; - /** Current tag type */ - private int tagType; + /** Parse state */ + private int parseState; - /** Quote character */ - private char quoteChar; + /** Parse substate */ + private int parseSubState; - /** Did we already start parsing? */ - boolean started = false; + /** Previous parse state */ + private int prevParseState; - private final org.xml.sax.helpers.AttributesImpl atts = new org.xml.sax.helpers.AttributesImpl(); + /** Current tag type */ + private int tagType; - private static final Set DEFAULT_INCLUSION_TAGS; - static { - DEFAULT_INCLUSION_TAGS = new HashSet(); - DEFAULT_INCLUSION_TAGS.add("A"); - DEFAULT_INCLUSION_TAGS.add("/A"); - DEFAULT_INCLUSION_TAGS.add("IMG"); - DEFAULT_INCLUSION_TAGS.add("AREA"); - DEFAULT_INCLUSION_TAGS.add("FORM"); - DEFAULT_INCLUSION_TAGS.add("BASE"); - DEFAULT_INCLUSION_TAGS.add("LINK"); - DEFAULT_INCLUSION_TAGS.add("SCRIPT"); - DEFAULT_INCLUSION_TAGS.add("/BODY"); - } + /** Quote character */ + private char quoteChar; - /** - * Default constructor. - */ - public HtmlGenerator() { - this.tagInclusionSet = DEFAULT_INCLUSION_TAGS; - } + /** Did we already start parsing? */ + boolean started = false; - /** - * @see org.apache.sling.rewriter.Generator#init(org.apache.sling.rewriter.ProcessingContext, org.apache.sling.rewriter.ProcessingComponentConfiguration) - */ - public void init(ProcessingContext pipelineContext, - ProcessingComponentConfiguration config) { - final String[] includedTags = OsgiUtil.toStringArray(config - .getConfiguration().get(INCLUDE_TAGS_PROPERTY)); - if (includedTags != null && includedTags.length > 0) { - this.tagInclusionSet = new HashSet(); - for (final String tag : includedTags) { - this.tagInclusionSet.add(tag); - } - // we always have to include body! - this.tagInclusionSet.add("/BODY"); + private final org.xml.sax.helpers.AttributesImpl atts = new org.xml.sax.helpers.AttributesImpl(); + + /** + * Default constructor. + */ + public HtmlGenerator() { + this.tagInclusionSet = DEFAULT_INCLUSION_TAGS; } - } - /** - * @see org.apache.sling.rewriter.Generator#getWriter() - */ - public PrintWriter getWriter() { - return new PrintWriter(this); - } + /** + * @see org.apache.sling.rewriter.Generator#init(org.apache.sling.rewriter.ProcessingContext, org.apache.sling.rewriter.ProcessingComponentConfiguration) + */ + public void init(ProcessingContext pipelineContext, + ProcessingComponentConfiguration config) { + final String[] includedTags = OsgiUtil.toStringArray(config + .getConfiguration().get(INCLUDE_TAGS_PROPERTY)); + if (includedTags != null && includedTags.length > 0) { + this.tagInclusionSet = new HashSet(); + for (final String tag : includedTags) { + this.tagInclusionSet.add(tag); + } + // we always have to include body! + this.tagInclusionSet.add("/BODY"); + } + } - public Set getTagInclusionSet() { - return tagInclusionSet; - } + /** + * @see org.apache.sling.rewriter.Generator#getWriter() + */ + public PrintWriter getWriter() { + return new PrintWriter(this); + } - public void setTagInclusionSet(Set tagInclusionSet) { - this.tagInclusionSet = tagInclusionSet; - } + public Set getTagInclusionSet() { + return tagInclusionSet; + } - /** - * @see org.apache.sling.rewriter.Generator#setContentHandler(org.xml.sax.ContentHandler) - */ - public void setContentHandler(ContentHandler handler) { - this.contentHandler = handler; - } + public void setTagInclusionSet(Set tagInclusionSet) { + this.tagInclusionSet = tagInclusionSet; + } - @Override - public void write(char cbuf[], int off, int len) throws IOException { - this.update(cbuf, 0, len); - } + /** + * @see org.apache.sling.rewriter.Generator#setContentHandler(org.xml.sax.ContentHandler) + */ + public void setContentHandler(ContentHandler handler) { + this.contentHandler = handler; + } - @Override - public void write(int b) throws IOException { - final char[] buf = new char[] { (char) b }; - this.update(buf, 0, buf.length); - } + @Override + public void write(char cbuf[], int off, int len) throws IOException { + this.update(cbuf, 0, len); + } - @Override - public void close() throws IOException { - // nothing to do - } + @Override + public void write(int b) throws IOException { + final char[] buf = new char[] { (char) b }; + this.update(buf, 0, buf.length); + } - @Override - public void flush() throws IOException { - flushBuffer(); - - // send 0-length characters that eventually let the serializer flush the - // underlying writer - try { - this.contentHandler.characters(new char[0], 0, 0); - } catch (SAXException e) { - throw handle(e); + @Override + public void close() throws IOException { + // nothing to do } - } - /** - * Feed characters to the parser. - * - * @param buf - * character buffer - * @param off - * offset where characters start - * @param len - * length of affected buffer - */ - public void update(char[] buf, int off, int len) throws IOException { - if (!this.started) { + @Override + public void flush() throws IOException { + flushBuffer(); + + // send 0-length characters that eventually let the serializer flush the + // underlying writer try { - this.contentHandler.startDocument(); - } catch (SAXException se) { - this.handle(se); + this.contentHandler.characters(new char[0], 0, 0); + } catch (SAXException e) { + throw handle(e); } - this.started = true; } - int start = off; - int end = off + len; - for (int curr = start; curr < end; curr++) { - char c = buf[curr]; + /** + * Feed characters to the parser. + * + * @param buf + * character buffer + * @param off + * offset where characters start + * @param len + * length of affected buffer + */ + public void update(char[] buf, int off, int len) throws IOException { + if (!this.started) { + try { + this.contentHandler.startDocument(); + } catch (SAXException se) { + this.handle(se); + } + this.started = true; + } + int start = off; + int end = off + len; + + for (int curr = start; curr < end; curr++) { + char c = buf[curr]; - switch (parseState) { - case PS_OUTSIDE: - if (c == '<') { - if (curr > start) { - try { - this.contentHandler.characters(buf, start, curr - start); - } catch (SAXException e) { - throw handle(e); + switch (parseState) { + case PS_OUTSIDE: + if (c == '<') { + if (curr > start) { + try { + this.contentHandler.characters(buf, start, curr - start); + } catch (SAXException e) { + throw handle(e); + } } - } - start = curr; - parseState = PS_TAG; - parseSubState = 0; - tagType = TT_MAYBE; - resetTagName(); - } - break; - case PS_TAG: - switch (parseSubState) { - case -1: - if (c == '"' || c == '\'') { - quoteChar = c; - prevParseState = parseState; - parseState = PS_STRING; - parseSubState = -1; - } else if (c == '>') { - parseState = PS_OUTSIDE; - } - break; - case 0: - if (c == '!') { - parseState = PS_COMMENT; + start = curr; + parseState = PS_TAG; parseSubState = 0; - tagType = TT_NONE; - flushBuffer(); - } else if (c == '"' || c == '\'') { - quoteChar = c; - prevParseState = parseState; - parseState = PS_STRING; - parseSubState = -1; - tagType = TT_NONE; - flushBuffer(); - } else if (c == '>') { - parseState = PS_OUTSIDE; - tagType = TT_NONE; - flushBuffer(); - } else if (!Character.isWhitespace(c)) { - tagNameBuffer.write(c); - parseSubState = 1; - } else { - parseSubState = -1; - tagType = TT_NONE; - flushBuffer(); + tagType = TT_MAYBE; + resetTagName(); } break; - case 1: - if (c == '"' || c == '\'') { - if (tagIncluded(getTagName())) { - tagType = TT_TAG; + case PS_TAG: + switch (parseSubState) { + case -1: + if (c == '"' || c == '\'') { + quoteChar = c; + prevParseState = parseState; + parseState = PS_STRING; + parseSubState = -1; + } else if (c == '>') { + parseState = PS_OUTSIDE; + } + break; + case 0: + if (c == '!') { + parseState = PS_COMMENT; + parseSubState = 0; + tagType = TT_NONE; + flushBuffer(); + } else if (c == '"' || c == '\'') { + quoteChar = c; + prevParseState = parseState; + parseState = PS_STRING; + parseSubState = -1; + tagType = TT_NONE; + flushBuffer(); + } else if (c == '>') { + parseState = PS_OUTSIDE; + tagType = TT_NONE; + flushBuffer(); + } else if (!Character.isWhitespace(c)) { + tagNameBuffer.write(c); + parseSubState = 1; } else { + parseSubState = -1; tagType = TT_NONE; flushBuffer(); } - parseSubState = 2; - quoteChar = c; - prevParseState = parseState; - parseState = PS_STRING; - } else if (c == '>') { - if (tagIncluded(getTagName())) { - processTag(buf, start, curr - start + 1); - start = curr + 1; + break; + case 1: + if (c == '"' || c == '\'') { + if (tagIncluded(getTagName())) { + tagType = TT_TAG; + } else { + tagType = TT_NONE; + flushBuffer(); + } + parseSubState = 2; + quoteChar = c; + prevParseState = parseState; + parseState = PS_STRING; + } else if (c == '>') { + if (tagIncluded(getTagName())) { + processTag(buf, start, curr - start + 1); + start = curr + 1; + tagType = TT_NONE; + parseState = getTagName() + .equalsIgnoreCase("SCRIPT") ? PS_SCRIPT + : PS_OUTSIDE; + parseSubState = 0; + } else { + tagType = TT_NONE; + flushBuffer(); + parseState = PS_OUTSIDE; + } + } else if (Character.isWhitespace(c)) { + if (tagIncluded(getTagName())) { + tagType = TT_TAG; + } else { + tagType = TT_NONE; + flushBuffer(); + } + parseSubState = 2; + } else { + tagNameBuffer.write(c); + } + break; + case 2: + if (c == '"' || c == '\'') { + quoteChar = c; + prevParseState = parseState; + parseState = PS_STRING; + } else if (c == '>') { + if (tagType == TT_TAG) { + processTag(buf, start, curr - start + 1); + start = curr + 1; + } else { + flushBuffer(); + } tagType = TT_NONE; - parseState = getTagName() - .equalsIgnoreCase("SCRIPT") ? PS_SCRIPT + parseState = getTagName().equalsIgnoreCase("SCRIPT") ? PS_SCRIPT : PS_OUTSIDE; parseSubState = 0; - } else { + } + break; + } + break; + case PS_COMMENT: + switch (parseSubState) { + case 0: + if (c == '-') { + parseSubState++; + } else if (c == '"' || c == '\'') { + quoteChar = c; + prevParseState = PS_TAG; + parseState = PS_STRING; + parseSubState = -1; tagType = TT_NONE; flushBuffer(); + } else if (c == '>') { parseState = PS_OUTSIDE; - } - } else if (Character.isWhitespace(c)) { - if (tagIncluded(getTagName())) { - tagType = TT_TAG; + tagType = TT_NONE; + flushBuffer(); } else { + parseState = PS_TAG; + parseSubState = -1; tagType = TT_NONE; flushBuffer(); } - parseSubState = 2; - } else { - tagNameBuffer.write(c); - } - break; - case 2: - if (c == '"' || c == '\'') { - quoteChar = c; - prevParseState = parseState; - parseState = PS_STRING; - } else if (c == '>') { - if (tagType == TT_TAG) { - processTag(buf, start, curr - start + 1); - start = curr + 1; + break; + case 1: + if (c == '-') { + parseSubState++; + } else if (c == '"' || c == '\'') { + quoteChar = c; + prevParseState = PS_TAG; + parseState = PS_STRING; + parseSubState = -1; + tagType = TT_NONE; + flushBuffer(); + } else if (c == '>') { + parseState = PS_OUTSIDE; + tagType = TT_NONE; + flushBuffer(); } else { + parseState = PS_TAG; + parseSubState = -1; + tagType = TT_NONE; flushBuffer(); } - tagType = TT_NONE; - parseState = getTagName().equalsIgnoreCase("SCRIPT") ? PS_SCRIPT - : PS_OUTSIDE; - parseSubState = 0; - } - break; - } - break; - case PS_COMMENT: - switch (parseSubState) { - case 0: - if (c == '-') { - parseSubState++; - } else if (c == '"' || c == '\'') { - quoteChar = c; - prevParseState = PS_TAG; - parseState = PS_STRING; - parseSubState = -1; - tagType = TT_NONE; - flushBuffer(); - } else if (c == '>') { - parseState = PS_OUTSIDE; - tagType = TT_NONE; - flushBuffer(); - } else { - parseState = PS_TAG; - parseSubState = -1; - tagType = TT_NONE; - flushBuffer(); - } - break; - case 1: - if (c == '-') { - parseSubState++; - } else if (c == '"' || c == '\'') { - quoteChar = c; - prevParseState = PS_TAG; - parseState = PS_STRING; - parseSubState = -1; - tagType = TT_NONE; - flushBuffer(); - } else if (c == '>') { - parseState = PS_OUTSIDE; - tagType = TT_NONE; - flushBuffer(); - } else { - parseState = PS_TAG; - parseSubState = -1; - tagType = TT_NONE; - flushBuffer(); - } - break; - case 2: - if (c == '-') { - parseSubState++; - } - break; - case 3: - if (c == '-') { - parseSubState++; - } else { - parseSubState = 2; - } - break; - case 4: - if (c == '>') { - parseState = PS_OUTSIDE; - } else { - parseSubState = 2; + break; + case 2: + if (c == '-') { + parseSubState++; + } + break; + case 3: + if (c == '-') { + parseSubState++; + } else { + parseSubState = 2; + } + break; + case 4: + if (c == '>') { + parseState = PS_OUTSIDE; + } else { + parseSubState = 2; + } + break; } break; - } - break; - case PS_SCRIPT: - switch (parseSubState) { - case 0: - if (c == '<') { - if (curr > start) { - try { - this.contentHandler.characters(buf, start, curr - start); - } catch (SAXException e) { - throw handle(e); + case PS_SCRIPT: + switch (parseSubState) { + case 0: + if (c == '<') { + if (curr > start) { + try { + this.contentHandler.characters(buf, start, curr - start); + } catch (SAXException e) { + throw handle(e); + } } + start = curr; + tagType = TT_MAYBE; + parseSubState++; } - start = curr; - tagType = TT_MAYBE; - parseSubState++; - } - break; - case 1: - if (c == '/') { - parseSubState++; - } else { - tagType = TT_NONE; - flushBuffer(); - parseSubState = 0; - } - break; - case 2: - if (c == 'S' || c == 's') { - parseSubState++; - } else { - tagType = TT_NONE; - flushBuffer(); - parseSubState = 0; - } - break; - case 3: - if (c == 'C' || c == 'c') { - parseSubState++; - } else { - tagType = TT_NONE; - flushBuffer(); - parseSubState = 0; - } - break; - case 4: - if (c == 'R' || c == 'r') { - parseSubState++; - } else { - tagType = TT_NONE; - flushBuffer(); - parseSubState = 0; - } - break; - case 5: - if (c == 'I' || c == 'i') { - parseSubState++; - } else { - tagType = TT_NONE; - flushBuffer(); - parseSubState = 0; - } - break; - case 6: - if (c == 'P' || c == 'p') { - parseSubState++; - } else { - tagType = TT_NONE; - flushBuffer(); - parseSubState = 0; - } - break; - case 7: - if (c == 'T' || c == 't') { - parseSubState++; - } else { - tagType = TT_NONE; - flushBuffer(); - parseSubState = 0; - } - break; - case 8: - if (c == '>') { - if (tagIncluded("SCRIPT")) { - processTag(buf, start, curr - start + 1); - start = curr + 1; + break; + case 1: + if (c == '/') { + parseSubState++; + } else { + tagType = TT_NONE; + flushBuffer(); + parseSubState = 0; + } + break; + case 2: + if (c == 'S' || c == 's') { + parseSubState++; + } else { + tagType = TT_NONE; + flushBuffer(); + parseSubState = 0; + } + break; + case 3: + if (c == 'C' || c == 'c') { + parseSubState++; + } else { + tagType = TT_NONE; + flushBuffer(); + parseSubState = 0; + } + break; + case 4: + if (c == 'R' || c == 'r') { + parseSubState++; } else { + tagType = TT_NONE; flushBuffer(); + parseSubState = 0; } - tagType = TT_NONE; - parseState = PS_OUTSIDE; + break; + case 5: + if (c == 'I' || c == 'i') { + parseSubState++; + } else { + tagType = TT_NONE; + flushBuffer(); + parseSubState = 0; + } + break; + case 6: + if (c == 'P' || c == 'p') { + parseSubState++; + } else { + tagType = TT_NONE; + flushBuffer(); + parseSubState = 0; + } + break; + case 7: + if (c == 'T' || c == 't') { + parseSubState++; + } else { + tagType = TT_NONE; + flushBuffer(); + parseSubState = 0; + } + break; + case 8: + if (c == '>') { + if (tagIncluded("SCRIPT")) { + processTag(buf, start, curr - start + 1); + start = curr + 1; + } else { + flushBuffer(); + } + tagType = TT_NONE; + parseState = PS_OUTSIDE; + } + break; } break; - } - break; - case PS_STRING: - if (c == quoteChar) { - parseState = prevParseState; + case PS_STRING: + if (c == quoteChar) { + parseState = prevParseState; + } + break; + } + } + if (start < end) { + if (tagType == TT_NONE) { + try { + this.contentHandler.characters(buf, start, end - start); + } catch (SAXException e) { + throw handle(e); + } + } else { + buffer.write(buf, start, end - start); } - break; } } - if (start < end) { - if (tagType == TT_NONE) { + + /** + * Return a flag indicating whether the parser has still some undigested + * characters left. + * + * @return true if the parser still contains characters + * false otherwise + */ + public boolean isEmpty() { + return buffer.size() == 0; + } + + /** + * Finish the parsing process. This forces the parser to flush the + * characters still held in its internal buffer, regardless of the parsing + * state. + */ + public void finished() throws IOException { + flushBuffer(); + if ( this.started ) { try { - this.contentHandler.characters(buf, start, end - start); + this.contentHandler.endDocument(); } catch (SAXException e) { throw handle(e); } - } else { - buffer.write(buf, start, end - start); - } - } - } - - /** - * Return a flag indicating whether the parser has still some undigested - * characters left. - * - * @return true if the parser still contains characters - * false otherwise - */ - public boolean isEmpty() { - return buffer.size() == 0; - } - /** - * Finish the parsing process. This forces the parser to flush the - * characters still held in its internal buffer, regardless of the parsing - * state. - */ - public void finished() throws IOException { - flushBuffer(); - if ( this.started ) { - try { - this.contentHandler.endDocument(); - } catch (SAXException e) { - throw handle(e); } - } - } - /** - * Clears the internal tagname buffer and cache - */ - protected void resetTagName() { - tagName = null; - tagNameBuffer.reset(); - } - - /** - * Returns the tagname scanned and resets the internal tagname buffer - * - * @return tagname - */ - protected String getTagName() { - if (tagName == null) { - tagName = tagNameBuffer.toString(); + /** + * Clears the internal tagname buffer and cache + */ + protected void resetTagName() { + tagName = null; + tagNameBuffer.reset(); } - return tagName; - } - /** - * Flush internal buffer. This forces the parser to flush the characters - * still held in its internal buffer, regardless of the parsing state. - */ - protected void flushBuffer() throws IOException { - if (buffer.size() > 0) { - char[] ch = buffer.toCharArray(); - try { - this.contentHandler.characters(ch, 0, ch.length); - } catch (SAXException e) { - throw handle(e); + /** + * Returns the tagname scanned and resets the internal tagname buffer + * + * @return tagname + */ + protected String getTagName() { + if (tagName == null) { + tagName = tagNameBuffer.toString(); } - buffer.reset(); + return tagName; } - } - /** - * Returns a flag indicating whether the specified tag should be included in - * the parsing process. - * - * @param tagName - * tag name - * @return true if the tag should be processed, else - * false - */ - protected boolean tagIncluded(String tagName) { - return tagInclusionSet == null - || tagInclusionSet.contains(tagName.toUpperCase()); - } + /** + * Flush internal buffer. This forces the parser to flush the characters + * still held in its internal buffer, regardless of the parsing state. + */ + protected void flushBuffer() throws IOException { + if (buffer.size() > 0) { + char[] ch = buffer.toCharArray(); + try { + this.contentHandler.characters(ch, 0, ch.length); + } catch (SAXException e) { + throw handle(e); + } + buffer.reset(); + } + } - /** - * Decompose a tag and feed it to the document handler. - * - * @param ch - * character data - * @param off - * offset where character data starts - * @param len - * length of character data - */ - protected void processTag(char[] ch, int off, int len) throws IOException { - buffer.write(ch, off, len); + /** + * Returns a flag indicating whether the specified tag should be included in + * the parsing process. + * + * @param tagName + * tag name + * @return true if the tag should be processed, else + * false + */ + protected boolean tagIncluded(String tagName) { + return tagInclusionSet == null + || tagInclusionSet.contains(tagName.toUpperCase()); + } - char[] snippet = buffer.toCharArray(); + /** + * Decompose a tag and feed it to the document handler. + * + * @param ch + * character data + * @param off + * offset where character data starts + * @param len + * length of character data + */ + protected void processTag(char[] ch, int off, int len) throws IOException { + buffer.write(ch, off, len); + + char[] snippet = buffer.toCharArray(); + + tokenizer.tokenize(snippet, 0, snippet.length); + if (!tokenizer.endTag()) { + final AttributeList attributes = tokenizer.attributes(); + final String tagName = tokenizer.tagName(); + this.atts.clear(); - tokenizer.tokenize(snippet, 0, snippet.length); - if (!tokenizer.endTag()) { - final AttributeList attributes = tokenizer.attributes(); - final String tagName = tokenizer.tagName(); - this.atts.clear(); - - final char[] quotes = new char[attributes.attributeCount()]; - int index = 0; - final Iterator names = attributes.attributeNames(); - while (names.hasNext()) { - final String name = names.next(); - final String value = attributes.getValue(name); - if (value != null) { - this.atts.addAttribute("", name, name, "CDATA", value); - } else { - this.atts.addAttribute("", name, name, "CDATA", ""); + final char[] quotes = new char[attributes.attributeCount()]; + int index = 0; + final Iterator names = attributes.attributeNames(); + while (names.hasNext()) { + final String name = names.next(); + final String value = attributes.getValue(name); + if (value != null) { + this.atts.addAttribute("", name, name, "CDATA", value); + } else { + this.atts.addAttribute("", name, name, "CDATA", ""); + } + quotes[index] = attributes.getQuoteChar(name); + index++; } - quotes[index] = attributes.getQuoteChar(name); - index++; - } - if ( index > 0 ) { - this.atts.addAttribute(NAMESPACE, QUOTES_ATTR, QUOTES_ATTR, "CDATA", new String(quotes)); - } - try { - if (tokenizer.endSlash()) { - // just tell the contentHandler via attribute that an end slash is needed - this.atts.addAttribute("", END_SLASH_ATTR, END_SLASH_ATTR, "CDATA", ""); + if ( index > 0 ) { + this.atts.addAttribute(NAMESPACE, QUOTES_ATTR, QUOTES_ATTR, "CDATA", new String(quotes)); + } + try { + if (tokenizer.endSlash()) { + // just tell the contentHandler via attribute that an end slash is needed + this.atts.addAttribute("", END_SLASH_ATTR, END_SLASH_ATTR, "CDATA", ""); + } + this.contentHandler.startElement("", tagName, tagName, this.atts); + } catch (SAXException e) { + throw handle(e); + } + } else { + try { + final String tagName = tokenizer.tagName(); + this.contentHandler.endElement("", tagName, tagName); + } catch (SAXException e) { + throw handle(e); } - this.contentHandler.startElement("", tagName, tagName, this.atts); - } catch (SAXException e) { - throw handle(e); - } - } else { - try { - final String tagName = tokenizer.tagName(); - this.contentHandler.endElement("", tagName, tagName); - } catch (SAXException e) { - throw handle(e); } - } - buffer.reset(); - } + buffer.reset(); + } - protected final IOException handle(SAXException se) { - if ( se.getCause() != null && se.getCause() instanceof IOException) { - return (IOException)se.getCause(); - } - final IOException ioe = new IOException("Unable to parse document"); - ioe.initCause(se); - return ioe; + protected final IOException handle(SAXException se) { + if ( se.getCause() != null && se.getCause() instanceof IOException) { + return (IOException)se.getCause(); + } + final IOException ioe = new IOException("Unable to parse document"); + ioe.initCause(se); + return ioe; + } } } Propchange: incubator/sling/whiteboard/rewriter/src/main/java/org/apache/sling/rewriter/impl/HtmlGeneratorFactory.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: incubator/sling/whiteboard/rewriter/src/main/java/org/apache/sling/rewriter/impl/HtmlGeneratorFactory.java ------------------------------------------------------------------------------ svn:keywords = author date id revision rev url Propchange: incubator/sling/whiteboard/rewriter/src/main/java/org/apache/sling/rewriter/impl/HtmlGeneratorFactory.java ------------------------------------------------------------------------------ svn:mime-type = text/plain