@Override
 protected void setUp() throws Exception {
   super.setUp();
   MockGrailsPluginManager pluginManager = new MockGrailsPluginManager();
   Holders.setPluginManager(pluginManager);
   pluginManager.registerMockPlugin(MockHibernatePluginHelper.FAKE_HIBERNATE_PLUGIN);
 }
 public static String getGspEncoding() {
   Config config = Holders.getConfig();
   if (config != null) {
     return config.getProperty(Settings.GSP_VIEW_ENCODING, DEFAULT_ENCODING);
   }
   return DEFAULT_ENCODING;
 }
 /**
  * For each DataBindingSource provided by collectionBindingSource a new instance of targetType is
  * created, data binding is imposed on that instance with the DataBindingSource and the instance
  * is added to the end of collectionToPopulate
  *
  * @param targetType The type of objects to create, must be a concrete class
  * @param collectionToPopulate A collection to populate with new instances of targetType
  * @param collectionBindingSource A CollectionDataBindingSource
  * @since 2.3
  */
 public static <T> void bindToCollection(
     final Class<T> targetType,
     final Collection<T> collectionToPopulate,
     final CollectionDataBindingSource collectionBindingSource)
     throws InstantiationException, IllegalAccessException {
   final GrailsApplication application = Holders.findApplication();
   GrailsDomainClass domain = null;
   if (application != null) {
     domain =
         (GrailsDomainClass)
             application.getArtefact(DomainClassArtefactHandler.TYPE, targetType.getName());
   }
   final List<DataBindingSource> dataBindingSources =
       collectionBindingSource.getDataBindingSources();
   for (final DataBindingSource dataBindingSource : dataBindingSources) {
     final T newObject = targetType.newInstance();
     bindObjectToDomainInstance(
         domain,
         newObject,
         dataBindingSource,
         getBindingIncludeList(newObject),
         Collections.emptyList(),
         null);
     collectionToPopulate.add(newObject);
   }
 }
  public GroovyPageParser(
      String name, String uri, String filename, String gspSource, String expressionCodecName)
      throws IOException {
    Config config = Holders.getConfig();
    if (config != null) {
      setEnableSitemeshPreprocessing(
          config.getProperty(
              GroovyPageParser.CONFIG_PROPERTY_GSP_SITEMESH_PREPROCESS,
              Boolean.class,
              enableSitemeshPreprocessing));
    }

    GrailsPluginInfo pluginInfo = null;
    //        TODO: figure out a way to restore plugin metadata for GSP
    //        if (filename != null && BuildSettingsHolder.getSettings() != null) {
    //            pluginInfo =
    // GrailsPluginUtils.getPluginBuildSettings().getPluginInfoForSource(filename);
    //            if (pluginInfo != null) {
    //                pluginAnnotation = "@GrailsPlugin(name='" + pluginInfo.getName() + "',
    // version='" +
    //                    pluginInfo.getVersion() + "')";
    //            }
    //        }

    OutputEncodingSettings gspConfig = new OutputEncodingSettings(config);

    this.expressionCodecDirectiveValue = expressionCodecName;
    if (expressionCodecDirectiveValue == null) {
      expressionCodecDirectiveValue =
          gspConfig.getCodecSettings(pluginInfo, OutputEncodingSettings.EXPRESSION_CODEC_NAME);
    }
    staticCodecDirectiveValue =
        gspConfig.getCodecSettings(pluginInfo, OutputEncodingSettings.STATIC_CODEC_NAME);
    outCodecDirectiveValue =
        gspConfig.getCodecSettings(pluginInfo, OutputEncodingSettings.OUT_CODEC_NAME);
    taglibCodecDirectiveValue =
        gspConfig.getCodecSettings(pluginInfo, OutputEncodingSettings.TAGLIB_CODEC_NAME);

    Map<String, String> directives = parseDirectives(gspSource);

    if (isSitemeshPreprocessingEnabled(directives.get(SITEMESH_PREPROCESS_DIRECTIVE))) {
      if (LOG.isDebugEnabled()) {
        LOG.debug(
            "Preprocessing "
                + uri
                + " for sitemesh. Replacing head, title, meta and body elements with sitemesh:capture*.");
      }
      // GSP preprocessing for direct sitemesh integration: replace head -> g:captureHead, title ->
      // g:captureTitle, meta -> g:captureMeta, body -> g:captureBody
      gspSource = sitemeshPreprocessor.addGspSitemeshCapturing(gspSource);
      sitemeshPreprocessMode = true;
    }
    scan = new GroovyPageScanner(gspSource, uri);
    pageName = uri;
    environment = Environment.getCurrent();
    makeName(name);
    makeSourceName(filename);
  }
 public static <T> void bindToCollection(
     final Class<T> targetType,
     final Collection<T> collectionToPopulate,
     final ServletRequest request)
     throws InstantiationException, IllegalAccessException {
   final GrailsApplication grailsApplication = Holders.findApplication();
   final CollectionDataBindingSource collectionDataBindingSource =
       createCollectionDataBindingSource(grailsApplication, targetType, request);
   bindToCollection(targetType, collectionToPopulate, collectionDataBindingSource);
 }
 /**
  * Binds the given source object to the given target object performing type conversion if
  * necessary
  *
  * @param object The object to bind to
  * @param source The source object
  * @param include The list of properties to include
  * @param exclude The list of properties to exclude
  * @param filter The prefix to filter by
  * @return A BindingResult or null if it wasn't successful
  */
 public static BindingResult bindObjectToInstance(
     Object object, Object source, List include, List exclude, String filter) {
   if (include == null && exclude == null) {
     include = getBindingIncludeList(object);
   }
   GrailsApplication application = Holders.findApplication();
   GrailsDomainClass domain = null;
   if (application != null) {
     domain =
         (GrailsDomainClass)
             application.getArtefact(DomainClassArtefactHandler.TYPE, object.getClass().getName());
   }
   return bindObjectToDomainInstance(domain, object, source, include, exclude, filter);
 }
 @Override
 protected void tearDown() throws Exception {
   super.tearDown();
   Holders.setPluginManager(null);
 }
  /**
   * Binds the given source object to the given target object performing type conversion if
   * necessary
   *
   * @param domain The GrailsDomainClass instance
   * @param object The object to bind to
   * @param source The source object
   * @param include The list of properties to include
   * @param exclude The list of properties to exclude
   * @param filter The prefix to filter by
   * @see grails.core.GrailsDomainClass
   * @return A BindingResult or null if it wasn't successful
   */
  @SuppressWarnings("unchecked")
  public static BindingResult bindObjectToDomainInstance(
      GrailsDomainClass domain,
      Object object,
      Object source,
      List include,
      List exclude,
      String filter) {
    BindingResult bindingResult = null;
    GrailsApplication grailsApplication = null;
    if (domain != null) {
      grailsApplication = domain.getApplication();
    }
    if (grailsApplication == null) {
      grailsApplication = Holders.findApplication();
    }
    try {
      final DataBindingSource bindingSource =
          createDataBindingSource(grailsApplication, object.getClass(), source);
      final DataBinder grailsWebDataBinder = getGrailsWebDataBinder(grailsApplication);
      grailsWebDataBinder.bind(object, bindingSource, filter, include, exclude);
    } catch (InvalidRequestBodyException e) {
      String messageCode = "invalidRequestBody";
      Class objectType = object.getClass();
      String defaultMessage = "An error occurred parsing the body of the request";
      String[] codes = getMessageCodes(messageCode, objectType);
      bindingResult = new BeanPropertyBindingResult(object, objectType.getName());
      bindingResult.addError(
          new ObjectError(bindingResult.getObjectName(), codes, null, defaultMessage));
    } catch (Exception e) {
      bindingResult = new BeanPropertyBindingResult(object, object.getClass().getName());
      bindingResult.addError(new ObjectError(bindingResult.getObjectName(), e.getMessage()));
    }

    if (domain != null && bindingResult != null) {
      BindingResult newResult = new ValidationErrors(object);
      for (Object error : bindingResult.getAllErrors()) {
        if (error instanceof FieldError) {
          FieldError fieldError = (FieldError) error;
          final boolean isBlank = BLANK.equals(fieldError.getRejectedValue());
          if (!isBlank) {
            newResult.addError(fieldError);
          } else if (domain.hasPersistentProperty(fieldError.getField())) {
            final boolean isOptional = domain.getPropertyByName(fieldError.getField()).isOptional();
            if (!isOptional) {
              newResult.addError(fieldError);
            }
          } else {
            newResult.addError(fieldError);
          }
        } else {
          newResult.addError((ObjectError) error);
        }
      }
      bindingResult = newResult;
    }
    MetaClass mc = GroovySystem.getMetaClassRegistry().getMetaClass(object.getClass());
    if (mc.hasProperty(object, "errors") != null && bindingResult != null) {
      ValidationErrors errors = new ValidationErrors(object);
      errors.addAllErrors(bindingResult);
      mc.setProperty(object, "errors", errors);
    }
    return bindingResult;
  }