Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 * Copyright 2012 Lev Khomich
 *
 * Licensed 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.
 */

import java.util.Map;

import org.xml.sax.Attributes;

public class Main {
    public static String serializeOpenTag(String nsUri, String qname, Map<String, String> nsMappings,
            Attributes attrs, boolean optimizeNs) {
        String result = "<" + qname;
        if (nsUri != null && nsUri.length() > 0) {
            int idx = Math.max(qname.indexOf(':'), 0);
            nsMappings.put(qname.substring(0, idx), nsUri);
        }
        for (int i = 0; i < attrs.getLength(); i++) {
            result += " " + attrs.getQName(i) + "=\"" + attrs.getValue(i) + "\"";
        }
        for (String key : nsMappings.keySet()) {
            if (optimizeNs) {
                boolean found = key.isEmpty() && qname.indexOf(':') == -1
                        || key.length() > 0 && qname.startsWith(key + ":");
                for (int i = 0; i < attrs.getLength(); i++) {
                    String aqn = attrs.getQName(i);
                    if (aqn.startsWith("xml")) {
                        continue;
                    }
                    if (key.isEmpty() && aqn.indexOf(':') == -1 || key.length() > 0 && aqn.startsWith(key + ":")) {
                        found = true;
                        break;
                    }
                }
                if (!found) {
                    continue;
                }
            }

            if (key.isEmpty()) {
                String value = nsMappings.get(key);
                result += " xmlns=\"" + value + "\"";
            } else {
                result += " xmlns:" + key + "=\"" + nsMappings.get(key) + "\"";
            }
        }
        result += ">";
        return result;
    }
}