Java 类com.intellij.psi.impl.source.xml.XmlEntityCache 实例源码

项目:intellij-ce-playground    文件:XmlPsiUtil.java   
private boolean processElement(PsiElement child, boolean deepFlag, boolean wideFlag, boolean processIncludes) {
  if (deepFlag) {
    if (!processXmlElements(child, true, wideFlag, processIncludes)) {
      return false;
    }
  }
  else {
    if (child instanceof XmlEntityRef) {
      if (!processXmlElements(child, false, wideFlag, processIncludes)) return false;
    }
    else if (child instanceof XmlConditionalSection) {
      if (!processXmlElements(child, false, wideFlag, processIncludes)) return false;
    }
    else if (processIncludes && XmlIncludeHandler.isXInclude(child)) {
      if (!processXmlElements(child, false, wideFlag, processIncludes)) return false;
    }
    else if (!processor.execute(child)) return false;
  }
  if (targetFile != null && child instanceof XmlEntityDecl) {
    XmlEntityDecl xmlEntityDecl = (XmlEntityDecl)child;
    XmlEntityCache.cacheParticularEntity(targetFile, xmlEntityDecl);
  }
  return true;
}
项目:intellij-ce-playground    文件:XmlPsiUtil.java   
private static PsiElement parseEntityDecl(final XmlEntityDecl entityDecl,
                                          final PsiFile targetFile,
                                          final XmlEntityDecl.EntityContextType type,
                                          final XmlEntityRef entityRef) {
  CachedValue<PsiElement> value;
  synchronized (PsiLock.LOCK) { // we depend on targetFile and entityRef
    value = entityRef.getUserData(PARSED_DECL_KEY);
    //    return entityDecl.parse(targetFile, type);

    if (value == null) {
      value = CachedValuesManager.getManager(entityDecl.getProject()).createCachedValue(new CachedValueProvider<PsiElement>() {
        @Override
        public Result<PsiElement> compute() {
          final PsiElement res = entityDecl.parse(targetFile, type, entityRef);
          if (res == null) return new Result<PsiElement>(res, targetFile);
          if (!entityDecl.isInternalReference()) XmlEntityCache.copyEntityCaches(res.getContainingFile(), targetFile);
          return new Result<PsiElement>(res, res.getUserData(XmlElement.DEPENDING_ELEMENT), entityDecl, targetFile, entityRef);
        }
      }, false);
      entityRef.putUserData(PARSED_DECL_KEY, value);
    }
  }

  return value.getValue();
}
项目:tools-idea    文件:XmlPsiUtil.java   
private boolean processElement(PsiElement child, boolean deepFlag, boolean wideFlag, boolean processIncludes) {
  if (deepFlag) {
    if (!processXmlElements(child, true, wideFlag, processIncludes)) {
      return false;
    }
  }
  else {
    if (child instanceof XmlEntityRef) {
      if (!processXmlElements(child, false, wideFlag, processIncludes)) return false;
    }
    else if (child instanceof XmlConditionalSection) {
      if (!processXmlElements(child, false, wideFlag, processIncludes)) return false;
    }
    else if (processIncludes && XmlIncludeHandler.isXInclude(child)) {
      if (!processXmlElements(child, false, wideFlag, processIncludes)) return false;
    }
    else if (!processor.execute(child)) return false;
  }
  if (targetFile != null && child instanceof XmlEntityDecl) {
    XmlEntityDecl xmlEntityDecl = (XmlEntityDecl)child;
    XmlEntityCache.cacheParticularEntity(targetFile, xmlEntityDecl);
  }
  return true;
}
项目:tools-idea    文件:XmlPsiUtil.java   
private static PsiElement parseEntityDecl(final XmlEntityDecl entityDecl,
                                          final PsiFile targetFile,
                                          final XmlEntityDecl.EntityContextType type,
                                          final XmlEntityRef entityRef) {
  synchronized (PsiLock.LOCK) { // we depend on targetFile and entityRef
    CachedValue<PsiElement> value = entityRef.getUserData(PARSED_DECL_KEY);
    //    return entityDecl.parse(targetFile, type);

    if (value == null) {
      value = CachedValuesManager.getManager(entityDecl.getProject()).createCachedValue(new CachedValueProvider<PsiElement>() {
        public Result<PsiElement> compute() {
          final PsiElement res = entityDecl.parse(targetFile, type, entityRef);
          if (res == null) return new Result<PsiElement>(res, targetFile);
          if (!entityDecl.isInternalReference()) XmlEntityCache.copyEntityCaches(res.getContainingFile(), targetFile);
          return new Result<PsiElement>(res, res.getUserData(XmlElement.DEPENDING_ELEMENT), entityDecl, targetFile, entityRef);
        }
      }, false);
      entityRef.putUserData(PARSED_DECL_KEY, value);
    }

    return value.getValue();
  }
}
项目:consulo-xml    文件:XmlPsiUtil.java   
private static PsiElement parseEntityDecl(final XmlEntityDecl entityDecl, final PsiFile targetFile, final XmlEntityDecl.EntityContextType type, final XmlEntityRef entityRef)
{
    CachedValue<PsiElement> value = entityRef.getUserData(PARSED_DECL_KEY);

    if(value == null)
    {
        value = CachedValuesManager.getManager(entityDecl.getProject()).createCachedValue(() ->
        {
            final PsiElement res = entityDecl.parse(targetFile, type, entityRef);
            if(res == null)
            {
                return new CachedValueProvider.Result<>(res, targetFile);
            }
            if(!entityDecl.isInternalReference())
            {
                XmlEntityCache.copyEntityCaches(res.getContainingFile(), targetFile);
            }
            return new CachedValueProvider.Result<>(res, res.getUserData(XmlElement.DEPENDING_ELEMENT), entityDecl, targetFile, entityRef);
        }, false);
        value = ((XmlEntityRefImpl) entityRef).putUserDataIfAbsent(PARSED_DECL_KEY, value);
    }

    return value.getValue();
}
项目:intellij-ce-playground    文件:XmlUtil.java   
@Nullable
public static PsiNamedElement findRealNamedElement(@NotNull final PsiNamedElement _element) {
  PsiElement currentElement = _element;
  final XmlEntityRef lastEntityRef = PsiTreeUtil.getParentOfType(currentElement, XmlEntityRef.class);

  while (!(currentElement instanceof XmlFile)) {
    PsiElement dependingElement = currentElement.getUserData(XmlElement.DEPENDING_ELEMENT);
    if (dependingElement == null) dependingElement = currentElement.getContext();
    currentElement = dependingElement;
    if (dependingElement == null) break;
  }

  if (currentElement != null) {
    final String name = _element.getName();
    if (_element instanceof XmlEntityDecl) {
      final XmlEntityDecl cachedEntity = XmlEntityCache.getCachedEntity((PsiFile)currentElement, name);
      if (cachedEntity != null) return cachedEntity;
    }

    final PsiNamedElement[] result = new PsiNamedElement[1];

    processXmlElements((XmlFile)currentElement, new PsiElementProcessor() {
      @Override
      public boolean execute(@NotNull final PsiElement element) {
        if (element instanceof PsiNamedElement) {
          final String elementName = ((PsiNamedElement)element).getName();

          if (elementName.equals(name) && _element.getClass().isInstance(element)
              || lastEntityRef != null && element instanceof XmlEntityDecl &&
                 elementName.equals(lastEntityRef.getText().substring(1, lastEntityRef.getTextLength() - 1))) {
            result[0] = (PsiNamedElement)element;
            return false;
          }
        }

        return true;
      }
    }, true);

    return result[0];
  }

  return null;
}
项目:intellij-ce-playground    文件:FetchExtResourceAction.java   
private static Set<String> extractEmbeddedFileReferences(XmlFile file, XmlFile context, final String url) {
  final Set<String> result = new LinkedHashSet<String>();
  if (context != null) {
    XmlEntityCache.copyEntityCaches(file, context);
  }

  XmlUtil.processXmlElements(
    file,
    new PsiElementProcessor() {
      @Override
      public boolean execute(@NotNull PsiElement element) {
        if (element instanceof XmlEntityDecl) {
          String candidateName = null;

          for (PsiElement e = element.getLastChild(); e != null; e = e.getPrevSibling()) {
            if (e instanceof XmlAttributeValue && candidateName == null) {
              candidateName = e.getText().substring(1, e.getTextLength() - 1);
            }
            else if (e instanceof XmlToken &&
                     candidateName != null &&
                     (((XmlToken)e).getTokenType() == XmlTokenType.XML_DOCTYPE_PUBLIC ||
                      ((XmlToken)e).getTokenType() == XmlTokenType.XML_DOCTYPE_SYSTEM
                     )
              ) {
              if (!result.contains(candidateName)) {
                result.add(candidateName);
              }
              break;
            }
          }
        }
        else if (element instanceof XmlTag) {
          final XmlTag tag = (XmlTag)element;
          String schemaLocation = tag.getAttributeValue(XmlUtil.SCHEMA_LOCATION_ATT);

          if (schemaLocation != null) {
            // processing xsd:import && xsd:include
            final PsiReference[] references = tag.getAttribute(XmlUtil.SCHEMA_LOCATION_ATT).getValueElement().getReferences();
            if (references.length > 0) {
              String extension = FileUtilRt.getExtension(new File(url).getName());
              final String namespace = tag.getAttributeValue("namespace");
              if (namespace != null &&
                  schemaLocation.indexOf('/') == -1 &&
                  !extension.equals(FileUtilRt.getExtension(schemaLocation))) {
                result.add(namespace.substring(0, namespace.lastIndexOf('/') + 1) + schemaLocation);
              }
              else {
                result.add(schemaLocation);
              }
            }
          }
          else {
            schemaLocation = tag.getAttributeValue(XmlUtil.SCHEMA_LOCATION_ATT, XmlUtil.XML_SCHEMA_INSTANCE_URI);
            if (schemaLocation != null) {
              final StringTokenizer tokenizer = new StringTokenizer(schemaLocation);

              while (tokenizer.hasMoreTokens()) {
                tokenizer.nextToken();
                if (!tokenizer.hasMoreTokens()) break;
                String location = tokenizer.nextToken();
                result.add(location);
              }
            }
          }
        }

        return true;
      }
    },
    true,
    true
  );
  return result;
}
项目:tools-idea    文件:XmlUtil.java   
@Nullable
public static PsiNamedElement findRealNamedElement(@NotNull final PsiNamedElement _element) {
  PsiElement currentElement = _element;
  final XmlEntityRef lastEntityRef = PsiTreeUtil.getParentOfType(currentElement, XmlEntityRef.class);

  while (!(currentElement instanceof XmlFile)) {
    PsiElement dependingElement = currentElement.getUserData(XmlElement.DEPENDING_ELEMENT);
    if (dependingElement == null) dependingElement = currentElement.getContext();
    currentElement = dependingElement;
    if (dependingElement == null) break;
  }

  if (currentElement != null) {
    final String name = _element.getName();
    if (_element instanceof XmlEntityDecl) {
      final XmlEntityDecl cachedEntity = XmlEntityCache.getCachedEntity((PsiFile)currentElement, name);
      if (cachedEntity != null) return cachedEntity;
    }

    final PsiNamedElement[] result = new PsiNamedElement[1];

    processXmlElements((XmlFile)currentElement, new PsiElementProcessor() {
      public boolean execute(@NotNull final PsiElement element) {
        if (element instanceof PsiNamedElement) {
          final String elementName = ((PsiNamedElement)element).getName();

          if (elementName.equals(name) && _element.getClass().isInstance(element)
              || lastEntityRef != null && element instanceof XmlEntityDecl &&
                 elementName.equals(lastEntityRef.getText().substring(1, lastEntityRef.getTextLength() - 1))) {
            result[0] = (PsiNamedElement)element;
            return false;
          }
        }

        return true;
      }
    }, true);

    return result[0];
  }

  return null;
}
项目:tools-idea    文件:FetchExtResourceAction.java   
private static Set<String> extractEmbeddedFileReferences(XmlFile file, XmlFile context, final String url) {
  final Set<String> result = new LinkedHashSet<String>();
  if (context != null) {
    XmlEntityCache.copyEntityCaches(file, context);
  }

  XmlUtil.processXmlElements(
    file,
    new PsiElementProcessor() {
      public boolean execute(@NotNull PsiElement element) {
        if (element instanceof XmlEntityDecl) {
          String candidateName = null;

          for (PsiElement e = element.getLastChild(); e != null; e = e.getPrevSibling()) {
            if (e instanceof XmlAttributeValue && candidateName == null) {
              candidateName = e.getText().substring(1, e.getTextLength() - 1);
            }
            else if (e instanceof XmlToken &&
                     candidateName != null &&
                     (((XmlToken)e).getTokenType() == XmlTokenType.XML_DOCTYPE_PUBLIC ||
                      ((XmlToken)e).getTokenType() == XmlTokenType.XML_DOCTYPE_SYSTEM
                     )
              ) {
              if (!result.contains(candidateName)) {
                result.add(candidateName);
              }
              break;
            }
          }
        }
        else if (element instanceof XmlTag) {
          final XmlTag tag = (XmlTag)element;
          String schemaLocation = tag.getAttributeValue(XmlUtil.SCHEMA_LOCATION_ATT);

          if (schemaLocation != null) {
            // processing xsd:import && xsd:include
            final PsiReference[] references = tag.getAttribute(XmlUtil.SCHEMA_LOCATION_ATT).getValueElement().getReferences();
            if (references.length > 0) {
              String extension = FileUtilRt.getExtension(new File(url).getName());
              final String namespace = tag.getAttributeValue("namespace");
              if (namespace != null &&
                  schemaLocation.indexOf('/') == -1 &&
                  !extension.equals(FileUtilRt.getExtension(schemaLocation))) {
                result.add(namespace.substring(0, namespace.lastIndexOf('/') + 1) + schemaLocation);
              }
              else {
                result.add(schemaLocation);
              }
            }
          }
          else {
            schemaLocation = tag.getAttributeValue(XmlUtil.SCHEMA_LOCATION_ATT, XmlUtil.XML_SCHEMA_INSTANCE_URI);
            if (schemaLocation != null) {
              final StringTokenizer tokenizer = new StringTokenizer(schemaLocation);

              while (tokenizer.hasMoreTokens()) {
                tokenizer.nextToken();
                if (!tokenizer.hasMoreTokens()) break;
                String location = tokenizer.nextToken();
                result.add(location);
              }
            }
          }
        }

        return true;
      }
    },
    true,
    true
  );
  return result;
}
项目:consulo-xml    文件:XmlPsiUtil.java   
private boolean processElement(PsiElement child, boolean deepFlag, boolean wideFlag, boolean processIncludes)
{
    if(deepFlag)
    {
        if(!processXmlElements(child, true, wideFlag, processIncludes))
        {
            return false;
        }
    }
    else
    {
        if(child instanceof XmlEntityRef)
        {
            if(!processXmlElements(child, false, wideFlag, processIncludes))
            {
                return false;
            }
        }
        else if(child instanceof XmlConditionalSection)
        {
            if(!processXmlElements(child, false, wideFlag, processIncludes))
            {
                return false;
            }
        }
        else if(processIncludes && XmlIncludeHandler.isXInclude(child))
        {
            if(!processXmlElements(child, false, wideFlag, processIncludes))
            {
                return false;
            }
        }
        else if(!processor.execute(child))
        {
            return false;
        }
    }
    if(targetFile != null && child instanceof XmlEntityDecl)
    {
        XmlEntityDecl xmlEntityDecl = (XmlEntityDecl) child;
        XmlEntityCache.cacheParticularEntity(targetFile, xmlEntityDecl);
    }
    return true;
}
项目:consulo-xml    文件:XmlUtil.java   
@Nullable
public static PsiNamedElement findRealNamedElement(@NotNull final PsiNamedElement _element)
{
    PsiElement currentElement = _element;
    final XmlEntityRef lastEntityRef = PsiTreeUtil.getParentOfType(currentElement, XmlEntityRef.class);

    while(!(currentElement instanceof XmlFile))
    {
        PsiElement dependingElement = currentElement.getUserData(XmlElement.DEPENDING_ELEMENT);
        if(dependingElement == null)
        {
            dependingElement = currentElement.getContext();
        }
        currentElement = dependingElement;
        if(dependingElement == null)
        {
            break;
        }
    }

    if(currentElement != null)
    {
        final String name = _element.getName();
        if(_element instanceof XmlEntityDecl)
        {
            final XmlEntityDecl cachedEntity = XmlEntityCache.getCachedEntity((PsiFile) currentElement, name);
            if(cachedEntity != null)
            {
                return cachedEntity;
            }
        }

        final PsiNamedElement[] result = new PsiNamedElement[1];

        processXmlElements((XmlFile) currentElement, new PsiElementProcessor()
        {
            @Override
            public boolean execute(@NotNull final PsiElement element)
            {
                if(element instanceof PsiNamedElement)
                {
                    final String elementName = ((PsiNamedElement) element).getName();

                    if(elementName.equals(name) && _element.getClass().isInstance(element) || lastEntityRef != null && element instanceof XmlEntityDecl && elementName.equals(lastEntityRef
                            .getText().substring(1, lastEntityRef.getTextLength() - 1)))
                    {
                        result[0] = (PsiNamedElement) element;
                        return false;
                    }
                }

                return true;
            }
        }, true);

        return result[0];
    }

    return null;
}