Пример #1
0
 private static void write(File configFile, String name, String path)
     throws JDOMException, IOException {
   Document document;
   try {
     document = JDOMUtil.loadDocument(configFile);
   } catch (JDOMException e) {
     document = new Document();
     Element rootElement = new Element(ROOT_TAG);
     document.setRootElement(rootElement);
   }
   Element element = document.getRootElement();
   assert element.getName().equals(ROOT_TAG);
   write(element, name, path);
   JDOMUtil.writeDocument(document, configFile);
 }
Пример #2
0
 private void load(File projFile) {
   if (projFile == null) {
     return;
   }
   Document document = null;
   try {
     document = JDOMUtil.loadDocument(projFile);
   } catch (JDOMException ex) {
   } catch (IOException ex) {
   }
   Element projectElement = null;
   if (document != null) {
     Element root = document.getRootElement();
     if ("project".equals(root.getName())) {
       for (Object ch : root.getChildren("component")) {
         if (ch instanceof Element
             && "MPSProject".equals(((Element) ch).getAttributeValue("name"))) {
           projectElement = (Element) ch;
           break;
         }
       }
     }
   }
   if (projectElement != null) {
     load(projFile, (Element) projectElement);
   }
 }
Пример #3
0
 public static StructureModificationLog load(IFile modelFile) {
   IFile refactoringsFile = getRefactoringsFile(modelFile);
   if (!refactoringsFile.exists()) return null;
   try {
     HistoryReaderHandler handler = new HistoryReaderHandler();
     JDOMUtil.createSAXParser().parse(JDOMUtil.loadSource(refactoringsFile), handler);
     return handler.getResult();
   } catch (SAXParseException e) {
     LOG.warning(refactoringsFile.getPath() + " line " + e.getLineNumber());
   } catch (IOException e) {
     LOG.error(e);
   } catch (SAXException e) {
     LOG.error(e);
   } catch (ParserConfigurationException e) {
     LOG.error(e);
   }
   return null;
 }
Пример #4
0
  public static void save(IFile modelFile, StructureModificationLog log) {
    IFile refactoringsFile = getRefactoringsFile(modelFile);
    refactoringsFile.createNewFile();

    Document document = new HistoryWriter().saveHistory(log);
    if (refactoringsFile.isReadOnly()) {
      LOG.error("Can't write to " + refactoringsFile.getPath());
      return;
    }

    try {
      JDOMUtil.writeDocument(document, refactoringsFile);
    } catch (IOException e) {
      LOG.error("Error in file " + refactoringsFile, e);
    }
  }
Пример #5
0
 @Override
 protected ModelDependencies load(InputStream is) throws IOException {
   try {
     SAXParser saxParser = JDOMUtil.createSAXParser();
     BLDependenciesHandler handler = new BLDependenciesHandler();
     saxParser.parse(
         new InputSource(new InputStreamReader(is, FileUtil.DEFAULT_CHARSET)), handler);
     ModelDependencies dependencies = handler.getResult();
     if (dependencies != null) {
       return dependencies;
     }
     throw new IOException("empty result");
   } catch (SAXParseException ex) {
     throw new IOException(ex);
   } catch (Exception ex) {
     throw new IOException(ex);
   }
 }
Пример #6
0
  public static Map<String, Library> readBuiltInLibraries() {
    Map<String, Library> result = new HashMap<String, Library>();

    URL resource = BuiltInLibrariesIO.class.getResource(CONFIG_FILE_WHOLE_NAME);
    if (resource == null) return result;

    InputStream in = null;
    try {
      in = resource.openStream();
      Document document = JDOMUtil.loadDocument(in);
      Element element = document.getRootElement();
      List children = element.getChildren(LIBRARY_TAG);
      for (Object childObj : children) {
        Element child = (Element) childObj;
        String name = child.getAttribute(LIBRARY_NAME_TAG).getValue();
        String path = child.getAttribute(LIBRARY_PATH_TAG).getValue();
        final String realPath =
            MacrosFactory.mpsHomeMacros().expandPath(path, new File(PathManager.getHomePath()));

        Library predefinedLibrary =
            new Library(name) {
              @NotNull
              @Override
              public String getPath() {
                return realPath;
              }
            };
        result.put(name, predefinedLibrary);
      }
    } catch (JDOMException e) {
      LOG.error(e);
    } catch (IOException e) {
      LOG.error(e);
    } finally {
      if (in != null) {
        try {
          in.close();
        } catch (IOException e) {
          LOG.error(e);
        }
      }
    }
    return result;
  }
Пример #7
0
 @Override
 public jetbrains.mps.smodel.SModel createModel() {
   if (!myToCopy.isLoaded()) {
     // we are going to access internal/implementation model which might be in a partially-loaded
     // state (only public API guarantees proper loading). With partial model, we could face odd
     // issues (e.g. incomplete set of implicit imports as implementation node's concepts are not
     // considered)
     myToCopy.load();
   }
   Document document = ModelPersistence.saveModel(((SModelBase) myToCopy).getSModel());
   Element rootElement = document.getRootElement();
   rootElement.setAttribute(ModelPersistence.REF, getReference().toString());
   String modelContent = JDOMUtil.asString(document);
   try {
     return ModelPersistence.readModel(modelContent, false);
   } catch (ModelReadException e) {
     return new StubModel(PersistenceFacade.getInstance().createModelReference(myLongName), e);
   }
 }
  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 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);
  }