Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   *  Licensed 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.wookie.flatpack;
 
 import java.io.File;
 
 import  org.apache.wookie.util.html.HtmlCleaner;
 import  org.apache.wookie.util.html.IHtmlProcessor;

This class is used to help create a "Flatpack" - a .wgt archive that can also include WidgetInstance information.

This class is invoked by the W3CWidgetFactory class when invoked by FlatpackFactory to unpack a Widget.

The purpose of this processor is to modify the Widget package, for Features that are flagged as being suitable for "flattening", it adds their resources to the exported package, injects script and stylesheet references in each start file, and removes the <feature> element from the Widget's config.xml

 
 public class FlatpackProcessor implements IStartPageProcessor {

  
Constructs a FlatpackProcessor

Parameters:
instance
 
   public FlatpackProcessor() {
   }

  
Processes the start file.

Parameters:
startFile the HTML file to process
model the Widget object to apply
Content:
the Content element to apply TODO implement
 
   public void processStartFile(File startFileW3CWidget model,
       IContentEntity contentthrows Exception {
     if (startFile == null)
       throw new Exception("Start file cannot be processed: file is null");
     if (!startFile.exists())
       throw new Exception(
           "Start file cannot be processed:  file does not exist");
     if (!(startFile.canWrite() && startFile.canRead()))
       throw new Exception(
           "Start file cannot be processed: read or write permissions missing");
     if (model == null)
       throw new Exception(
           "Start file cannot be processed: widget model is null");
     //
     // Set the HTML processing engine to use to modify the Widget start files
     // and pass it a reference to a FileReader it can use to read the start file
     //
     IHtmlProcessor engine = new HtmlCleaner();
     engine.setReader(new FileReader(startFile));
     //
     // Process Features
     //
     addFlattenedFeatures(startFile.getParentFile(), engine, model);
     FileWriter writer = new FileWriter(startFile);
     engine.process(writer);
   }

  
Adds features to widget start file by injecting javascript and stylesheets required by each supported feature in the model.

Parameters:
engine
model
Throws:
Exception if a feature cannot be found and instantiated for the widget.
  private void addFlattenedFeatures(File widgetFolder, IHtmlProcessor engine,
      W3CWidget modelthrows Exception {
    for (IFeatureEntity feature : model.getFeatures()) {
      for (IFeature theFeature : Features.getFeatures()) {
        if (theFeature.getName().equals(feature.getName())
            && theFeature.flattenOnExport()) {
          addScripts(engine, theFeature);
          addStylesheets(engine, theFeature);
        }
      }
    }
  }

  
Adds scripts for a given feature

Parameters:
engine
feature
  private void addScripts(IHtmlProcessor engineIFeature feature) {
    if (feature.scripts() != null) {
      for (String script : feature.scripts()) {
        // remove the "base" path
        // FIXME this is fragile - consider replacing with a better solution
        script = script.replace(WidgetRuntimeHelper.getWebContextPath() + "/features/""");
        engine.injectScript(script);
      }
    }
  }

  
Adds stylesheets for a given feature

Parameters:
engine
feature
  private void addStylesheets(IHtmlProcessor engineIFeature feature) {
    if (feature.stylesheets() != null) {
      for (String style : feature.stylesheets()) {
        // remove the "base" path
        // FIXME this is fragile - consider replacing with a better solution
        style = style.replace(WidgetRuntimeHelper.getWebContextPath() + "/features/""");
        engine.injectStylesheet(style);
      }
    }
  }
New to GrepCode? Check out our FAQ X