How to format file strings to a dot path












-2















I want to make a configuration to store items, however, when I was making the paths to get the values, something wrong happened.



HashMap<String, Text> sections;

private void loadKeys() {
List<String> list = new ArrayList<>();
for (String s : sections.keySet()) {
Text te = sections.get(s);
String changeable = s.substring(0, s.length() - 1);
for (int i = 0; i < te.lines(); i++) {
String line = te.getLine(i);
while (line.startsWith(" ")) {
line = line.substring(2);
}
if (!line.startsWith("-")) {
if (line.endsWith(":")) {
changeable = changeable + "." + line.substring(0, line.length() - 1);
} else {
list.add(changeable + "." + line);
}
}
}
}
for (String s : list) {
System.out.println(s);
}
}


Text.java



public class Text {
private List<String> lines = new ArrayList<>();

public Text(String txt) {
if (txt.contains("n")) {
for (String s : txt.split("n")) {
lines.add(s);
}
} else {
lines.add(txt);
}
}

public int lines() {
return lines.size();
}

public String getLine(int line) {
return lines.get(line);
}

@Override
public String toString() {
String string = "";
for (String s : lines) {
if (string.equals("")) {
string = s;
} else {
string = string + "n" + s;
}
}
return string;
}
}


File:



Test11:
Test12:
Test13: 'test'
Test14: 'test2'
Test15: teste
Test16:
Test17: "test test"


The output I want:




  • Test11.Test12.Test13: 'test'

  • Test11.Test12.Test14: 'test2'

  • Test11.Test15: teste

  • Test11.Test16.Test17: "test test"


What I got with the code above:




  • Test11.Test12.Test13: 'test'

  • Test11.Test12.Test14: 'test2'

  • Test11.Test12.Test15: teste

  • Test11.Test12.Test16.Test17: "test test"


Test12 is being repeated. Can you help me have what I want? Thanks in advance










share|improve this question




















  • 2





    It looks like you may need to learn to use a debugger. Please help yourself to some complementary debugging techniques. If you still have issues afterwards, please edit your question to be more specific with what help you need.

    – Joe C
    Jan 1 at 22:04











  • What is Text? Could you give the full code?

    – oleg.cherednik
    Jan 1 at 22:05











  • What is sections?

    – Andreas
    Jan 1 at 22:32











  • Sorry, edited and added Text.java and sections

    – Christiano Rangel
    Jan 1 at 23:08


















-2















I want to make a configuration to store items, however, when I was making the paths to get the values, something wrong happened.



HashMap<String, Text> sections;

private void loadKeys() {
List<String> list = new ArrayList<>();
for (String s : sections.keySet()) {
Text te = sections.get(s);
String changeable = s.substring(0, s.length() - 1);
for (int i = 0; i < te.lines(); i++) {
String line = te.getLine(i);
while (line.startsWith(" ")) {
line = line.substring(2);
}
if (!line.startsWith("-")) {
if (line.endsWith(":")) {
changeable = changeable + "." + line.substring(0, line.length() - 1);
} else {
list.add(changeable + "." + line);
}
}
}
}
for (String s : list) {
System.out.println(s);
}
}


Text.java



public class Text {
private List<String> lines = new ArrayList<>();

public Text(String txt) {
if (txt.contains("n")) {
for (String s : txt.split("n")) {
lines.add(s);
}
} else {
lines.add(txt);
}
}

public int lines() {
return lines.size();
}

public String getLine(int line) {
return lines.get(line);
}

@Override
public String toString() {
String string = "";
for (String s : lines) {
if (string.equals("")) {
string = s;
} else {
string = string + "n" + s;
}
}
return string;
}
}


File:



Test11:
Test12:
Test13: 'test'
Test14: 'test2'
Test15: teste
Test16:
Test17: "test test"


The output I want:




  • Test11.Test12.Test13: 'test'

  • Test11.Test12.Test14: 'test2'

  • Test11.Test15: teste

  • Test11.Test16.Test17: "test test"


What I got with the code above:




  • Test11.Test12.Test13: 'test'

  • Test11.Test12.Test14: 'test2'

  • Test11.Test12.Test15: teste

  • Test11.Test12.Test16.Test17: "test test"


Test12 is being repeated. Can you help me have what I want? Thanks in advance










