summaryrefslogtreecommitdiff
path: root/java/common/src/main/java/org/apache/qpid/messaging/util/JAddr.java
blob: 93df052af1b41fd3f7ac69e8fcd701d3c02d2f99 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */
package org.apache.qpid.messaging.util;

import java.io.InputStreamReader;

import java.util.List;

import org.apache.qpid.messaging.Address;
import org.apache.qpid.messaging.util.ParseError;
import org.apache.qpid.messaging.util.Token;

import static org.apache.qpid.messaging.util.PyPrint.pprint;


/**
 * JAddr
 *
 */

public class JAddr
{

    public static final void main(String[] args) throws Exception
    {
        StringBuilder addr = new StringBuilder();
        InputStreamReader reader = new InputStreamReader(System.in);

        char[] buf = new char[1024];
        while (true)
        {
            int n = reader.read(buf, 0, buf.length);
            if (n < 0)
            {
                break;
            }
            addr.append(buf, 0, n);
        }

        if ("parse".equals(args[0]))
        {
            try
            {
                Address address = Address.parse(addr.toString());
                System.out.println(pprint_address(address));
            }
            catch (ParseError e)
            {
                System.out.println(String.format("ERROR: %s", e.getMessage()));
            }
        }
        else
        {
            List<Token> tokens = AddressParser.lex(addr.toString());
            for (Token t : tokens)
            {
                String value = t.getValue();
                if (value != null)
                {
                    value = value.replace("\\", "\\\\").replace("\n", "\\n");
                    System.out.println(String.format("%s:%s:%s", t.getType(), t.getPosition(), value));
                }
                else
                {
                    System.out.println(String.format("%s:%s", t.getType(), t.getPosition()));
                }
            }
        }
    }

    private static String pprint_address(Address addr)
    {
        return String.format("NAME: %s\nSUBJECT: %s\nOPTIONS: %s",
                             pprint(addr.getName()),
                             pprint(addr.getSubject()),
                             pprint(addr.getOptions()));
    }

}