1 18 19 package sync4j.server; 20 21 import java.io.Serializable ; 22 import java.util.logging.Logger ; 23 import java.util.logging.Level ; 24 25 import sync4j.framework.logging.Sync4jLogger; 26 27 import sync4j.framework.core.SyncML; 28 import sync4j.framework.core.Sync4jException; 29 30 import org.apache.commons.lang.StringUtils; 31 32 37 public class SyncMLCanonizer implements Serializable { 38 39 private static final Logger log = Sync4jLogger.getLogger("engine"); 41 42 public SyncMLCanonizer() { 44 } 45 46 48 55 public String canonizeOutput(String message) { 56 if (log.isLoggable(Level.FINEST)) { 57 log.finest("Starting output canonization"); 58 } 59 60 message = devInfHandler(message); 61 62 return metInfNamespaceHandler(message); 63 } 64 65 73 public String canonizeInput(String message) { 74 if (log.isLoggable(Level.FINEST)) { 75 log.finest("Starting input canonization"); 76 } 77 78 message = fixXMLVersion(message); 79 80 return message; 81 } 82 83 90 private String replaceEntity(String message) { 91 return StringUtils.replace(message, "&", "&"); 92 } 93 94 102 private String metInfNamespaceHandler(String msg) { 103 int s = 0; 104 int e = 0; 105 106 StringBuffer response = new StringBuffer (); 107 while (( e = msg.indexOf("<Meta", s)) >= 0) { 108 109 response = response.append(msg.substring(s, e)); 110 111 int a = msg.indexOf("</Meta>", e); 112 String meta = msg.substring(e, a); 113 114 meta = meta.replaceAll("<MetInf>" , "<MetInf xmlns='syncml:metinf'>"); 115 meta = meta.replaceAll("<Type>" , "<Type xmlns='syncml:metinf'>"); 116 meta = meta.replaceAll("<Format>" , "<Format xmlns='syncml:metinf'>"); 117 meta = meta.replaceAll("<Mark>" , "<Mark xmlns='syncml:metinf'>"); 118 meta = meta.replaceAll("<Size>" , "<Size xmlns='syncml:metinf'>"); 119 meta = meta.replaceAll("<Anchor>" , "<Anchor xmlns='syncml:metinf'>"); 120 meta = meta.replaceAll("<Version>", "<Version xmlns='syncml:metinf'>"); 121 meta = meta.replaceAll("<NextNonce>" , "<NextNonce xmlns='syncml:metinf'>"); 122 meta = meta.replaceAll("<MaxMsgSize>", "<MaxMsgSize xmlns='syncml:metinf'>"); 123 meta = meta.replaceAll("<MaxObjSize>", "<MaxObjSize xmlns='syncml:metinf'>"); 124 meta = meta.replaceAll("<EMI>" , "<EMI xmlns='syncml:metinf'>"); 125 meta = meta.replaceAll("<Mem>" , "<Mem xmlns='syncml:metinf'>"); 126 127 s = a + 7; 128 response.append(meta).append("</Meta>"); 129 } 130 return response.append(msg.substring(s, msg.length())).toString(); 131 } 132 133 private String devInfHandler(String msg) { 134 135 msg = StringUtils.replaceOnce(msg, "<UTC></UTC>", "<UTC/>"); 136 msg = StringUtils.replaceOnce(msg, "<SupportLargeObjs></SupportLargeObjs>", "<SupportLargeObjs/>"); 137 msg = StringUtils.replaceOnce(msg, "<SupportNumberOfChanges></SupportNumberOfChanges>", "<SupportNumberOfChanges/>"); 138 139 140 msg = StringUtils.replaceOnce(msg, "<MoreData></MoreData>", "<MoreData/>"); 141 142 return msg; 143 } 144 145 154 private String fixXMLVersion(String msg) { 155 156 if (!msg.startsWith("<?xml")) { 157 return msg; 158 } 159 160 int e = msg.indexOf("?>"); 161 if (e < 0) { 162 return msg; 163 } 164 165 String prolog = msg.substring(0, e+2); 166 167 log.finest("prolog: " + prolog); 168 169 prolog = prolog.replaceFirst("version(\\s)*=[\"'][^\"']+[\"']", "version=\"1.0\""); 170 171 return prolog + msg.substring(e+2); 172 173 } 174 } 175 176 177 | Popular Tags |