share|improve this question




















  • 2





    It looks like you may need to learn to use a debugger. Please help yourself to some complementary debugging techniques. If you still have issues afterwards, please edit your question to be more specific with what help you need.

    – Joe C
    Jan 1 at 22:04











  • What is Text? Could you give the full code?

    – oleg.cherednik
    Jan 1 at 22:05











  • What is sections?

    – Andreas
    Jan 1 at 22:32











  • Sorry, edited and added Text.java and sections

    – Christiano Rangel
    Jan 1 at 23:08
















-2












-2








-2








I want to make a configuration to store items, however, when I was making the paths to get the values, something wrong happened.



HashMap<String, Text> sections;

private void loadKeys() {
List<String> list = new ArrayList<>();
for (String s : sections.keySet()) {
Text te = sections.get(s);
String changeable = s.substring(0, s.length() - 1);
for (int i = 0; i < te.lines(); i++) {
String line = te.getLine(i);
while (line.startsWith(" ")) {
line = line.substring(2);
}
if (!line.startsWith("-")) {
if (line.endsWith(":")) {
changeable = changeable + "." + line.substring(0, line.length() - 1);
} else {
list.add(changeable + "." + line);
}
}
}
}
for (String s : list) {
System.out.println(s);
}
}


Text.java



public class Text {
private List<String> lines = new ArrayList<>();

public Text(String txt) {
if (txt.contains("n")) {
for (String s : txt.split("n")) {
lines.add(s);
}
} else {
lines.add(txt);
}
}

public int lines() {
return lines.size();
}

public String getLine(int line) {
return lines.get(line);
}

@Override
public String toString() {
String string = "";
for (String s : lines) {
if (string.equals("")) {
string = s;
} else {
string = string + "n" + s;
}
}
return string;
}
}


File:



Test11:
Test12:
Test13: 'test'
Test14: 'test2'
Test15: teste
Test16:
Test17: "test test"


The output I want:




  • Test11.Test12.Test13: 'test'

  • Test11.Test12.Test14: 'test2'

  • Test11.Test15: teste

  • Test11.Test16.Test17: "test test"


What I got with the code above:




  • Test11.Test12.Test13: 'test'

  • Test11.Test12.Test14: 'test2'

  • Test11.Test12.Test15: teste

  • Test11.Test12.Test16.Test17: "test test"


Test12 is being repeated. Can you help me have what I want? Thanks in advance










share|improve this question
















I want to make a configuration to store items, however, when I was making the paths to get the values, something wrong happened.



HashMap<String, Text> sections;

private void loadKeys() {
List<String> list = new ArrayList<>();
for (String s : sections.keySet()) {
Text te = sections.get(s);
String changeable = s.substring(0, s.length() - 1);
for (int i = 0; i < te.lines(); i++) {
String line = te.getLine(i);
while (line.startsWith(" ")) {
line = line.substring(2);
}
if (!line.startsWith("-")) {
if (line.endsWith(":")) {
changeable = changeable + "." + line.substring(0, line.length() - 1);
} else {
list.add(changeable + "." + line);
}
}
}
}
for (String s : list) {
System.out.println(s);
}
}


Text.java



public class Text {
private List<String> lines = new ArrayList<>();

public Text(String txt) {
if (txt.contains("n")) {
for (String s : txt.split("n")) {
lines.add(s);
}
} else {
lines.add(txt);
}
}

public int lines() {
return lines.size();
}

public String getLine(int line) {
return lines.get(line);
}

@Override
public String toString() {
String string = "";
for (String s : lines) {
if (string.equals("")) {
string = s;
} else {
string = string + "n" + s;
}
}
return string;
}
}


File:



Test11:
Test12:
Test13: 'test'
Test14: 'test2'
Test15: teste
Test16:
Test17: "test test"


The output I want:




  • Test11.Test12.Test13: 'test'

  • Test11.Test12.Test14: 'test2'

  • Test11.Test15: teste

  • Test11.Test16.Test17: "test test"


What I got with the code above:




  • Test11.Test12.Test13: 'test'

  • Test11.Test12.Test14: 'test2'

  • Test11.Test12.Test15: teste

  • Test11.Test12.Test16.Test17: "test test"


Test12 is being repeated. Can you help me have what I want? Thanks in advance







java






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 1 at 23:05







Christiano Rangel

















asked Jan 1 at 21:58









