All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.fulib.classmodel.FileFragmentMap Maven / Gradle / Ivy

There is a newer version: 1.6.2
Show newest version
package org.fulib.classmodel;

import org.fulib.Parser;
import org.fulib.StrUtil;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class FileFragmentMap  
{

   protected PropertyChangeSupport listeners = null;

   public boolean firePropertyChange(String propertyName, Object oldValue, Object newValue)
   {
      if (listeners != null)
      {
         listeners.firePropertyChange(propertyName, oldValue, newValue);
         return true;
      }
      return false;
   }

   public boolean addPropertyChangeListener(PropertyChangeListener listener)
   {
      if (listeners == null)
      {
         listeners = new PropertyChangeSupport(this);
      }
      listeners.addPropertyChangeListener(listener);
      return true;
   }

   public boolean addPropertyChangeListener(String propertyName, PropertyChangeListener listener)
   {
      if (listeners == null)
      {
         listeners = new PropertyChangeSupport(this);
      }
      listeners.addPropertyChangeListener(propertyName, listener);
      return true;
   }

   public boolean removePropertyChangeListener(PropertyChangeListener listener)
   {
      if (listeners != null)
      {
         listeners.removePropertyChangeListener(listener);
      }
      return true;
   }

   public boolean removePropertyChangeListener(String propertyName,PropertyChangeListener listener)
   {
      if (listeners != null)
      {
         listeners.removePropertyChangeListener(propertyName, listener);
      }
      return true;
   }

   private LinkedHashMap codeMap = new LinkedHashMap<>();
   private ArrayList fragmentList = new ArrayList<>();

   public ArrayList getFragmentList()
   {
      return fragmentList;
   }

   //==========================================================================

   public FileFragmentMap()
   {
      CodeFragment startFragment = new CodeFragment().setKey("start:").setText("");
      fragmentList.add(startFragment);
   }

   public FileFragmentMap(String fileName)
   {
      this.setFileName(fileName);
   }

   public CodeFragment getFragment(String key)
   {
      return codeMap.get(key);
   }

   @Override // no fulib
   public String toString()
   {
      StringBuilder result = new StringBuilder();

      result.append(" ").append(this.getFileName());

      result.append("\n");

      result.append(getFileText());

      return result.substring(1);
   }

   private String getFileText()
   {
      StringBuilder fileBody = new StringBuilder();

      for(CodeFragment fragment : this.fragmentList)
      {
         fileBody.append(fragment.getText());
      }

      return fileBody.toString();
   }

   public CodeFragment add(String key, String newText, int newLines)
   {
      return add(key, newText, newLines, false);
   }

   public CodeFragment add(String key, String newText, int newLines, boolean removeFragment)
   {

      CodeFragment result = codeMap.get(key);

      if (result != null)
      {
         if (result.getText().indexOf("// no") >= 0)
         {
            // do not overwrite
            return result;
         }

         if (removeFragment)
         {
            codeMap.remove(key);
            int pos = fragmentList.indexOf(result);
            fragmentList.remove(pos);
            CodeFragment gap = fragmentList.get(pos - 1);
            if (StrUtil.stringEquals(gap.getKey(), Parser.GAP))
            {
               fragmentList.remove(pos - 1);
            }
            return result;
         }

         // keep annotations and modifiers
         if (newText.indexOf("@") >= 0)
         {
            // newtext contains annotations, thus it overrides annotations in the code
            // do not modify newtext
         }
         else if (key.equals(Parser.CLASS))
         {
            // keep annotations and implements clause "\\s*public\\s+class\\s+(\\w+)(\\.+)\\{"
            Pattern pattern = Pattern.compile("class\\s+(\\w+)\\s*(extends\\s+[^\\s]+)?");
            Matcher match = pattern.matcher(newText);
            boolean b = match.find();
            String className = match.group(1);
            String extendsClause = match.group(2);
            extendsClause = extendsClause == null ? "" : extendsClause + " ";

            int resultClassNamePos = result.getText().indexOf("class " + className);
            if (resultClassNamePos >= 0)
            {
               String prefix = result.getText().substring(0, resultClassNamePos);
               String middle = "class " + className + " " + extendsClause;
               String suffix = " \n{";

               int implementsPos = result.getText().indexOf("implements");
               if (implementsPos >= 0)
               {
                  suffix = " " + result.getText().substring(implementsPos);
               }

               newText = prefix + middle + suffix;
            }
         }
         else if (key.startsWith(Parser.ATTRIBUTE))
         {
            // keep everything before public
            int newTextPublicPos = newText.indexOf("public");
            int resultPublicPos = result.getText().indexOf("public");
            if (newTextPublicPos >= 0 && resultPublicPos >= 0)
            {
               newText = result.getText().substring(0, resultPublicPos) + newText.substring(newTextPublicPos);
            }
         }
         else if (key.startsWith(Parser.ATTRIBUTE)) // ToDo: this looks wrong, remove it?
         {
            // keep everything before private
            int newTextPrivatePos = newText.indexOf("private");
            int resultPrivatePos = result.getText().indexOf("private");
            if (newTextPrivatePos >= 0 && resultPrivatePos >= 0)
            {
               newText = result.getText().substring(0, resultPrivatePos) + newText.substring(newTextPrivatePos);
            }
         }

         result.setText(newText.trim());

         return result;
      }

      if (removeFragment) return result;

      result = new CodeFragment().setKey(key).setText(newText);
      codeMap.put(key, result);
      CodeFragment gap = getNewLineGapFragment(newLines);


      if (key.startsWith(Parser.ATTRIBUTE) || key.startsWith(Parser.METHOD) || key.startsWith(Parser.CONSTRUCTOR))
      {
         add(result, Parser.CLASS_END);

         add(gap, Parser.CLASS_END);

         return result;
      }

      if (key.startsWith(Parser.IMPORT))
      {
         CodeFragment oldFragment = codeMap.get(Parser.CLASS);
         int pos = fragmentList.indexOf(oldFragment);

         // go to the gap before this
         pos--;

         pos = Math.max(0, pos);

         fragmentList.add(pos, gap);
         pos++;
         //         fragmentList.add(pos, gap);
         //         pos++;
         fragmentList.add(pos, result);

         return result;
      }

      add(result);
      add(gap, Parser.CLASS_END);

      return result;
   }

   private CodeFragment getNewLineGapFragment(int newLines)
   {
      CodeFragment gap = new CodeFragment().setKey("gap:");

      String text = "";
      for (int i = 0; i 




© 2015 - 2025 Weber Informatics LLC | Privacy Policy