Android Open Source - codemap Syntax Highlighter






From Project

Back to project page codemap.

License

The source code is released under:

GNU General Public License

If you think the Android project codemap listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.hdweiss.codemap.util;
/* ww  w.j av  a2s . c  om*/
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.text.Html;
import android.text.SpannableString;

import com.hdweiss.codemap.data.CscopeEntry;

public class SyntaxHighlighter {
  
  private final String[] C_KEYWORDS =  {
      "auto",
      "break",
      "case",
      "char",
      "const",
      "continue",
      "default",
      "do",
      "double",
      "else",
      "enum",
      "extern",
      "float",
      "for",
      "goto",
      "if",
      "int",
      "long",
      "register",
      "return",
      "short",
      "unsigned",
      "signed",
      "sizeof",
      "static",
      "struct",
      "switch",
      "typedef",
      "union",
      "void",
      "volatile",
      "while" };
          
  private String content;
  
  public SyntaxHighlighter(String contents) {
    this.content = contents;
  }
  
  public SpannableString formatToHtml() {
    formatNewline();
    highlightKeywords();
    highlightComments();
    
    SpannableString spannableString = new SpannableString(
        Html.fromHtml(content));
    return spannableString;
  }
  
  private void formatNewline() {
    content = content.replaceAll("\n\r", "<br />");
    content = content.replaceAll("\n", "<br />");
    content = content.replaceAll("\r", "<br />");
  }
  
  private void highlightKeywords() {
    for(String keyword: C_KEYWORDS) {
      content = content.replaceAll(keyword, "<font color=\"purple\">" + keyword + "</font>");
    }
  }
  
  private void highlightComments() {
    content = content.replaceAll("/\\*.*?\\*/", "<font color=\"green\">$0</font>");
    content = content.replaceAll("//[^\n]*?\n", "<font color=\"green\">$0</font>");
  }
  
  public void markupReferences(ArrayList<CscopeEntry> references) {
    StringBuilder result = new StringBuilder(content);
    for(CscopeEntry entry: references) {
      Matcher matcher = Pattern.compile("(" + Pattern.quote(entry.actualName) + ")(?:\\s?\\()").matcher(result);
      
      if(matcher.find()) {
        result.insert(matcher.end(1), "</a>");
        result.insert(matcher.start(1), "<a href=\"" + entry.actualName + "\">");
      }
    }
    content = result.toString();
  }
}




Java Source Code List

com.hdweiss.codemap.data.CodeMapApp.java
com.hdweiss.codemap.data.CscopeEntry.java
com.hdweiss.codemap.data.CscopeWrapper.java
com.hdweiss.codemap.data.Cscope.java
com.hdweiss.codemap.data.ICodeMapItem.java
com.hdweiss.codemap.data.JGitWrapper.java
com.hdweiss.codemap.data.ProjectController.java
com.hdweiss.codemap.data.Project.java
com.hdweiss.codemap.data.SerializableItem.java
com.hdweiss.codemap.data.SerializableLink.java
com.hdweiss.codemap.util.AbsoluteLayout.java
com.hdweiss.codemap.util.CodeMapCursorPoint.java
com.hdweiss.codemap.util.CodeMapPoint.java
com.hdweiss.codemap.util.FlowLayout.java
com.hdweiss.codemap.util.ObjectSerializer.java
com.hdweiss.codemap.util.SpanUtils.java
com.hdweiss.codemap.util.SyntaxHighlighter.java
com.hdweiss.codemap.util.Utils.java
com.hdweiss.codemap.util.ZoomableAbsoluteLayout.java
com.hdweiss.codemap.util.ZoomableLinearLayout.java
com.hdweiss.codemap.view.CodeMapActivity.java
com.hdweiss.codemap.view.CodeMapTabListener.java
com.hdweiss.codemap.view.Preferences.java
com.hdweiss.codemap.view.project.ProjectAdapter.java
com.hdweiss.codemap.view.project.ProjectBrowser.java
com.hdweiss.codemap.view.project.ProjectItemView.java
com.hdweiss.codemap.view.project.ProjectWizard.java
com.hdweiss.codemap.view.workspace.CollisionManager.java
com.hdweiss.codemap.view.workspace.FindDeclarationTask.java
com.hdweiss.codemap.view.workspace.WorkspaceController.java
com.hdweiss.codemap.view.workspace.WorkspaceFragment.java
com.hdweiss.codemap.view.workspace.WorkspaceStateLoader.java
com.hdweiss.codemap.view.workspace.WorkspaceState.java
com.hdweiss.codemap.view.workspace.WorkspaceViewListeners.java
com.hdweiss.codemap.view.workspace.WorkspaceView.java
com.hdweiss.codemap.view.workspace.browser.WorkspaceBrowserAdapter.java
com.hdweiss.codemap.view.workspace.browser.WorkspaceBrowser.java
com.hdweiss.codemap.view.workspace.fragments.CodeMapAnnotation.java
com.hdweiss.codemap.view.workspace.fragments.CodeMapFunction.java
com.hdweiss.codemap.view.workspace.fragments.CodeMapImage.java
com.hdweiss.codemap.view.workspace.fragments.CodeMapItem.java
com.hdweiss.codemap.view.workspace.fragments.CodeMapLink.java
com.hdweiss.codemap.view.workspace.fragments.FunctionLinkSpan.java
com.hdweiss.codemap.view.workspace.outline.CscopeEntryAdapter.java
com.hdweiss.codemap.view.workspace.outline.OutlineAdapter.java
com.hdweiss.codemap.view.workspace.outline.OutlineBrowser.java
com.hdweiss.codemap.view.workspace.outline.OutlineItemComparator.java
com.hdweiss.codemap.view.workspace.outline.OutlineItemView.java
com.hdweiss.codemap.view.workspace.outline.OutlineItem.java