Christiano RangelChristiano Rangel

12




12








  • 2





    It looks like you may need to learn to use a debugger. Please help yourself to some complementary debugging techniques. If you still have issues afterwards, please edit your question to be more specific with what help you need.

    – Joe C
    Jan 1 at 22:04











  • What is Text? Could you give the full code?

    – oleg.cherednik
    Jan 1 at 22:05











  • What is sections?

    – Andreas
    Jan 1 at 22:32











  • Sorry, edited and added Text.java and sections

    – Christiano Rangel
    Jan 1 at 23:08
















  • 2





    It looks like you may need to learn to use a debugger. Please help yourself to some complementary debugging techniques. If you still have issues afterwards, please edit your question to be more specific with what help you need.

    – Joe C
    Jan 1 at 22:04











  • What is Text? Could you give the full code?

    – oleg.cherednik
    Jan 1 at 22:05











  • What is sections?

    – Andreas
    Jan 1 at 22:32











  • Sorry, edited and added Text.java and sections

    – Christiano Rangel
    Jan 1 at 23:08










2




2





It looks like you may need to learn to use a debugger. Please help yourself to some complementary debugging techniques. If you still have issues afterwards, please edit your question to be more specific with what help you need.

– Joe C
Jan 1 at 22:04





It looks like you may need to learn to use a debugger. Please help yourself to some complementary debugging techniques. If you still have issues afterwards, please edit your question to be more specific with what help you need.

– Joe C
Jan 1 at 22:04













What is Text? Could you give the full code?

– oleg.cherednik
Jan 1 at 22:05





What is Text? Could you give the full code?

– oleg.cherednik
Jan 1 at 22:05













What is sections?

– Andreas
Jan 1 at 22:32





What is sections?

– Andreas
Jan 1 at 22:32













Sorry, edited and added Text.java and sections

– Christiano Rangel
Jan 1 at 23:08







Sorry, edited and added Text.java and sections

– Christiano Rangel
Jan 1 at 23:08














1 Answer
1






active

oldest

votes


















0














It is pretty easy. All you need is just keep current level depth and level name. You can do it via recursion or using queue.



