1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
|
private void appliquerColorationSyntaxique(final String xml) {
final int length = FUeEditor.this.textSource.getText().length();
Job job = new Job("Application de la coloration syntaxique") {
public static final int LIMITE = 2000;
@Override
protected IStatus run(IProgressMonitor monitor) {
final ArrayList<StyleRange> rangeList = new ArrayList<StyleRange>();
boolean debutDejaAffiche = false;
int finAnalise = 0;
//Entete
Pattern patternDebutEntete = Pattern.compile("<\\?[\\p{ASCII}]+\\?>");
Matcher matcher = patternDebutEntete.matcher(xml);
if(matcher.find()){
finAnalise = matcher.end();
appliquerStyleEntete(matcher.start(), finAnalise, rangeList);
}
//recherche les balises suivantes
//Pattern patternBalise = Pattern.compile("<[\\p{ASCII}]+>");
Pattern patternBalise = Pattern.compile("<[^>]+>");
Pattern baliseVide = Pattern.compile("<[\\s]*[\\S&&[^>]]+[>]+");
Pattern debutBalise = Pattern.compile("<[\\s]*[\\S&&[^>]]+[\\s]+");
Pattern attributs = Pattern.compile("[\\s]*[\\S]+[=][\\s]*[\"][^\"]*[\"]");
matcher = patternBalise.matcher(xml.substring(finAnalise));
while(matcher.find()){
int indexDebutBalise = matcher.start() + finAnalise;
int indexFinBalise = matcher.end() + finAnalise;
//appliquerStyleBalise(indexDebutBalise, indexFinBalise, rangeList);
String balise = xml.substring(indexDebutBalise, indexFinBalise);
//Si assez de coloration a été calculée, affichage du début
if (indexDebutBalise > LIMITE && !debutDejaAffiche){
afficherColoration(rangeList, MODE_AFFICHER_DEBUT);
debutDejaAffiche = true;
}
Matcher matcherBalise = baliseVide.matcher(balise);
if(matcherBalise.find()){
//balise sans attributs
int indexFin = matcherBalise.end() + indexDebutBalise;
int indexDebut = matcherBalise.start() + indexDebutBalise;
appliquerStyleBalise(indexDebut, indexFin, rangeList);
}else {
//balise avec attributs
matcherBalise = debutBalise.matcher(balise);
if(matcherBalise.find()){
int indexFinPremierePartieBalise = matcherBalise.end() + indexDebutBalise;
int indexDebutPremierePartieBalise = matcherBalise.start() + indexDebutBalise;
appliquerStyleBalise(indexDebutPremierePartieBalise, indexFinPremierePartieBalise, rangeList);
//recherche les attributs
String attributsList = xml.substring(indexFinPremierePartieBalise, indexFinBalise);
//Pattern attributs = Pattern.compile("[\\s]*[\\s]+[=][\\s]*[\"][\\p{ASCII}]*[\"]");
Matcher matcherAttributs = attributs.matcher(attributsList);
int indexFinAttribut = indexFinPremierePartieBalise;
while (matcherAttributs.find()){
indexFinAttribut = matcherAttributs.end() + indexFinPremierePartieBalise;
int indexDebutAttribut = matcherAttributs.start() + indexFinPremierePartieBalise;
//appliquerStyleValeurAttribut(indexDebutAttribut, indexFinAttribut, rangeList);
String attribut = xml.substring(indexDebutAttribut, indexFinAttribut);
int indexDuEgal = attribut.indexOf('=');
int indexFinNomAttribut = indexDebutAttribut + indexDuEgal;
appliquerStyleEntete(indexDebutAttribut, indexFinNomAttribut +1, rangeList);
appliquerStyleValeurAttribut(indexFinNomAttribut + 1,indexFinAttribut , rangeList);
}
appliquerStyleBalise(indexFinAttribut,indexFinBalise , rangeList);
}
}
}
if (length < LIMITE){
//Rien n'a encore été affiché
afficherColoration(rangeList, MODE_AFFICHER_ENTIER);
} else {
//Le début a déjà été affiché
afficherColoration(rangeList, MODE_AFFICHER_FIN);
}
return Status.OK_STATUS;
}
public static final int MODE_AFFICHER_DEBUT = 1;
public static final int MODE_AFFICHER_FIN = 2;
public static final int MODE_AFFICHER_ENTIER = 3;
private void afficherColoration(final ArrayList<StyleRange> rangeList, final int mode) {
Display.getDefault().asyncExec(new Runnable() {
@Override
public void run() {
if (mode == MODE_AFFICHER_ENTIER){
//affiche tout d'un coup
FUeEditor.this.textSource.replaceStyleRanges(0, length, rangeList.toArray(new StyleRange[rangeList.size()]));
}else if (mode == MODE_AFFICHER_DEBUT) {
//affiche les premiers 2000 caractères
FUeEditor.this.textSource.replaceStyleRanges(0, LIMITE, rangeList.toArray(new StyleRange[rangeList.size()]));
FUeEditor.this.textSource.update();
}else if (mode == MODE_AFFICHER_FIN){
FUeEditor.this.textSource.replaceStyleRanges(LIMITE, length - LIMITE, rangeList.toArray(new StyleRange[rangeList.size()]));
}
}
});
}
};
job.setSystem(true);
job.schedule();
} |
Partager