public static LanguageDescriptor loadLanguageDescriptor(
      final IFile file, final MacroHelper macroHelper) {
    LanguageDescriptor descriptor;

    try {
      Document document = JDOMUtil.loadDocument(file);
      final Element languageElement = document.getRootElement();
      final String contentRoot = file.getParent().getPath();

      descriptor =
          new _FunctionTypes._return_P0_E0<LanguageDescriptor>() {
            public LanguageDescriptor invoke() {
              final LanguageDescriptor result_v3r4p8_a0a0e0c0b = new LanguageDescriptor();
              final String result_v3r4p8_a0a0a0e0c0b =
                  languageElement.getAttributeValue("namespace");
              result_v3r4p8_a0a0e0c0b.setNamespace(result_v3r4p8_a0a0a0e0c0b);
              String uuidValue = languageElement.getAttributeValue("uuid");
              if (uuidValue != null) {
                final String result_v3r4p8_a0a2a0a0e0c0b = uuidValue;
                result_v3r4p8_a0a0e0c0b.setUUID(result_v3r4p8_a0a2a0a0e0c0b);
              }

              String genOutput = languageElement.getAttributeValue("generatorOutputPath");
              if (genOutput != null) {
                final String result_v3r4p8_a0a5a0a0e0c0b = macroHelper.expandPath(genOutput);
                result_v3r4p8_a0a0e0c0b.setGenPath(result_v3r4p8_a0a5a0a0e0c0b);
              }

              Element modelsTag = XmlUtil.first(languageElement, "models");
              if (modelsTag != null) {
                result_v3r4p8_a0a0e0c0b
                    .getModelRootDescriptors()
                    .addAll(
                        ModuleDescriptorPersistence.loadModelRoots(
                            XmlUtil.children(modelsTag, "modelRoot"), contentRoot, macroHelper));
              } else {
                // old - for backwards compatibility
                result_v3r4p8_a0a0e0c0b
                    .getModelRootDescriptors()
                    .addAll(
                        ModuleDescriptorPersistence.loadModelRoots(
                            XmlUtil.children(languageElement, "modelRoot"),
                            contentRoot,
                            macroHelper));
              }

              Element facets = XmlUtil.first(languageElement, "facets");
              if (facets != null) {
                result_v3r4p8_a0a0e0c0b
                    .getModuleFacetDescriptors()
                    .addAll(
                        ModuleDescriptorPersistence.loadFacets(
                            XmlUtil.children(facets, "facet"), macroHelper));
              }

              ModuleDescriptorPersistence.loadDependencies(
                  result_v3r4p8_a0a0e0c0b, languageElement);
              for (Element extendedLanguage :
                  Sequence.fromIterable(
                      XmlUtil.children(
                          XmlUtil.first(languageElement, "extendedLanguages"),
                          "extendedLanguage"))) {
                result_v3r4p8_a0a0e0c0b
                    .getExtendedLanguages()
                    .add(
                        PersistenceFacade.getInstance()
                            .createModuleReference(extendedLanguage.getText()));
              }

              Element autoImports = XmlUtil.first(languageElement, "accessoryModels");
              if (autoImports == null) {
                // deprecated name
                autoImports = XmlUtil.first(languageElement, "library");
              }
              for (Element modelElement :
                  Sequence.fromIterable(XmlUtil.children(autoImports, "model"))) {
                result_v3r4p8_a0a0e0c0b
                    .getAccessoryModels()
                    .add(
                        PersistenceFacade.getInstance()
                            .createModelReference(modelElement.getAttributeValue("modelUID")));
              }

              for (Element generatorElement :
                  Sequence.fromIterable(
                      XmlUtil.children(
                          XmlUtil.first(languageElement, "generators"), "generator"))) {
                result_v3r4p8_a0a0e0c0b
                    .getGenerators()
                    .add(
                        GeneratorDescriptorPersistence.loadGeneratorDescriptor(
                            generatorElement, file, contentRoot, macroHelper));
              }

              for (Element entryElement :
                  Sequence.fromIterable(
                          XmlUtil.children(XmlUtil.first(languageElement, "classPath"), "entry"))
                      .concat(
                          Sequence.fromIterable(
                              XmlUtil.children(
                                  XmlUtil.first(languageElement, "runtimeClassPath"), "entry")))) {
                // runtimeClassPath was left for compatibility
                result_v3r4p8_a0a0e0c0b
                    .getAdditionalJavaStubPaths()
                    .add(macroHelper.expandPath(entryElement.getAttributeValue("path")));
              }

              Element stubModelEntries = XmlUtil.first(languageElement, "stubModelEntries");
              if (stubModelEntries != null) {
                List<String> roots =
                    ModuleDescriptorPersistence.loadStubModelEntries(stubModelEntries, macroHelper);
                result_v3r4p8_a0a0e0c0b.getAdditionalJavaStubPaths().addAll(roots);
              }

              for (Element entryElement :
                  Sequence.fromIterable(
                      XmlUtil.children(XmlUtil.first(languageElement, "sourcePath"), "source"))) {
                result_v3r4p8_a0a0e0c0b
                    .getSourcePaths()
                    .add(macroHelper.expandPath(entryElement.getAttributeValue("path")));
              }
              return result_v3r4p8_a0a0e0c0b;
            }
          }.invoke();
    } catch (Exception e) {
      throw new ModuleReadException(e);
    }

    ModuleDescriptorPersistence.setTimestamp(descriptor, file);
    return descriptor;
  }
  public static GeneratorDescriptor loadGeneratorDescriptor(
      final Element generatorElement,
      IFile file,
      final String contentRoot,
      final MacroHelper macroHelper) {
    GeneratorDescriptor descriptor =
        new _FunctionTypes._return_P0_E0<GeneratorDescriptor>() {
          public GeneratorDescriptor invoke() {
            final GeneratorDescriptor result_wk2vdq_a0a0a0b = new GeneratorDescriptor();
            String genUID = generatorElement.getAttributeValue("generatorUID");
            final String result_wk2vdq_a1a0a0a0b = genUID;
            result_wk2vdq_a0a0a0b.setGeneratorUID(result_wk2vdq_a1a0a0a0b);
            final boolean result_wk2vdq_a2a0a0a0b =
                XmlUtil.booleanWithDefault(generatorElement, "generate-templates", false);
            result_wk2vdq_a0a0a0b.setGenerateTemplates(result_wk2vdq_a2a0a0a0b);
            final boolean result_wk2vdq_a3a0a0a0b =
                XmlUtil.booleanWithDefault(generatorElement, "reflective-queries", true);
            result_wk2vdq_a0a0a0b.setReflectiveQueries(result_wk2vdq_a3a0a0a0b);
            final boolean result_wk2vdq_a4a0a0a0b =
                XmlUtil.booleanWithDefault(generatorElement, "needs-opctx", true);
            result_wk2vdq_a0a0a0b.setNeedOperationContext(result_wk2vdq_a4a0a0a0b);

            String uuid = generatorElement.getAttributeValue("uuid");
            if (uuid != null) {
              final String result_wk2vdq_a0a7a0a0a0b = uuid;
              result_wk2vdq_a0a0a0b.setUUID(result_wk2vdq_a0a7a0a0a0b);
            }

            String generatorName = generatorElement.getAttributeValue("name");
            if (generatorName != null) {
              final String result_wk2vdq_a0a01a0a0a0b = generatorName;
              result_wk2vdq_a0a0a0b.setNamespace(result_wk2vdq_a0a01a0a0a0b);
            }

            Element models = XmlUtil.first(generatorElement, "models");
            if (models != null) {
              result_wk2vdq_a0a0a0b
                  .getModelRootDescriptors()
                  .addAll(
                      ModuleDescriptorPersistence.loadModelRoots(
                          XmlUtil.children(models, "modelRoot"), contentRoot, macroHelper));
            } else {
              // old - for backwards compatibility
              result_wk2vdq_a0a0a0b
                  .getModelRootDescriptors()
                  .addAll(
                      ModuleDescriptorPersistence.loadModelRoots(
                          XmlUtil.children(generatorElement, "modelRoot"),
                          contentRoot,
                          macroHelper));
            }

            Element facets = XmlUtil.first(generatorElement, "facets");
            if (facets != null) {
              result_wk2vdq_a0a0a0b
                  .getModuleFacetDescriptors()
                  .addAll(
                      ModuleDescriptorPersistence.loadFacets(
                          XmlUtil.children(facets, "facet"), macroHelper));
            }

            ModuleDescriptorPersistence.loadDependencies(result_wk2vdq_a0a0a0b, generatorElement);

            // "depends on" generators
            for (Element refGenerator :
                Sequence.fromIterable(
                    XmlUtil.children(
                        XmlUtil.first(generatorElement, "external-templates"), "generator"))) {
              result_wk2vdq_a0a0a0b
                  .getDepGenerators()
                  .add(
                      PersistenceFacade.getInstance()
                          .createModuleReference(refGenerator.getAttributeValue("generatorUID")));
            }

            for (Element ruleElement :
                Sequence.fromIterable(
                    XmlUtil.children(
                        XmlUtil.first(generatorElement, "mapping-priorities"),
                        "mapping-priority-rule"))) {
              final MappingPriorityRule result_wk2vdq_a0a32a0a0a0b = new MappingPriorityRule();
              // TODO: remove when the error disappear.
              try {
                final RuleType result_wk2vdq_a0a1a0a32a0a0a0b =
                    RuleType.parse(ruleElement.getAttributeValue("kind"));
                result_wk2vdq_a0a32a0a0a0b.setType(result_wk2vdq_a0a1a0a32a0a0a0b);
              } catch (IllegalArgumentException e) {
                if (LOG.isEnabledFor(Level.ERROR)) {
                  LOG.error(
                      e.getMessage()
                          + " Rule type for generator "
                          + genUID
                          + " is set to EQUALS. You can change this in Generator Properties dialog.",
                      e);
                }
                final RuleType result_wk2vdq_a1a0b0a0x0a0a0a1 = RuleType.STRICTLY_TOGETHER;
                result_wk2vdq_a0a32a0a0a0b.setType(result_wk2vdq_a1a0b0a0x0a0a0a1);
              }

              Element greaterPM = XmlUtil.first(ruleElement, "greater-priority-mapping");
              if (greaterPM != null) {
                final MappingConfig_AbstractRef result_wk2vdq_a0a4a0a32a0a0a0b =
                    loadGeneratorMappingConfigRef(greaterPM, genUID, false);
                result_wk2vdq_a0a32a0a0a0b.setLeft(result_wk2vdq_a0a4a0a32a0a0a0b);
              }
              Element lesserPM = XmlUtil.first(ruleElement, "lesser-priority-mapping");
              if (lesserPM != null) {
                final MappingConfig_AbstractRef result_wk2vdq_a0a6a0a32a0a0a0b =
                    loadGeneratorMappingConfigRef(lesserPM, genUID, false);
                result_wk2vdq_a0a32a0a0a0b.setRight(result_wk2vdq_a0a6a0a32a0a0a0b);
              }
              result_wk2vdq_a0a0a0b.getPriorityRules().add(result_wk2vdq_a0a32a0a0a0b);
            }
            return result_wk2vdq_a0a0a0b;
          }
        }.invoke();
    ModuleDescriptorPersistence.setTimestamp(descriptor, file);
    return descriptor;
  }
  public static void saveLanguageDescriptor(
      IFile file, LanguageDescriptor descriptor, MacroHelper macroHelper) {
    if (file.isReadOnly()) {
      if (LOG.isEnabledFor(Priority.ERROR)) {
        LOG.error("Cant't save " + file.getPath());
      }
      return;
    }

    Element languageElement = new Element("language");
    languageElement.setAttribute("namespace", descriptor.getNamespace());
    String uuid = descriptor.getUUID();
    if (uuid != null) {
      languageElement.setAttribute("uuid", uuid);
    }
    if (descriptor.getGenPath() != null) {
      languageElement.setAttribute(
          "generatorOutputPath", macroHelper.shrinkPath(descriptor.getGenPath()));
    }

    Element models = new Element("models");
    ModuleDescriptorPersistence.saveModelRoots(
        models, descriptor.getModelRootDescriptors(), macroHelper);
    languageElement.addContent(models);

    if (!(descriptor.getModuleFacetDescriptors().isEmpty())) {
      Element facets = new Element("facets");
      ModuleDescriptorPersistence.saveFacets(
          facets, descriptor.getModuleFacetDescriptors(), macroHelper);
      languageElement.addContent(facets);
    }

    Element accessoryModels = new Element("accessoryModels");
    for (SModelReference model : SetSequence.fromSet(descriptor.getAccessoryModels())) {
      XmlUtil.tagWithAttribute(accessoryModels, "model", "modelUID", model.toString());
    }
    languageElement.addContent(accessoryModels);

    Element generators = new Element("generators");
    for (GeneratorDescriptor generatorDescriptor :
        ListSequence.fromList(descriptor.getGenerators())) {
      GeneratorDescriptorPersistence.saveGeneratorDescriptor(
          generators, generatorDescriptor, macroHelper);
    }
    languageElement.addContent(generators);

    if (!(descriptor.getAdditionalJavaStubPaths().isEmpty())) {
      Element stubModelEntries = new Element("stubModelEntries");
      ModuleDescriptorPersistence.saveStubModelEntries(
          stubModelEntries, descriptor.getAdditionalJavaStubPaths(), macroHelper);
      languageElement.addContent(stubModelEntries);
    }

    Element sourcePath = new Element("sourcePath");
    for (String p : descriptor.getSourcePaths()) {
      XmlUtil.tagWithAttribute(sourcePath, "source", "path", macroHelper.shrinkPath(p));
    }
    languageElement.addContent(sourcePath);

    ModuleDescriptorPersistence.saveDependencies(languageElement, descriptor);

    Element extendedLanguages = new Element("extendedLanguages");
    for (SModuleReference ref : SetSequence.fromSet(descriptor.getExtendedLanguages())) {
      XmlUtil.tagWithText(extendedLanguages, "extendedLanguage", ref.toString());
    }
    languageElement.addContent(extendedLanguages);

    try {
      OutputStream os = file.openOutputStream();
      JDOMUtil.writeDocument(new Document(languageElement), os);
    } catch (Exception e) {
      if (LOG.isEnabledFor(Priority.ERROR)) {
        LOG.error("", e);
      }
    }
    ModuleDescriptorPersistence.setTimestamp(descriptor, file);
  }
  public static void saveGeneratorDescriptor(
      Element languageGeneratorsElement, GeneratorDescriptor descriptor, MacroHelper macroHelper) {
    Element generator = new Element("generator");
    if (descriptor.getNamespace() != null) {
      generator.setAttribute("name", descriptor.getNamespace());
    }
    if (descriptor.getGeneratorUID() != null) {
      generator.setAttribute("generatorUID", descriptor.getGeneratorUID());
    }
    if (descriptor.getUUID() != null) {
      generator.setAttribute("uuid", descriptor.getUUID());
    }
    if (descriptor.isGenerateTemplates()) {
      generator.setAttribute(
          "generate-templates", Boolean.toString(descriptor.isGenerateTemplates()));
    }
    if (!(descriptor.isReflectiveQueries())) {
      generator.setAttribute("reflective-queries", Boolean.toString(false));
    }
    if (!(descriptor.needsOperationContext())) {
      generator.setAttribute("needs-opctx", Boolean.toString(false));
    }

    Element models = new Element("models");
    ModuleDescriptorPersistence.saveModelRoots(
        models, descriptor.getModelRootDescriptors(), macroHelper);
    generator.addContent(models);

    if (!(descriptor.getModuleFacetDescriptors().isEmpty())) {
      Element facets = new Element("facets");
      ModuleDescriptorPersistence.saveFacets(
          facets, descriptor.getModuleFacetDescriptors(), macroHelper);
      generator.addContent(facets);
    }

    // "depends on" generators
    Element extTemplates = new Element("external-templates");
    for (SModuleReference generatorReference : SetSequence.fromSet(descriptor.getDepGenerators())) {
      XmlUtil.tagWithAttribute(
          extTemplates, "generator", "generatorUID", generatorReference.toString());
    }
    generator.addContent(extTemplates);

    ModuleDescriptorPersistence.saveDependencies(generator, descriptor);

    // mapping priority rules
    Element mapPrio = new Element("mapping-priorities");
    for (MappingPriorityRule rule : ListSequence.fromList(descriptor.getPriorityRules())) {
      Element ruleElement = new Element("mapping-priority-rule");
      ruleElement.setAttribute("kind", rule.getType().getName());

      Element gpm = new Element("greater-priority-mapping");
      saveGeneratorMappingConfigRef(rule.getLeft(), gpm);
      ruleElement.addContent(gpm);

      Element lpm = new Element("lesser-priority-mapping");
      saveGeneratorMappingConfigRef(rule.getRight(), lpm);
      ruleElement.addContent(lpm);

      mapPrio.addContent(ruleElement);
    }
    generator.addContent(mapPrio);

    languageGeneratorsElement.addContent(generator);
  }