public static Map<String, String> readProperties(Path path) throws IOException {
final class Level {

private final String name;
private final int offs;

public Level(String name, int offs) {
this.name = name;
this.offs = offs;
}
}

Map<String, String> map = new LinkedHashMap<>();
// contains all root items for current one with it's offset, to detecl that current level is sub level or parent
Deque<Level> levels = new LinkedList<>();
Pattern pattern = Pattern.compile("(?<offs>\s*)(?<key>[^:]+)\s*:\s*(?<value>.*)\s*");

Files.lines(path)
.map(pattern::matcher)
.filter(Matcher::matches)
.forEach(matcher -> {
int offs = matcher.group("offs").length();

// remove parent levels until reach the parent of current level
while (!levels.isEmpty() && levels.peekLast().offs >= offs) {
levels.removeLast();
}

String key = matcher.group("key");
String value = matcher.group("value");

if (value.isEmpty())
levels.add(new Level(key, offs));
else
map.put(levels.stream().map(level -> level.name).collect(Collectors.joining(".")) + '.' + key, value);
});

return map;
}





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53999257%2fhow-to-format-file-strings-to-a-dot-path%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    It is pretty easy. All you need is just keep current level depth and level name. You can do it via recursion or using queue.



    public static Map<String, String> readProperties(Path path) throws IOException {
    final class Level {

    private final String name;
    private final int offs;

    public Level(String name, int offs) {
    this.name = name;
    this.offs = offs;
    }
    }

    Map<String, String> map = new LinkedHashMap<>();
    // contains all root items for current one with it's offset, to detecl that current level is sub level or parent
    Deque<Level> levels = new LinkedList<>();
    Pattern pattern = Pattern.compile("(?<offs>\s*)(?<key>[^:]+)\s*:\s*(?<value>.*)\s*");

    Files.lines(path)
    .map(pattern::matcher)
    .filter(Matcher::matches)
    .forEach(matcher -> {
    int offs = matcher.group("offs").length();

    // remove parent levels until reach the parent of current level
    while (!levels.isEmpty() && levels.peekLast().offs >= offs) {
    levels.removeLast();
    }

    String key = matcher.group("key");
    String value = matcher.group("value");

    if (value.isEmpty())
    levels.add(new Level(key, offs));
    else
    map.put(levels.stream().map(level -> level.name).collect(Collectors.joining(".")) + '.' + key, value);
    });

    return map;
    }





    share|improve this answer




























      0














      It is pretty easy. All you need is just keep current level depth and level name. You can do it via recursion or using queue.



      public static Map<String, String> readProperties(Path path) throws IOException {
      final class Level {

      private final String name;
      private final int offs;

      public Level(String name, int offs) {
      this.name = name;
      this.offs = offs;
      }
      }

      Map<String, String> map = new LinkedHashMap<>();
      // contains all root items for current one with it's offset, to detecl that current level is sub level or parent
      Deque<Level> levels = new LinkedList<>();
      Pattern pattern = Pattern.compile("(?<offs>\s*)(?<key>[^:]+)\s*:\s*(?<value>.*)\s*");

      Files.lines(path)
      .map(pattern::matcher)
      .filter(Matcher::matches)
      .forEach(matcher -> {
      int offs = matcher.group("offs").length();

      // remove parent levels until reach the parent of current level
      while (!levels.isEmpty() && levels.peekLast().offs >= offs) {
      levels.removeLast();
      }

      String key = matcher.group("key");
      String value = matcher.group("value");

      if (value.isEmpty())
      levels.add(new Level(key, offs));
      else
      map.put(levels.stream().map(level -> level.name).collect(Collectors.joining(".")) + '.' + key, value);
      });

      return map;
      }





      share|improve this answer


























        0












        0








        0







        It is pretty easy. All you need is just keep current level depth and level name. You can do it via recursion or using queue.



        public static Map<String, String> readProperties(Path path) throws IOException {
        final class Level {

        private final String name;
        private final int offs;

        public Level(String name, int offs) {
        this.name = name;
        this.offs = offs;
        }
        }

        Map<String, String> map = new LinkedHashMap<>();
        // contains all root items for current one with it's offset, to detecl that current level is sub level or parent
        Deque<Level> levels = new LinkedList<>();
        Pattern pattern = Pattern.compile("(?<offs>\s*)(?<key>[^:]+)\s*:\s*(?<value>.*)\s*");

        Files.lines(path)
        .map(pattern::matcher)
        .filter(Matcher::matches)
        .forEach(matcher -> {
        int offs = matcher.group("offs").length();

        // remove parent levels until reach the parent of current level
        while (!levels.isEmpty() && levels.peekLast().offs >= offs) {
        levels.removeLast();
        }

        String key = matcher.group("key");
        String value = matcher.group("value");

        if (value.isEmpty())
        levels.add(new Level(key, offs));
        else
        map.put(levels.stream().map(level -> level.name).collect(Collectors.joining(".")) + '.' + key, value);
        });

        return map;
        }





        share|improve this answer













        It is pretty easy. All you need is just keep current level depth and level name. You can do it via recursion or using queue.



        public static Map<String, String> readProperties(Path path) throws IOException {
        final class Level {

        private final String name;
        private final int offs;

        public Level(String name, int offs) {
        this.name = name;
        this.offs = offs;
        }
        }

        Map<String, String> map = new LinkedHashMap<>();
        // contains all root items for current one with it's offset, to detecl that current level is sub level or parent
        Deque<Level> levels = new LinkedList<>();
        Pattern pattern = Pattern.compile("(?<offs>\s*)(?<key>[^:]+)\s*:\s*(?<value>.*)\s*");

        Files.lines(path)
        .map(pattern::matcher)
        .filter(Matcher::matches)
        .forEach(matcher -> {
        int offs = matcher.group("offs").length();

        // remove parent levels until reach the parent of current level
        while (!levels.isEmpty() && levels.peekLast().offs >= offs) {
        levels.removeLast();
        }

        String key = matcher.group("key");
        String value = matcher.group("value");

        if (value.isEmpty())
        levels.add(new Level(key, offs));
        else
        map.put(levels.stream().map(level -> level.name).collect(Collectors.joining(".")) + '.' + key, value);
        });

        return map;
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 1 at 22:44









        oleg.cherednikoleg.cherednik

        7,16021119




        7,16021119
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53999257%2fhow-to-format-file-strings-to-a-dot-path%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            How to fix TextFormField cause rebuild widget in Flutter

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith