1fa170100a1250e3fd45e8e077d9b25db7f41f91
[aaf/authz.git] / misc / rosetta / src / main / java / org / onap / aaf / misc / rosetta / marshal / DocMarshal.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6  * ===========================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END====================================================
19  *
20  */
21
22 package org.onap.aaf.misc.rosetta.marshal;
23
24 import java.util.Iterator;
25 import java.util.NoSuchElementException;
26
27 import org.onap.aaf.misc.rosetta.Ladder;
28 import org.onap.aaf.misc.rosetta.Marshal;
29 import org.onap.aaf.misc.rosetta.ParseException;
30 import org.onap.aaf.misc.rosetta.Parsed;
31
32 public class DocMarshal<T> extends Marshal<T> {
33     private Marshal<T> root;
34     
35     public DocMarshal(Marshal<T> root) {
36         this.root = root;
37     }
38     
39     @Override
40     public Parsed<State> parse(T t, Parsed<State> parsed) throws ParseException {
41         Ladder<Iterator<?>> ladder = parsed.state.ladder;
42         Iterator<?> iter = ladder.peek();
43         if (iter==null) {
44             ladder.push(PENDING_ITERATOR);
45             parsed.event = START_DOC;
46         } else if (DONE_ITERATOR.equals(iter)) {
47         } else {
48             ladder.ascend(); // look at field info
49                 Iterator<?> currFieldIter = ladder.peek();
50                 if (!DONE_ITERATOR.equals(currFieldIter)){
51                     parsed = root.parse(t, parsed);
52                 }
53             ladder.descend();
54             if (DONE_ITERATOR.equals(currFieldIter) || parsed.event==NONE) {
55                 parsed.event = END_DOC;
56                 ladder.push(DONE_ITERATOR);
57             }
58         }
59         return parsed; // if unchanged, then it will end process
60
61     }
62
63     public static final Iterator<Void> PENDING_ITERATOR = new Iterator<Void>() {
64         @Override
65         public boolean hasNext() {
66             return false;
67         }
68
69         @Override
70         public Void next() {
71             if (!hasNext()) {
72                 throw new NoSuchElementException();
73             }
74             return null;
75         }
76
77         @Override
78         public void remove() {
79         }
80     };
81
82     public static<T> DocMarshal<T> root(Marshal<T> m) {
83         return (DocMarshal<T>)new DocMarshal<T>(m);
84     }
85
86 }