com.android.ide.common.res2
Class ResourceMerger

java.lang.Object
  extended by com.android.ide.common.res2.ResourceMerger

public class ResourceMerger
extends java.lang.Object

Implementation of DataMerger for ResourceSet, ResourceItem, and ResourceFile.


Field Summary
protected  javax.xml.parsers.DocumentBuilderFactory mFactory
           
protected  java.util.Map<java.lang.String,java.util.Map<java.lang.String,ResourceItem>> mMergedItems
          Map of items that are purely results of merges (ie item that made up of several original items).
 
Constructor Summary
ResourceMerger()
           
 
Method Summary
 void addDataSet(S resourceSet)
          adds a new DataSet and overlays it on top of the existing DataSet.
 boolean checkValidUpdate(java.util.List<S> dataSets)
          Checks that a loaded merger can be updated with a given list of DataSet.
 void cleanBlob(java.io.File blobRootFolder)
           
protected  void createDir(java.io.File folder)
           
protected  ResourceSet createFromXml(org.w3c.dom.Node node)
           
 FileValidity<S> findDataSetContaining(java.io.File file)
          Finds the DataSet that contains the given file.
 FileValidity<S> findDataSetContaining(java.io.File file, FileValidity<S> fileValidity)
          Finds the DataSet that contains the given file.
 com.google.common.collect.ListMultimap<java.lang.String,I> getDataMap()
          Returns a map of the data items.
 java.util.List<S> getDataSets()
          Returns the list of ResourceSet objects.
 boolean loadFromBlob(java.io.File blobRootFolder, boolean incrementalState)
          Loads the merger state from a blob file.
protected  void loadMergedItems(org.w3c.dom.Node mergedItemsNode)
           
 void mergeData(MergeConsumer<I> consumer, boolean doCleanUp)
          Merges the data into a given consumer.
protected  void mergeItems(java.lang.String dataItemKey, java.util.List<ResourceItem> items, MergeConsumer<ResourceItem> consumer)
          Merge items together, and register the merged items with the given consumer.
protected  boolean requiresMerge(java.lang.String dataItemKey)
           
 int size()
          Returns the number of items.
 java.lang.String toString()
           
 void writeBlobTo(java.io.File blobRootFolder, MergeConsumer<I> consumer)
          Writes a single blob file to store all that the DataMerger knows about.
protected  void writeMergedItems(org.w3c.dom.Document document, org.w3c.dom.Node rootNode)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

mMergedItems

protected final java.util.Map<java.lang.String,java.util.Map<java.lang.String,ResourceItem>> mMergedItems
Map of items that are purely results of merges (ie item that made up of several original items). The first map key is the associated qualifier for the items, the second map key is the item name.


mFactory

@NonNull
protected final javax.xml.parsers.DocumentBuilderFactory mFactory
Constructor Detail

ResourceMerger

public ResourceMerger()
Method Detail

createFromXml

protected ResourceSet createFromXml(org.w3c.dom.Node node)

requiresMerge

protected boolean requiresMerge(@NonNull
                                java.lang.String dataItemKey)

mergeItems

protected void mergeItems(@NonNull
                          java.lang.String dataItemKey,
                          @NonNull
                          java.util.List<ResourceItem> items,
                          @NonNull
                          MergeConsumer<ResourceItem> consumer)
                   throws MergingException
Merge items together, and register the merged items with the given consumer.

Parameters:
dataItemKey - the key for the items
items - the items, from lower priority to higher priority.
consumer - the consumer to receive the merged items.
Throws:
MergingException

loadMergedItems

protected void loadMergedItems(@NonNull
                               org.w3c.dom.Node mergedItemsNode)

writeMergedItems

protected void writeMergedItems(org.w3c.dom.Document document,
                                org.w3c.dom.Node rootNode)

addDataSet

public void addDataSet(S resourceSet)
adds a new DataSet and overlays it on top of the existing DataSet.

Parameters:
resourceSet - the ResourceSet to add.

getDataSets

