Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #33525 > unrolled thread

how to loop over nested yaml file content?

Started byMohammed Suleman khan <salmankhan0921@gmail.com>
First post2016-01-23 06:06 -0800
Last post2016-01-23 16:28 +0000
Articles 3 — 3 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  how to loop over nested yaml file content? Mohammed Suleman khan <salmankhan0921@gmail.com> - 2016-01-23 06:06 -0800
    Re: how to loop over nested yaml file content? Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2016-01-23 07:52 -0800
      Re: how to loop over nested yaml file content? Jeff Higgins <jeff@invalid.invalid> - 2016-01-23 16:28 +0000

#33525 — how to loop over nested yaml file content?

FromMohammed Suleman khan <salmankhan0921@gmail.com>
Date2016-01-23 06:06 -0800
Subjecthow to loop over nested yaml file content?
Message-ID<83fb14d1-778c-44b8-bc6a-25742445c958@googlegroups.com>
I am having a .yml file that has nested level navigation items that I am going to use for building the navigation. here my .yml file content.

navigationItems:
-
  link: /features
  label: Features
-
  link: /contact
  label: Contact Us
-
  link: /about
  label: About Us
-
  link: /services
  label: Services
  navigationItems:
    -
      link: /service-1
      label: Service 1

I am having two mapper classes navigation and navigatiomItems to map the fields of this yml file, Navigation.groovy

class Navigation {

    List<NavigationItem> navigationItems
}

NavigationItem.groovy

class NavigationItem {

    String link
    String label
    List<NavigationItem> navigationItems
}

I am using Jackson objectmapper to map the fields of yml with my mapper class like this:

private Navigation mapYamlNavigation(final String mdFileContents) {
        final ObjectMapper mapper = new ObjectMapper(new YAMLFactory()); // jackson databind
        Navigation navigations = new Navigation();
        // matches true means that we have legit yaml in our .md file
        try {
            // read and assign the yaml to our properties
            navigations = mapper.readValue(mdFileContents, Navigation.class);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return navigations;
    }

here I am trying to loop over it to get the all label, links and also the nested ones

private String yamlToHtml() throws FileNotFoundException {
        String filePath = "src/main/resources/static/navigation/topnav.yml";
        String content = new Scanner(new FileReader(filePath)).useDelimiter("\\Z").next();
        Navigation navItems = mapYamlNavigation(content);
        for (NavigationItem nav : navItems.getNavigationItems()) {
            System.out.println(nav.getLabel());
            System.out.println(nav.getLink());
            System.out.println(navItems.getNavigationItems().size());
            if (navItems.getNavigationItems().size() > 0) {
                for (int i=0; i<navItems.getNavigationItems().size(); i++) {
                    System.out.println(navItems.getNavigationItems().get(i).getLabel());
                    System.out.println(navItems.getNavigationItems().get(i).getLink());
                }
            }
        }

        return null;
    }

I am able to get the top ones but I am not able to get the nested ones that is

navigationItems:
    -
      link: /service-1
      label: Service 1

can anyone tell me how can I loop to get the nested ones ? is my yaml file is in correct order ?

[toc] | [next] | [standalone]


#33527

FromDaniel Pitts <newsgroup.nospam@virtualinfinity.net>
Date2016-01-23 07:52 -0800
Message-ID<XkNoy.139454$K21.72352@fx02.iad>
In reply to#33525
On 1/23/16 6:27 AM, Stefan Ram wrote:
> Mohammed Suleman khan <salmankhan0921@gmail.com> writes:
>> I am having a .yml file that has nested level navigation
>
> public final class Main
> {
>
>    private static final java.lang.String source =
>    "navigationItems:\n" +
>    "-\n" +
>    "  link: /features\n" +
>    "  label: Features\n" +
>    "-\n" +
>    "  link: /contact\n" +
>    "  label: Contact Us\n" +
>    "-\n" +
>    "  link: /about\n" +
>    "  label: About Us\n" +
>    "-\n" +
>    "  link: /services\n" +
>    "  label: Services\n" +
>    "  navigationItems:\n" +
>    "    -\n" +
>    "      link: /service-1\n" +
>    "      label: Service 1\n";
>
>    public static void main(String args[]) throws Exception
>    { final java.util.regex.Pattern p =
>      java.util.regex.Pattern.compile
>      ( "^ {6}([^ ].*)$", java.util.regex.Pattern.MULTILINE );
>      final java.util.regex.Matcher m = p.matcher( source );
>      while( m.find() )java.lang.System.out.println( m.group( 1 )); }}
>
> link: /service-1
> label: Service 1
>
Stefan: why are you using regex to parse Yaml? Yaml has a spec, and its 
far more complex than your regex suggests.

Mohammed:  The problem is that you're trying to represent a tree 
structure as a list.  Really you need:

class NavigationNode {
     private List<NavigationNode> children;
     private String label;
     private String link;
}


Then you can use recursion to go through the list.

Hopefully this helps.

[toc] | [prev] | [next] | [standalone]


#33529

FromJeff Higgins <jeff@invalid.invalid>
Date2016-01-23 16:28 +0000
Message-ID<n809ms$ee6$1@dont-email.me>
In reply to#33527
On Sat, 23 Jan 2016 07:52:23 -0800, Daniel Pitts wrote:

> On 1/23/16 6:27 AM, Stefan Ram wrote:
>> Mohammed Suleman khan <salmankhan0921@gmail.com> writes:
>>> I am having a .yml file that has nested level navigation
>>
>> public final class Main {
>>
>>    private static final java.lang.String source = "navigationItems:\n"
>>    +
>>    "-\n" +
>>    "  link: /features\n" +
>>    "  label: Features\n" +
>>    "-\n" +
>>    "  link: /contact\n" +
>>    "  label: Contact Us\n" +
>>    "-\n" +
>>    "  link: /about\n" +
>>    "  label: About Us\n" +
>>    "-\n" +
>>    "  link: /services\n" +
>>    "  label: Services\n" +
>>    "  navigationItems:\n" +
>>    "    -\n" +
>>    "      link: /service-1\n" +
>>    "      label: Service 1\n";
>>
>>    public static void main(String args[]) throws Exception { final
>>    java.util.regex.Pattern p =
>>      java.util.regex.Pattern.compile ( "^ {6}([^ ].*)$",
>>      java.util.regex.Pattern.MULTILINE );
>>      final java.util.regex.Matcher m = p.matcher( source );
>>      while( m.find() )java.lang.System.out.println( m.group( 1 )); }}
>>
>> link: /service-1 label: Service 1
>>
> Stefan: why are you using regex to parse Yaml? Yaml has a spec, and its
> far more complex than your regex suggests.
> 
> Mohammed:  The problem is that you're trying to represent a tree
> structure as a list.  Really you need:
> 
> class NavigationNode {
>      private List<NavigationNode> children;
>      private String label;
>      private String link;
> }

He has:

class NavigationItem {

    String link String label List<NavigationItem> navigationItems
}

and also;

I am using Jackson objectmapper to map the fields of yml with my mapper
class like this:

and;

here I am trying to loop over it to get the all label, links and also the
nested ones

private String yamlToHtml() throws FileNotFoundException {
        String filePath =
        "src/main/resources/static/navigation/topnav.yml";

and;

Navigation.groovy (?) in a couple places.

all apparently to build some html;

I am having a .yml file that has nested level navigation items that I am
going to use for building the navigation. here my .yml file content.

so that I'm not certain whether this cluelessness, trolling, or some 
highly developed workflow.

> 
> 
> Then you can use recursion to go through the list.
> 
> Hopefully this helps.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web