/flexodesktop/generators/flexogenerator/src/main/java/org/openflexo/generator/file/AbstractCGFile.java

https://github.com/bluepimento/openflexo · Java · 131 lines · 67 code · 16 blank · 48 comment · 9 complexity · e8819323a85002141edf48677782128b MD5 · raw file

  1. /*
  2. * (c) Copyright 2010-2011 AgileBirds
  3. *
  4. * This file is part of OpenFlexo.
  5. *
  6. * OpenFlexo is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * OpenFlexo is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with OpenFlexo. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. package org.openflexo.generator.file;
  21. import java.util.logging.Logger;
  22. import org.openflexo.foundation.FlexoException;
  23. import org.openflexo.foundation.cg.CGFile;
  24. import org.openflexo.foundation.cg.GeneratedOutput;
  25. import org.openflexo.foundation.cg.GenerationRepository;
  26. import org.openflexo.foundation.cg.generator.IFlexoResourceGenerator;
  27. import org.openflexo.foundation.rm.SaveResourceException;
  28. import org.openflexo.foundation.rm.cg.CGRepositoryFileResource;
  29. import org.openflexo.foundation.xml.GeneratedCodeBuilder;
  30. import org.openflexo.generator.exception.GenerationException;
  31. import org.openflexo.generator.rm.GenerationAvailableFileResource;
  32. public abstract class AbstractCGFile extends CGFile {
  33. @SuppressWarnings("unused")
  34. private static final Logger logger = Logger.getLogger(AbstractCGFile.class.getPackage().getName());
  35. public AbstractCGFile(GeneratedCodeBuilder builder) {
  36. this(builder.generatedCode);
  37. initializeDeserialization(builder);
  38. }
  39. public AbstractCGFile(GeneratedOutput generatedCode) {
  40. super(generatedCode);
  41. }
  42. public AbstractCGFile(GenerationRepository repository, CGRepositoryFileResource resource) {
  43. super(repository, resource);
  44. }
  45. public GenerationAvailableFileResource getGenerationAvailableFileResource() {
  46. if (getResource() instanceof GenerationAvailableFileResource) {
  47. return (GenerationAvailableFileResource) getResource();
  48. }
  49. return null;
  50. }
  51. public IFlexoResourceGenerator getGenerator() {
  52. if (getGenerationAvailableFileResource() != null) {
  53. return getGenerationAvailableFileResource().getGenerator();
  54. }
  55. return null;
  56. }
  57. @Override
  58. public boolean isCodeGenerationAvailable() {
  59. return (getGenerator() != null);
  60. }
  61. public GenerationException getGenerationException() {
  62. if (getGenerationAvailableFileResource() != null) {
  63. return (GenerationException) getGenerationAvailableFileResource().getGenerationException();
  64. }
  65. return null;
  66. }
  67. @Override
  68. public boolean hasGenerationErrors() {
  69. if (getMarkedAsDoNotGenerate()) {
  70. return false;
  71. }
  72. hasGenerationErrors = (getGenerationException() != null);
  73. return hasGenerationErrors;
  74. }
  75. @Override
  76. public void writeModifiedFile() throws SaveResourceException, FlexoException {
  77. // Before to write the file, ensure generation is up-to-date
  78. /* if (getGenerator() != null) {
  79. if (logger.isLoggable(Level.FINE))
  80. logger.fine("Running generator if required");
  81. getGenerator().generate(false);
  82. }*/
  83. // GPO: The above code has been commented. Unless we find a very good reason for calling the code above, we should not do this. Why?
  84. // Well because! No, because when we call generate(false), we may trigger the generator to run again. So far it ain't too bad,
  85. // except that when the generator is done, it sends a notification CGContentRegenerated and it causes the flag "mark as merged" to
  86. // go back to false (making it impossible to write it down!). In conclusion, if you decide to uncomment the block above, then you
  87. // need to do something about the org.openflexo.foundation.cg.CGFile.update(FlexoObservable, DataModification) method that sets
  88. // the flag markAsMerged back to false
  89. super.writeModifiedFile();
  90. }
  91. /* public void dismissWhenUnchanged()
  92. {
  93. // Before to write the file, ensure generation is up-to-date
  94. if (getGenerator() != null) {
  95. if (logger.isLoggable(Level.FINE))
  96. logger.fine("Running generator if required");
  97. try {
  98. getGenerator().generate(false);
  99. } catch (Exception e) {
  100. e.printStackTrace();
  101. throw new RuntimeException(e);
  102. }
  103. }
  104. super.dismissWhenUnchanged();
  105. }
  106. */
  107. @Override
  108. public boolean needsMemoryGeneration() {
  109. if (getMarkedAsDoNotGenerate()) {
  110. return false;
  111. }
  112. // return (getGenerator() != null && getGenerator().needsGeneration());
  113. return getResource().needsMemoryGeneration();
  114. }
  115. }