コード例 #1
0
  /**
   * Writes a single blob file to store all that the DataMerger knows about.
   *
   * @param blobRootFolder the root folder where blobs are store.
   * @param consumer the merge consumer that was used by the merge.
   * @throws MergingException if something goes wrong
   * @see #loadFromBlob(File, boolean)
   */
  public void writeBlobTo(@NonNull File blobRootFolder, @NonNull MergeConsumer<I> consumer)
      throws MergingException {
    // write "compact" blob
    DocumentBuilder builder;

    try {
      builder = mFactory.newDocumentBuilder();
      Document document = builder.newDocument();

      Node rootNode = document.createElement(NODE_MERGER);
      // add the version code.
      NodeUtils.addAttribute(document, rootNode, null, ATTR_VERSION, MERGE_BLOB_VERSION);

      document.appendChild(rootNode);

      for (S dataSet : mDataSets) {
        Node dataSetNode = document.createElement(NODE_DATA_SET);
        rootNode.appendChild(dataSetNode);

        dataSet.appendToXml(dataSetNode, document, consumer);
      }

      // write merged items
      writeMergedItems(document, rootNode);

      String content = XmlUtils.toXml(document, true /*preserveWhitespace*/);

      try {
        createDir(blobRootFolder);
      } catch (IOException ioe) {
        throw new MergingException(ioe).setFile(blobRootFolder);
      }
      File file = new File(blobRootFolder, FN_MERGER_XML);
      try {
        Files.write(content, file, Charsets.UTF_8);
      } catch (IOException ioe) {
        throw new MergingException(ioe).setFile(file);
      }
    } catch (ParserConfigurationException e) {
      throw new MergingException(e);
    }
  }
コード例 #2
0
  @Override
  protected void postWriteAction() throws ConsumerException {

    // now write the values files.
    for (String key : mValuesResMap.keySet()) {
      // the key is the qualifier.

      // check if we have to write the file due to deleted values.
      // also remove it from that list anyway (to detect empty qualifiers later).
      boolean mustWriteFile = mQualifierWithDeletedValues.remove(key);

      // get the list of items to write
      List<ResourceItem> items = mValuesResMap.get(key);

      // now check if we really have to write it
      if (!mustWriteFile) {
        for (ResourceItem item : items) {
          if (item.isTouched()) {
            mustWriteFile = true;
            break;
          }
        }
      }

      if (mustWriteFile) {
        String folderName =
            key.isEmpty()
                ? ResourceFolderType.VALUES.getName()
                : ResourceFolderType.VALUES.getName() + RES_QUALIFIER_SEP + key;

        try {
          File valuesFolder = new File(getRootFolder(), folderName);
          createDir(valuesFolder);
          File outFile = new File(valuesFolder, FN_VALUES_XML);

          DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
          factory.setNamespaceAware(true);
          factory.setValidating(false);
          factory.setIgnoringComments(true);
          DocumentBuilder builder;

          builder = factory.newDocumentBuilder();
          Document document = builder.newDocument();

          Node rootNode = document.createElement(TAG_RESOURCES);
          document.appendChild(rootNode);

          Collections.sort(items);

          ResourceFile currentFile = null;
          for (ResourceItem item : items) {
            ResourceFile source = item.getSource();
            if (source != currentFile) {
              currentFile = source;
              rootNode.appendChild(document.createTextNode("\n"));
              File file = source.getFile();
              rootNode.appendChild(document.createComment(createPathComment(file)));
              rootNode.appendChild(document.createTextNode("\n"));
            }
            Node adoptedNode = NodeUtils.adoptNode(document, item.getValue());
            rootNode.appendChild(adoptedNode);
          }

          String content;
          try {
            content = XmlPrettyPrinter.prettyPrint(document, true);
          } catch (Throwable t) {
            content = XmlUtils.toXml(document, false);
          }

          Files.write(content, outFile, Charsets.UTF_8);
        } catch (Throwable t) {
          throw new ConsumerException(t);
        }
      }
    }

    // now remove empty values files.
    for (String key : mQualifierWithDeletedValues) {
      String folderName =
          key != null && !key.isEmpty()
              ? ResourceFolderType.VALUES.getName() + RES_QUALIFIER_SEP + key
              : ResourceFolderType.VALUES.getName();

      removeOutFile(folderName, FN_VALUES_XML);
    }
  }