public Converter convert(String sourceValue) throws ConversionException {
   if (StringUtil.isEmpty(sourceValue)) return null;
   Object result = BeanUtil.newInstance(sourceValue);
   if (result instanceof Format)
     return new ParseFormatConverter(Object.class, (Format) result, false);
   else if (result instanceof Converter) return (Converter) result;
   else
     throw new ConfigurationError("Class is neither Converter nor Format: " + result.getClass());
 }
 @SuppressWarnings({"unchecked", "rawtypes"})
 static ComponentBuilder<?> wrapWithCondition(
     ComponentDescriptor descriptor, ComponentBuilder<?> builder) {
   TypeDescriptor typeDescriptor = descriptor.getTypeDescriptor();
   if (typeDescriptor == null) return builder;
   String conditionText = typeDescriptor.getCondition();
   if (!StringUtil.isEmpty(conditionText)) {
     Expression<Boolean> condition = new ScriptExpression<Boolean>(conditionText);
     return new ConditionalComponentBuilder(builder, condition);
   } else return builder;
 }
  @Override
  public ImportStatement doParse(
      Element element, Statement[] parentPath, BeneratorParseContext context) {
    // check syntax
    assertAtLeastOneAttributeIsSet(element, ATT_DEFAULTS, ATT_DOMAINS, ATT_PLATFORMS, ATT_CLASS);

    // prepare parsing
    ArrayBuilder<String> classImports = new ArrayBuilder<String>(String.class);
    ArrayBuilder<String> domainImports = new ArrayBuilder<String>(String.class);

    // defaults import
    boolean defaults = ("true".equals(element.getAttribute("defaults")));

    // check class import
    String attribute = element.getAttribute("class");
    if (!StringUtil.isEmpty(attribute)) classImports.add(attribute);

    // (multiple) domain import
    attribute = element.getAttribute("domains");
    if (!StringUtil.isEmpty(attribute))
      domainImports.addAll(StringUtil.trimAll(StringUtil.tokenize(attribute, ',')));

    // (multiple) platform import
    attribute = element.getAttribute("platforms");

    List<PlatformDescriptor> platformImports = null;
    if (!StringUtil.isEmpty(attribute))
      platformImports = importPlatforms(StringUtil.trimAll(attribute.split(",")), context);

    return new ImportStatement(
        defaults, classImports.toArray(), domainImports.toArray(), platformImports);
  }
 @Override
 public boolean eval() {
   String[] actualValues = wrapper.getValues(elementLocator);
   if (contains) {
     mismatches = DataUtil.containsStrings(values, actualValues);
   } else {
     if (checkOrder) {
       mismatches = DataUtil.expectEqualArrays(values, actualValues);
     } else {
       mismatches = DataUtil.expectEqualArraysIgnoreOrder(values, actualValues);
     }
   }
   return (StringUtil.isEmpty(mismatches));
 }
 /**
  * helper method to check for selectors of individual fields like "select x from y" or "{'select x
  * from y where id=' + z}". For such selectors it returns true, otherwise false
  */
 protected static boolean isIndividualSelector(String selector) {
   if (selector == null) return false;
   StreamTokenizer tokenizer = new StreamTokenizer(new StringReader(selector));
   tokenizer.ordinaryChar('\'');
   tokenizer.ordinaryChar('"');
   int token;
   try {
     while ((token = tokenizer.nextToken()) != StreamTokenizer.TT_EOF) {
       if (token == StreamTokenizer.TT_WORD)
         return StringUtil.startsWithIgnoreCase(tokenizer.sval.trim(), "select");
     }
   } catch (IOException e) {
     throw new RuntimeException("Unexpected error", e);
   }
   return false;
 }
 @SuppressWarnings({"unchecked", "rawtypes"})
 private static Generator<Object[]> createSourceGeneratorFromObject(
     ArrayTypeDescriptor descriptor,
     BeneratorContext context,
     Generator<Object[]> generator,
     Object sourceObject) {
   if (sourceObject instanceof StorageSystem) {
     StorageSystem storage = (StorageSystem) sourceObject;
     String selector = descriptor.getSelector();
     String subSelector = descriptor.getSubSelector();
     if (!StringUtil.isEmpty(subSelector))
       generator =
           WrapperFactory.applyHeadCycler(
               new DataSourceGenerator(storage.query(subSelector, false, context)));
     else generator = new DataSourceGenerator(storage.query(selector, false, context));
   } else if (sourceObject instanceof EntitySource) {
     DataSourceGenerator<Entity> entityGenerator =
         new DataSourceGenerator<Entity>((EntitySource) sourceObject);
     generator = WrapperFactory.applyConverter(entityGenerator, new Entity2ArrayConverter());
   } else if (sourceObject instanceof Generator) {
     generator = (Generator<Object[]>) sourceObject;
   } else throw new ConfigurationError("Source type not supported: " + sourceObject.getClass());
   return generator;
 }
  @SuppressWarnings({"unchecked", "rawtypes"})
  static ComponentBuilder<?> createReferenceBuilder(
      ReferenceDescriptor descriptor, BeneratorContext context) {
    SimpleTypeDescriptor typeDescriptor = (SimpleTypeDescriptor) descriptor.getTypeDescriptor();

    // check uniqueness
    boolean unique = DescriptorUtil.isUnique(descriptor, context);
    Uniqueness uniqueness = (unique ? Uniqueness.SIMPLE : Uniqueness.NONE);

    // do I only need to generate nulls?
    if (DescriptorUtil.isNullable(descriptor, context)
        && DescriptorUtil.shouldNullifyEachNullable(descriptor, context))
      return builderFromGenerator(createNullGenerator(descriptor, context), descriptor, context);

    // TODO use SimpleTypeGeneratorFactory?
    Generator<?> generator = null;
    generator = DescriptorUtil.getGeneratorByName(typeDescriptor, context);
    if (generator == null)
      generator = SimpleTypeGeneratorFactory.createScriptGenerator(typeDescriptor);
    if (generator == null)
      generator = SimpleTypeGeneratorFactory.createConstantGenerator(typeDescriptor, context);
    if (generator == null)
      generator =
          SimpleTypeGeneratorFactory.createValuesGenerator(typeDescriptor, uniqueness, context);

    // get distribution
    Distribution distribution =
        FactoryUtil.getDistribution(
            typeDescriptor.getDistribution(), descriptor.getUniqueness(), false, context);

    // check source
    if (generator == null) {
      // check target type
      String targetTypeName = descriptor.getTargetType();
      ComplexTypeDescriptor targetType =
          (ComplexTypeDescriptor) context.getDataModel().getTypeDescriptor(targetTypeName);
      if (targetType == null) throw new ConfigurationError("Type not defined: " + targetTypeName);

      // check targetComponent
      String targetComponent = descriptor.getTargetComponent();

      // check source
      String sourceName = typeDescriptor.getSource();
      if (sourceName == null) throw new ConfigurationError("'source' is not set for " + descriptor);
      Object sourceObject = context.get(sourceName);
      if (sourceObject instanceof StorageSystem) {
        StorageSystem sourceSystem = (StorageSystem) sourceObject;
        String selector = typeDescriptor.getSelector();
        String subSelector = typeDescriptor.getSubSelector();
        boolean subSelect = !StringUtil.isEmpty(subSelector);
        String selectorToUse = (subSelect ? subSelector : selector);
        if (isIndividualSelector(selectorToUse)) {
          generator = new DataSourceGenerator(sourceSystem.query(selectorToUse, true, context));
        } else {
          generator =
              new DataSourceGenerator(
                  sourceSystem.queryEntityIds(
                      targetTypeName,
                      selectorToUse,
                      context)); // TODO v0.7.2 query by targetComponent
          if (selectorToUse == null && distribution == null)
            if (context.isDefaultOneToOne()) distribution = new ExpandSequence();
            else distribution = SequenceManager.RANDOM_SEQUENCE;
        }
        if (subSelect) generator = WrapperFactory.applyHeadCycler(generator);
      } else throw new ConfigurationError("Not a supported source type: " + sourceName);
    }

    // apply distribution if necessary
    if (distribution != null)
      generator = new DistributingGenerator(generator, distribution, unique);

    // check multiplicity
    generator = ComponentBuilderFactory.createMultiplicityWrapper(descriptor, generator, context);
    if (LOGGER.isDebugEnabled()) LOGGER.debug("Created " + generator);

    // check 'cyclic' config
    generator = DescriptorUtil.wrapWithProxy(generator, typeDescriptor);
    return builderFromGenerator(generator, descriptor, context);
  }
Пример #8
0
 private static String postProcess(String base, boolean upperCase, String pattern, int length) {
   if (upperCase) base = base.toUpperCase();
   if (length > 0) base = StringUtil.padLeft(base, length, '0');
   if (pattern != null) base = MessageFormat.format(pattern, base);
   return base;
 }