Example #1
0
  private static void mapJsp(FileContent inputData, final List<LinkInfo> result) {
    final FileViewProvider viewProvider = inputData.getPsiFile().getViewProvider();

    PsiFile psiFile = null;
    if (viewProvider instanceof TemplateLanguageFileViewProvider) {
      final Language dataLanguage =
          ((TemplateLanguageFileViewProvider) viewProvider).getTemplateDataLanguage();
      if (dataLanguage == HTMLLanguage.INSTANCE || dataLanguage == XHTMLLanguage.INSTANCE) {
        psiFile = viewProvider.getPsi(dataLanguage);
      }
    } else {
      psiFile = viewProvider.getPsi(viewProvider.getBaseLanguage());
    }

    if (psiFile != null) {
      final XmlRecursiveElementVisitor visitor =
          new XmlRecursiveElementVisitor() {
            @Override
            public void visitXmlTag(XmlTag tag) {
              if (LINK.equalsIgnoreCase(tag.getLocalName())) {

                final String href = getAttributeValue(tag, HREF_ATTR);
                final String media = getAttributeValue(tag, MEDIA_ATTR);
                final String type = getAttributeValue(tag, TYPE_ATTR);
                final String rel = getAttributeValue(tag, REL_ATTR);
                final String title = getAttributeValue(tag, TITLE_ATTR);

                addResult(
                    result,
                    tag.getTextOffset(),
                    href,
                    media,
                    type,
                    rel,
                    title,
                    isHrefScripted(tag));
              }

              super.visitXmlTag(tag);
            }
          };

      psiFile.accept(visitor);
    }
  }
Example #2
0
  private static void mapHtml(FileContent inputData, Language language, List<LinkInfo> result) {
    final Lexer original =
        HTMLLanguage.INSTANCE == language
            ? new HtmlHighlightingLexer()
            : new XHtmlHighlightingLexer();
    final Lexer lexer =
        new FilterLexer(
            original,
            new FilterLexer.Filter() {
              public boolean reject(final IElementType type) {
                return XmlElementType.XML_WHITE_SPACE == type;
              }
            });

    final CharSequence data = inputData.getContentAsText();
    lexer.start(data);

    IElementType tokenType = lexer.getTokenType();
    boolean linkTag = false;
    while (tokenType != null) {
      if (XmlElementType.XML_TAG_NAME == tokenType) {
        final String tagName =
            data.subSequence(lexer.getTokenStart(), lexer.getTokenEnd()).toString();
        linkTag = LINK.equalsIgnoreCase(tagName);
        // if (BODY_TAG.equalsIgnoreCase(tagName)) {
        //  break; // there are no LINK tags under the body
        // }
      }

      if (linkTag && XmlElementType.XML_NAME == tokenType) {
        int linkTagOffset = lexer.getTokenStart();
        String href = null;
        String type = null;
        String media = null;
        String rel = null;
        String title = null;

        while (true) {
          if (tokenType == null
              || tokenType == XmlTokenType.XML_END_TAG_START
              || tokenType == XmlTokenType.XML_EMPTY_ELEMENT_END
              || tokenType == XmlTokenType.XML_START_TAG_START) {
            break;
          }

          if (XmlElementType.XML_NAME == tokenType) {
            final String attrName =
                data.subSequence(lexer.getTokenStart(), lexer.getTokenEnd()).toString();
            if (HREF_ATTR.equalsIgnoreCase(attrName)) {
              href = parseAttributeValue(lexer, data);
            } else if (MEDIA_ATTR.equalsIgnoreCase(attrName)) {
              media = parseAttributeValue(lexer, data);
            } else if (TYPE_ATTR.equalsIgnoreCase(attrName)) {
              type = parseAttributeValue(lexer, data);
            } else if (REL_ATTR.equalsIgnoreCase(attrName)) {
              rel = parseAttributeValue(lexer, data);
            } else if (TITLE_ATTR.equalsIgnoreCase(attrName)) {
              title = parseAttributeValue(lexer, data);
            }
          }

          lexer.advance();
          tokenType = lexer.getTokenType();
        }

        addResult(result, linkTagOffset, href, media, type, rel, title, false);
      }

      lexer.advance();
      tokenType = lexer.getTokenType();
    }
  }