org.apache.shindig.gadgets.rewrite
Class MutableContent

java.lang.Object
  extended by org.apache.shindig.gadgets.rewrite.MutableContent
Direct Known Subclasses:
HttpResponseBuilder

public class MutableContent
extends Object

Object that maintains a String representation of arbitrary contents and a consistent view of those contents as an HTML parse tree.


Constructor Summary
MutableContent(GadgetHtmlParser contentParser, HttpResponse contentSource)
          Construct with HttpResponse so we can defer string decoding until we actually need the content.
MutableContent(GadgetHtmlParser contentParser, String content)
          Construct with decoded string content
 
Method Summary
 void addPipelinedData(String key, Object value)
           
 void documentChanged()
          Notification that the content of the document has changed.
 String getContent()
          Retrieves the current content for this object in String form.
 InputStream getContentBytes()
          Retrieves the current content for this object as an InputStream.
 Document getDocument()
          Retrieves the object contents in parsed form, if a GadgetHtmlParser is configured and is able to parse the string contents appropriately.
 int getNumChanges()
           
 Map<String,Object> getPipelinedData()
           
protected  byte[] getRawContentBytes()
           
 boolean hasDocument()
          True if current state has a parsed document.
protected  void incrementNumChanges()
           
static void notifyEdit(Document doc)
           
 void setContent(String newContent)
          Sets the object's content as a raw String.
 void setContentBytes(byte[] newBytes)
          Sets content to new byte array, with unspecified charset.
 void setContentBytes(byte[] newBytes, Charset newEncoding)
          Sets the object's contentBytes as the given raw input.
protected  void setContentBytesState(byte[] newBytes, Charset newEncoding)
          Sets internal state having to do with content bytes, from the provided byte array and charset.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MutableContent

public MutableContent(GadgetHtmlParser contentParser,
                      String content)
Construct with decoded string content


MutableContent

public MutableContent(GadgetHtmlParser contentParser,
                      HttpResponse contentSource)
Construct with HttpResponse so we can defer string decoding until we actually need the content. Given that we dont rewrite many mime types this is a performance advantage

Method Detail

notifyEdit

public static void notifyEdit(Document doc)

getContent

public String getContent()
Retrieves the current content for this object in String form. If content has been retrieved in parse tree form and has been edited, the String form is computed from the parse tree by rendering it. It is strongly encouraged to avoid switching between retrieval of parse tree (through getParseTree), with subsequent edits and retrieval of String contents to avoid repeated serialization and deserialization. As a final fallback, if content has been set as bytes, interprets them as a UTF8 String.

Returns:
Renderable/active content.

setContent

public void setContent(String newContent)
Sets the object's content as a raw String. Note, this operation may clear the document if the content has changed

Parameters:
newContent - New content.

getContentBytes

public InputStream getContentBytes()
Retrieves the current content for this object as an InputStream.

Returns:
Active content as InputStream.

getRawContentBytes

protected byte[] getRawContentBytes()

setContentBytes

public void setContentBytes(byte[] newBytes,
                            Charset newEncoding)
Sets the object's contentBytes as the given raw input. If ever interpreted as a String, the data will be decoded as the encoding specified. Note, this operation may clear the document if the content has changed. Also note, it's mandated that the new bytes array will NOT be modified by the caller of this API. The array is not copied, for performance reasons. If the caller may modify a byte array, it MUST pass in a new copy.

Parameters:
newBytes - New content.

setContentBytes

public final void setContentBytes(byte[] newBytes)
Sets content to new byte array, with unspecified charset. It is recommended to use the setContentBytes(byte[], Charset) API instead, where possible.

Parameters:
newBytes - New content.

setContentBytesState

protected void setContentBytesState(byte[] newBytes,
                                    Charset newEncoding)
Sets internal state having to do with content bytes, from the provided byte array and charset. This MUST be the only place in which MutableContent's notion of encoding is mutated.

Parameters:
newBytes - New content.
newEncoding - Encoding for the bytes, or null for unspecified.

documentChanged

public void documentChanged()
Notification that the content of the document has changed. Causes the content string and bytes to be cleared.


getDocument

public Document getDocument()
Retrieves the object contents in parsed form, if a GadgetHtmlParser is configured and is able to parse the string contents appropriately. To modify the object's contents by parse tree after setting new String contents, this method must be called again. However, this practice is highly discouraged, as parsing a tree from String is a costly operation and should be done at most once per rewrite.


getNumChanges

public int getNumChanges()

incrementNumChanges

protected void incrementNumChanges()

hasDocument

public boolean hasDocument()
True if current state has a parsed document. Allows rewriters to switch mode based on which content is most readily available


addPipelinedData

public void addPipelinedData(String key,
                             Object value)

getPipelinedData

public Map<String,Object> getPipelinedData()


Copyright © 2007-2012. All Rights Reserved.