private boolean addXmlFileChanges(IFile file, CompositeChange changes, boolean isManifest) {
    IModelManager modelManager = StructuredModelManager.getModelManager();
    IStructuredModel model = null;
    try {
      model = modelManager.getExistingModelForRead(file);
      if (model == null) {
        model = modelManager.getModelForRead(file);
      }
      if (model != null) {
        IStructuredDocument document = model.getStructuredDocument();
        if (model instanceof IDOMModel) {
          IDOMModel domModel = (IDOMModel) model;
          Element root = domModel.getDocument().getDocumentElement();
          if (root != null) {
            List<TextEdit> edits = new ArrayList<TextEdit>();
            if (isManifest) {
              addManifestReplacements(edits, root, document);
            } else {
              addLayoutReplacements(edits, root, document);
            }
            if (!edits.isEmpty()) {
              MultiTextEdit rootEdit = new MultiTextEdit();
              rootEdit.addChildren(edits.toArray(new TextEdit[edits.size()]));
              TextFileChange change = new TextFileChange(file.getName(), file);
              change.setTextType(EXT_XML);
              change.setEdit(rootEdit);
              changes.add(change);
            }
          }
        } else {
          return false;
        }
      }

      return true;
    } catch (IOException e) {
      AdtPlugin.log(e, null);
    } catch (CoreException e) {
      AdtPlugin.log(e, null);
    } finally {
      if (model != null) {
        model.releaseFromRead();
      }
    }

    return false;
  }
예제 #2
0
    /**
     * Returns the content type of document
     *
     * @param document - assumes document is not null
     * @return String content type of given document
     */
    private String getContentType() {
      String type = null;

      IModelManager mgr = StructuredModelManager.getModelManager();
      IStructuredModel model = null;
      try {
        model = mgr.getExistingModelForRead(document);
        if (model != null) {
          type = model.getContentTypeIdentifier();
        }
      } finally {
        if (model != null) {
          model.releaseFromRead();
        }
      }
      return type;
    }
  /**
   * Method validateEdit.
   *
   * @param file org.eclipse.core.resources.IFile
   * @param context org.eclipse.swt.widgets.Shell
   * @return IStatus
   */
  private static IStatus validateEdit(IFile file, Shell context) {
    if (file == null || !file.exists()) return STATUS_ERROR;
    if (!(file.isReadOnly())) return STATUS_OK;

    IPath location = file.getLocation();

    final long beforeModifiedFromJavaIO =
        (location != null) ? location.toFile().lastModified() : IResource.NULL_STAMP;
    final long beforeModifiedFromIFile = file.getModificationStamp();

    IStatus status = ResourcesPlugin.getWorkspace().validateEdit(new IFile[] {file}, context);
    if (!status.isOK()) return status;

    final long afterModifiedFromJavaIO =
        (location != null) ? location.toFile().lastModified() : IResource.NULL_STAMP;
    final long afterModifiedFromIFile = file.getModificationStamp();

    if (beforeModifiedFromJavaIO != afterModifiedFromJavaIO
        || beforeModifiedFromIFile != afterModifiedFromIFile) {
      IModelManager manager = StructuredModelManager.getModelManager();
      IStructuredModel model = manager.getExistingModelForRead(file);
      if (model != null) {
        if (!model.isDirty()) {
          try {
            file.refreshLocal(IResource.DEPTH_ONE, null);
          } catch (CoreException e) {
            return STATUS_ERROR;
          } finally {
            model.releaseFromRead();
          }
        } else {
          model.releaseFromRead();
        }
      }
    }

    if ((beforeModifiedFromJavaIO != afterModifiedFromJavaIO)
        || (beforeModifiedFromIFile != afterModifiedFromIFile)) {
      // the file is replaced. Modification cannot be
      // applied.
      return STATUS_ERROR;
    }
    return STATUS_OK;
  }
  /**
   * Returns the closest IndexedRegion for the offset and viewer allowing for differences between
   * viewer offsets and model positions. note: this method returns an IndexedRegion for read only
   *
   * @param viewer the viewer whose document is used to compute the proposals
   * @param documentOffset an offset within the document for which completions should be computed
   * @return an IndexedRegion
   */
  public static IndexedRegion getNodeAt(ITextViewer viewer, int documentOffset) {

    if (viewer == null) return null;

    IndexedRegion node = null;
    IModelManager mm = StructuredModelManager.getModelManager();
    IStructuredModel model = null;
    if (mm != null) model = mm.getExistingModelForRead(viewer.getDocument());
    try {
      if (model != null) {
        int lastOffset = documentOffset;
        node = model.getIndexedRegion(documentOffset);
        while (node == null && lastOffset >= 0) {
          lastOffset--;
          node = model.getIndexedRegion(lastOffset);
        }
      }
    } finally {
      if (model != null) model.releaseFromRead();
    }
    return node;
  }