-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathNameConverter.java
More file actions
111 lines (97 loc) · 2.96 KB
/
NameConverter.java
File metadata and controls
111 lines (97 loc) · 2.96 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
* Copyright (c) 2010-2025 Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved.
*/
package com.marklogic.client.example.extension;
/**
* NameConverter mangles and unmangles strings as local names
* for XML elements.
* Warning: This example provides only crude, partial detection
* of Unicode characters that require escaping in an NCName.
* A more robust implementation could use
* org.apache.xerces.util.XML11Char for character and value testing.
*/
public class NameConverter {
final static public int HYPHEN_CP = "-".codePointAt(0);
final static public int PERIOD_CP = ".".codePointAt(0);
final static public int UNDERSCORE_CP = "_".codePointAt(0);
static public String unmangleFromNCName(String name) {
if (name == null)
return null;
if ("_".equals(name))
return "";
StringBuilder buf = new StringBuilder();
int namelen = name.length();
for (int i=0; i < namelen; i++) {
char c = name.charAt(i);
if (c != '_') {
buf.append(c);
continue;
}
StringBuilder hex = null;
for (; i < namelen; i++) {
c = name.charAt(i);
if (c == '_')
break;
if (hex == null) {
hex = new StringBuilder();
}
hex.append(c);
}
if (c != '_') {
throw new IllegalArgumentException("not a mangled name: "+name);
}
if (hex == null) {
buf.append('_');
} else {
buf.append(
Character.toChars(
Integer.parseInt(hex.toString(), 16))
);
}
}
return buf.toString();
}
static public String mangleToNCName(String string) {
if (string == null)
return null;
if (string.length() == 0)
return "_";
StringBuilder buf = new StringBuilder();
for (int i=0; i < string.length(); i++) {
appendToNCName(i==0, string.codePointAt(i), buf);
}
return buf.toString();
}
static public void appendToNCName(boolean isFirstChar, int cp, StringBuilder buf) {
if (isValidForNCName(isFirstChar, cp)) {
buf.appendCodePoint(cp);
} else if (cp == UNDERSCORE_CP) {
buf.append("__");
} else {
buf.appendCodePoint(UNDERSCORE_CP);
buf.append(Integer.toHexString(cp));
buf.appendCodePoint(UNDERSCORE_CP);
}
}
static public boolean isValidForNCName(boolean isFirstChar, int cp) {
int type = Character.getType(cp);
switch (type) {
case Character.LETTER_NUMBER:
case Character.LOWERCASE_LETTER:
case Character.OTHER_LETTER:
case Character.TITLECASE_LETTER:
case Character.UPPERCASE_LETTER:
return true;
case Character.COMBINING_SPACING_MARK:
case Character.DECIMAL_DIGIT_NUMBER:
case Character.ENCLOSING_MARK:
case Character.MODIFIER_LETTER:
case Character.NON_SPACING_MARK:
return !isFirstChar;
}
if (cp == PERIOD_CP || cp == HYPHEN_CP) {
return !isFirstChar;
}
return false;
}
}