@NonNull
public java.util.List<S> getDataSets()
Returns the list of ResourceSet objects.

Returns:
the resource sets.

size

public int size()
Returns the number of items.

Returns:
the number of items.
See Also:
DataMap

getDataMap

@NonNull
public com.google.common.collect.ListMultimap<java.lang.String,I> getDataMap()
Returns a map of the data items.

Returns:
a map of items.
See Also:
DataMap

mergeData

public void mergeData(@NonNull
                      MergeConsumer<I> consumer,
                      boolean doCleanUp)
               throws MergingException
Merges the data into a given consumer.

Parameters:
consumer - the consumer of the merge.
doCleanUp - clean up the state to be able to do further incremental merges. If this is a one-shot merge, this can be false to improve performance.
Throws:
MergingException - such as a DuplicateDataException or a MergeConsumer.ConsumerException if something goes wrong

writeBlobTo

public void writeBlobTo(@NonNull
                        java.io.File blobRootFolder,
                        @NonNull
                        MergeConsumer<I> consumer)
                 throws MergingException
Writes a single blob file to store all that the DataMerger knows about.

Parameters:
blobRootFolder - the root folder where blobs are store.
consumer - the merge consumer that was used by the merge.
Throws:
MergingException - if something goes wrong
See Also:
loadFromBlob(File, boolean)

loadFromBlob

public boolean loadFromBlob(@NonNull
                            java.io.File blobRootFolder,
                            boolean incrementalState)
                     throws MergingException
Loads the merger state from a blob file. This can be loaded into two different ways that differ only by the state on the DataItem objects. If incrementalState is true then the items that are on disk are marked as written (DataItem.isWritten() returning true. This is to be used by MergeWriter to update a merged res folder. If false, the items are marked as touched, and this can be used to feed a new ResourceRepository object.

Parameters:
blobRootFolder - the folder containing the blob.
incrementalState - whether to load into an incremental state or a new state.
Returns:
true if the blob was loaded.
Throws:
MergingException - if something goes wrong
See Also:
writeBlobTo(File, MergeConsumer)

cleanBlob

public void cleanBlob(@NonNull
                      java.io.File blobRootFolder)

checkValidUpdate

public boolean checkValidUpdate(java.util.List<S> dataSets)
Checks that a loaded merger can be updated with a given list of DataSet. For now this means the sets haven't changed.

Parameters:
dataSets - the resource sets.
Returns:
true if the update can be performed. false if a full merge should be done.

findDataSetContaining

public FileValidity<S> findDataSetContaining(@NonNull
                                             java.io.File file)
Finds the DataSet that contains the given file. This methods will also performs some checks to make sure the given file is a valid file in the data set. All the information is set in a FileValidity object that is returned. FileValidity contains information about the changed file including: - is it from an known set, is it an ignored file, or is it unknown? - what data set does it belong to - what source folder in the data set does it belong to. "belong" means that the DataSet has a source file/folder that is the root folder of this file. The folder and/or file doesn't have to exist.

Parameters:
file - the file to check
Returns:
a new FileValidity.

findDataSetContaining

public FileValidity<S> findDataSetContaining(@NonNull
                                             java.io.File file,
                                             @Nullable
                                             FileValidity<S> fileValidity)
Finds the DataSet that contains the given file. This methods will also performs some checks to make sure the given file is a valid file in the data set. All the information is set in a FileValidity object that is returned. If an instance is passed, then this object is filled instead, and returned. FileValidity contains information about the changed file including: - is it from an known set, is it an ignored file, or is it unknown? - what data set does it belong to - what source folder in the data set does it belong to. "belong" means that the DataSet has a source file/folder that is the root folder of this file. The folder and/or file doesn't have to exist.

Parameters:
file - the file to check
fileValidity - an optional FileValidity to fill. If null a new one is returned.
Returns:
a new FileValidity or the one given as a parameter.

createDir

protected void createDir(java.io.File folder)
                  throws java.io.IOException
Throws:
java.io.IOException

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object