Package de.innovationgate.eclipse.editors.tml.markers

Source Code of de.innovationgate.eclipse.editors.tml.markers.TMLTagValidationMarkingHandler

/*******************************************************************************
* Copyright (c) 2009, 2010 Innovation Gate GmbH.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
*     Innovation Gate GmbH - initial API and implementation
******************************************************************************/
package de.innovationgate.eclipse.editors.tml.markers;

import java.text.ParseException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITypedRegion;
import org.eclipse.ui.texteditor.IDocumentProvider;

import de.innovationgate.eclipse.editors.Plugin;
import de.innovationgate.eclipse.editors.config.TMLTag;
import de.innovationgate.eclipse.editors.config.TMLTagDefinitions;
import de.innovationgate.eclipse.editors.helpers.MarkingHandler;
import de.innovationgate.eclipse.editors.helpers.TMLTagValidator;
import de.innovationgate.eclipse.editors.tml.TMLPartitionScanner;
import de.innovationgate.eclipse.editors.tml.TMLRegion;
import de.innovationgate.eclipse.utils.wga.WGADesignStructureHelper;
import de.innovationgate.wga.model.VersionCompliance;

/**
* iterates over all tml tags and execute configured {@link TMLTagValidator}
*
*
*/
public class TMLTagValidationMarkingHandler implements MarkingHandler {

  private Map<String, TMLTagValidator> _allValidators = new HashMap<String, TMLTagValidator>();
  private VersionCompliance _versionCompliance;
 
  public TMLTagValidationMarkingHandler() {
//    // retrieve all configured validators
//    Iterator<TMLTag> tags = TMLTagDefinitions.getInstance(WGADesignStructureHelper.getWGAVersionCompliance(file)).getTags().iterator();
//    while (tags.hasNext()) {
//      Iterator<String> validators = tags.next().getValidators().iterator();
//      while (validators.hasNext()) {
//        String validatorClassName = validators.next();
//        if (!_allValidators.containsKey(validatorClassName)) {
//          TMLTagValidator validator = null;
//          if (validatorClassName != null) {
//            try {
//              validator = (TMLTagValidator) Class.forName(validatorClassName).newInstance();
//            } catch (Exception e) {
//              Plugin.getDefault().getLog().log(new Status(Status.ERROR, Plugin.PLUGIN_ID, "Unable to intitialize attribute value lookup '" + validatorClassName + "'.", e));
//            }
//          }
//          if (validator != null) {
//            _allValidators.put(validatorClassName, validator);
//          }
//        }
//      }
//    }
  }
 
  private void updateValidators(IFile file){
   
    _allValidators.clear();
    // retrieve all configured validators
    Iterator<TMLTag> tags = TMLTagDefinitions.getInstance(_versionCompliance).getTags().iterator();
    while (tags.hasNext()) {
      Iterator<String> validators = tags.next().getValidators().iterator();
      while (validators.hasNext()) {
        String validatorClassName = validators.next();
        if (!_allValidators.containsKey(validatorClassName)) {
          TMLTagValidator validator = null;
          if (validatorClassName != null) {
            try {
              validator = (TMLTagValidator) Class.forName(validatorClassName).newInstance();
            } catch (Exception e) {
              Plugin.getDefault().getLog().log(new Status(Status.ERROR, Plugin.PLUGIN_ID, "Unable to intitialize attribute value lookup '" + validatorClassName + "'.", e));
            }
          }
          if (validator != null) {
            _allValidators.put(validatorClassName, validator);
          }
        }
      }
    }
  }
 
  public void createMarkers(IFile file, IDocument document) throws CoreException
    updateValidators(file);
    try {
      VersionCompliance versionCompliance = WGADesignStructureHelper.getWGAVersionCompliance(file);
      // clear markers of all validators
      Iterator<TMLTagValidator> validators = _allValidators.values().iterator();
      while (validators.hasNext()) {
        TMLTagValidator validator = validators.next();
        file.deleteMarkers(validator.getMarkerID(), true, IResource.DEPTH_ZERO);
      }
     
      ITypedRegion[] partitions = document.computePartitioning(0, document.getLength());
      for (ITypedRegion partition : partitions) {
        if (partition.getType().equals(TMLPartitionScanner.TML_TAG_START)) {
          try {           
            TMLRegion tmlInfo = TMLRegion.parse(partition, document);
            TMLTag tag = TMLTagDefinitions.getInstance(versionCompliance).getTagByName(tmlInfo.getTagName());
            if (tag != null) {
              executeValidators(file, document, partition, tmlInfo, tag, versionCompliance);             
            }
          } catch (ParseException e) {
          }         
        }
      }
    } catch (BadLocationException e) {     
    }
  }

  private void executeValidators(IFile file, IDocument document, ITypedRegion partition, TMLRegion tmlInfo, TMLTag tag, VersionCompliance versionCompliance) {
    //updateValidators(file);
    Iterator<String> validators = tag.getValidators().iterator();
   
    while (validators.hasNext()) {
      String validatorClassName = validators.next();
      TMLTagValidator validator = _allValidators.get(validatorClassName);
      if (validator != null) {
        try {
          validator.setVersionCompliance(versionCompliance);
          validator.validateAndMark(file, document, partition, tmlInfo, tag);
        } catch (Exception e) {
          Plugin.getDefault().getLog().log(new Status(Status.ERROR, Plugin.PLUGIN_ID, "Execution of tag validator '" + validatorClassName + "' failed.", e));
        }
      }       
    } 
  }

  public void setWGAVersionCompliance(VersionCompliance versionCompliance) {
    _versionCompliance = versionCompliance;   
  }

  public void setDocumentProvider(IDocumentProvider provider) {
    // TODO Auto-generated method stub
   
  }

  public void createMarkers(IResource resource) throws CoreException {
    // TODO Auto-generated method stub
   
  }
}
TOP

Related Classes of de.innovationgate.eclipse.editors.tml.markers.TMLTagValidationMarkingHandler

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.