Solved Add Missing Properties To File

Discussion in 'Plugin Development' started by OMGitzFROST, Jun 19, 2020.

Thread Status:
Not open for further replies.
  1. Offline

    OMGitzFROST

    So the outline of what I am trying to do goes as follows.
    • Compare 2 Properties Objects
    • Determine if a property key is missing
    • Add and store the missing property key to a property file.
    Any ideas, examples, or references on how to approach this will be appreciated
     
  2. Offline

    Strahan

    I'm not aware of a "Property" object. Is this an object you designed?
     
  3. Offline

    OMGitzFROST

    Thanks for your reply @Strahan, Im referring to this.

    Code:
    Properties exampleProp = new Properties();
     
  4. Offline

    Machine Maker

    This might be an instance of the XYProblem. (asking about your attempted solution rather than your problem)
     
    Strahan and bowlerguy66 like this.
  5. Offline

    OMGitzFROST

    This is what I've been working with.

    Code:
    //TODO: NOT WORKING AS INTENDED, NEEDS TO UPDATE MISSING VALUES TO PROPERTY FILE
    if(messageFile.exists()){
         FileInputStream inputStream = new FileInputStream(messageFile);
         InputStream packedInput = plugin.getResource(messageFile.getName());
         message.load(inputStream);
         packedMessage.load(packedInput);
    
         // COMPARE BOTH PROPERTY OBJECTS TO SEE IF THERE IS A DIFFERENCE (METHOD USES MapOne.equals(MapTwo))
         if(!Util.compare(message, packedMessage)){
              Set<String> unpacked = message.stringPropertyNames();
              Set<String> packed = packedMessage.stringPropertyNames();
    
              // ADDS MISSING PROPERTIES TO MAP
              for (String currentKey : unpacked) {
              if(message.getProperty(currentKey) == null) {
                   message.setProperty(currentKey, message.getProperty(currentKey));
              }
              message.setProperty(currentKey, message.getProperty(currentKey));
         }
    
         // REMOVE PROPERTIES THAT ARE NOT IN THE RAW
         for(String currentKey : packed){
              if(packedMessage.getProperty(currentKey) != null && message.containsKey(currentKey)){
                   message.removeKey(currentKey);
                    System.out.println("Key Removed."); // Temporary Logger To Check if Task is Executed...
              }
         }
         FileOutputStream outputStream = new FileOutputStream(messageFile);
         message.store(outputStream, null);
      }
    }
    
     
  6. Offline

    Strahan

    So you are using java.utils.Properties? I've never used that before, but from looking at it, it appears to function like a Map. Determining if a key is missing would be Properties#containsKey, adding a value would be Properties#put, etc. You can write it by creating a FileOutputStream and passing it to Properties#store.

    What are you trying to achieve?
     
  7. Offline

    OMGitzFROST

    @Strahan Thank you for your suggestions, as of now I have changed my code and its currently working as intended. Here is what I came up with incase anyone else needs this too

    Code:
    if(messageFile.exists()){
        InputStream customInput = new FileInputStream(messageFile);
        InputStream storedInput = plugin.getResource(messageFile.getName());
        customProp.load(customInput); defaultProp.load(storedInput);
        
        for (String defaultMessages : defaultProp.stringPropertyNames()){
            for (String customMessages : customProp.stringPropertyNames()){
                // CHECKS IF THE PROPERTIES IN JAR CONTAINS THE CUSTOM MESSAGE FILE PROPERTIES
                // IF IT EXISTS, IT ADDS IT TO A MAP
                if(defaultProp.containsKey(customMessages)){
                    customProp.setProperty(customMessages, customProp.getProperty(customMessages));
                    System.out.println("Added Existing Property"); // TEMP LOGGER
                }
                
                // CHECKS IS THE CUSTOM MESSAGE FILE CONTAINS THE PROPERTIES IN JAR
                // IF NOT, IT ADDS THE NEW PROPERTY TO A MAP
                if(!customProp.containsKey(defaultMessages)){
                    customProp.setProperty(defaultMessages, defaultProp.getProperty(defaultMessages));
                    System.out.println("Added New Property"); // TEMP LOGGER
                }
                
                // CHECKS IF THE CUSTOM MESSAGE FILE CONTAINS UNUSED PROPERTIES  
                // IF IT DOES, IT REMOVES IT AS ITS NO LONGER NEEDED.
                if(customProp.containsKey(customMessages) && !defaultProp.containsKey(customMessages)){
                    customProp.remove(customMessages);
                    System.out.println("Removed Unused Property"); // TEMP LOGGER
                }  
                FileOutputStream outputStream = new FileOutputStream(messageFile);
                customProp.store(outputStream, null);
            }
        }
    }
    
     
Thread Status:
Not open for further replies.

Share This Page