All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.intellij.util.xml.model.impl.DomModelImpl Maven / Gradle / Ivy

Go to download

A packaging of the IntelliJ Community Edition dom-openapi library. This is release number 1 of trunk branch 142.

The newest version!
/*
 * Copyright 2000-2014 JetBrains s.r.o.
 *
 * 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 com.intellij.util.xml.model.impl;

import com.intellij.openapi.project.Project;
import com.intellij.psi.xml.XmlFile;
import com.intellij.util.NullableFunction;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.xml.*;
import com.intellij.util.xml.model.DomModel;
import org.jetbrains.annotations.NotNull;

import java.util.Collections;
import java.util.List;
import java.util.Set;

/**
 * @author Dmitry Avdeev
 */
public class DomModelImpl implements DomModel {

  protected final DomFileElement myMergedModel;
  protected final Set myConfigFiles;
  private Class myClass;
  private Project myProject;

  /**
   * Using this method may result in a large memory usage, since it will keep all the DOM and PSI for all the config files
   * @return
   */
  @Deprecated
  public DomModelImpl(T mergedModel, @NotNull Set configFiles) {
    myMergedModel = DomUtil.getFileElement(mergedModel);
    myConfigFiles = configFiles;
  }

  public DomModelImpl(DomFileElement mergedModel, @NotNull Set configFiles) {
    myMergedModel = DomUtil.getFileElement(mergedModel);
    myConfigFiles = configFiles;
  }

  public DomModelImpl(@NotNull Set configFiles, Class clazz, Project project) {
    myProject = project;
    myMergedModel = null;
    myConfigFiles = configFiles;
    myClass = clazz;
  }

  @Override
  @NotNull
  public T getMergedModel() {
    if (myMergedModel == null) {
      final DomManager domManager = DomManager.getDomManager(myProject);
      return domManager.createModelMerger().mergeModels(myClass, ContainerUtil.mapNotNull(myConfigFiles, new NullableFunction() {
        @Override
        public T fun(XmlFile xmlFile) {
          DomFileElement fileElement = domManager.getFileElement(xmlFile, myClass);
          return fileElement == null ? null : fileElement.getRootElement();
        }
      }));
    }
    return myMergedModel.getRootElement();
  }

  @Override
  @NotNull
  public Set getConfigFiles() {
    return myConfigFiles;
  }

  @Override
  @NotNull
  public List> getRoots() {
    if (myMergedModel == null) {
      return ContainerUtil.mapNotNull(myConfigFiles, new NullableFunction>() {
        @Override
        public DomFileElement fun(XmlFile xmlFile) {
          return DomManager.getDomManager(xmlFile.getProject()).getFileElement(xmlFile, myClass);
        }
      });
    }
    return myMergedModel instanceof MergedObject ? ((MergedObject) myMergedModel).getImplementations() : Collections.singletonList(myMergedModel);
  }

  @NotNull
  public Project getProject() {
    return myProject;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy