1/*
2 * Copyright (c) 2018, Daniel Gultsch All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification,
5 * are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation and/or
12 * other materials provided with the distribution.
13 *
14 * 3. Neither the name of the copyright holder nor the names of its contributors
15 * may be used to endorse or promote products derived from this software without
16 * specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
22 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30package eu.siacs.conversations.xmpp;
31
32import android.support.annotation.NonNull;
33
34import rocks.xmpp.addr.Jid;
35
36public class InvalidJid implements Jid {
37
38 private final String value;
39
40 private InvalidJid(String jid) {
41 this.value = jid;
42 }
43 public static Jid of(String jid, boolean fallback) {
44 final int pos = jid.indexOf('/');
45 if (fallback && pos >= 0 && jid.length() >= pos + 1) {
46 if (jid.substring(pos+1).trim().isEmpty()) {
47 return Jid.ofEscaped(jid.substring(0,pos));
48 }
49 }
50 return new InvalidJid(jid);
51 }
52
53 @Override
54 @NonNull
55 public String toString() {
56 return value;
57 }
58
59 @Override
60 public boolean isFullJid() {
61 throw new AssertionError("Not implemented");
62 }
63
64 @Override
65 public boolean isBareJid() {
66 throw new AssertionError("Not implemented");
67 }
68
69 @Override
70 public Jid asBareJid() {
71 throw new AssertionError("Not implemented");
72 }
73
74 @Override
75 public Jid withLocal(CharSequence charSequence) {
76 throw new AssertionError("Not implemented");
77 }
78
79 @Override
80 public Jid withResource(CharSequence charSequence) {
81 throw new AssertionError("Not implemented");
82 }
83
84 @Override
85 public Jid atSubdomain(CharSequence charSequence) {
86 throw new AssertionError("Not implemented");
87 }
88
89 @Override
90 public String getLocal() {
91 throw new AssertionError("Not implemented");
92 }
93
94 @Override
95 public String getEscapedLocal() {
96 throw new AssertionError("Not implemented");
97 }
98
99 @Override
100 public String getDomain() {
101 throw new AssertionError("Not implemented");
102 }
103
104 @Override
105 public String getResource() {
106 throw new AssertionError("Not implemented");
107 }
108
109 @Override
110 public String toEscapedString() {
111 throw new AssertionError("Not implemented");
112 }
113
114 @Override
115 public int length() {
116 return value.length();
117 }
118
119 @Override
120 public char charAt(int index) {
121 return value.charAt(index);
122 }
123
124 @Override
125 public CharSequence subSequence(int start, int end) {
126 return value.subSequence(start, end);
127 }
128
129 @Override
130 public int compareTo(@NonNull Jid o) {
131 throw new AssertionError("Not implemented");
132 }
133
134 public static Jid getNullForInvalid(Jid jid) {
135 if (jid != null && jid instanceof InvalidJid) {
136 return null;
137 } else {
138 return jid;
139 }
140 }
141
142 public static boolean isValid(Jid jid) {
143 if (jid != null && jid instanceof InvalidJid) {
144 return false;
145 } else {
146 return true;
147 }
148 }
149}