001: /*
002: * Utils.java
003: *
004: * Created on 31.08.2007, 15:40:45
005: *
006: * To change this template, choose Tools | Templates
007: * and open the template in the editor.
008: */
009:
010: package org.netbeans.modules.xml.xpath.ext;
011:
012: import javax.xml.namespace.NamespaceContext;
013: import javax.xml.namespace.QName;
014: import org.netbeans.modules.xml.schema.model.ElementReference;
015: import org.netbeans.modules.xml.schema.model.Form;
016: import org.netbeans.modules.xml.schema.model.GlobalAttribute;
017: import org.netbeans.modules.xml.schema.model.GlobalElement;
018: import org.netbeans.modules.xml.schema.model.LocalAttribute;
019: import org.netbeans.modules.xml.schema.model.LocalElement;
020: import org.netbeans.modules.xml.schema.model.SchemaComponent;
021:
022: /**
023: * Utility class.
024: *
025: * @author nk160297
026: */
027: public class XPathUtils {
028:
029: /**
030: * Converts the qName to a String.
031: * Only the prefix and the local part is used.
032: * The namespace URI is ignored.
033: *
034: * This method is intended to print an entity.
035: */
036: public static String qNameObjectToString(QName qName) {
037: String prefix = qName.getPrefix();
038: if (prefix == null || prefix.length() == 0) {
039: return qName.getLocalPart();
040: } else {
041: return prefix + ":" + qName.getLocalPart();
042: }
043:
044: }
045:
046: /**
047: * Converts the qName to a String.
048: * Only the prefix and the namespace URI is used.
049: * The local part is ignored.
050: *
051: * This method is intended to print a namespace which is held in a QName.
052: */
053: public static String gNameNamespaceToString(QName qName) {
054: String prefix = qName.getPrefix();
055: String nsUri = qName.getNamespaceURI();
056: //
057: if (prefix == null || prefix.length() == 0) {
058: return "{" + nsUri + "}";
059: } else {
060: return "{" + nsUri + "}" + prefix;
061: }
062: }
063:
064: /**
065: * Check if the namespace URI is specified for the name.
066: * If the name isn't specified, then try resolve it from the prefix.
067: * Returns the corrected name if possible. Otherwise returns old name.
068: * @param nsContext
069: * @param name
070: * @return
071: */
072: public static QName resolvePrefix(NamespaceContext nsContext,
073: QName name) {
074: String nsUri = name.getNamespaceURI();
075: if (nsUri == null || nsUri.length() == 0 && nsContext != null) {
076: //
077: String nsPrefix = name.getPrefix();
078: nsUri = nsContext.getNamespaceURI(nsPrefix);
079: //
080: if (nsUri != null) {
081: String localPart = name.getLocalPart();
082: QName newName = new QName(nsUri, localPart);
083: name = newName;
084: }
085: }
086: //
087: return name;
088: }
089:
090: public static boolean equalsIgnorNsUri(QName qName1, QName qName2) {
091: return (qName1.getLocalPart().equals(qName2.getLocalPart()))
092: && (qName1.getPrefix().equals(qName2.getPrefix()));
093: }
094:
095: public static boolean samePredicatesArr(
096: XPathPredicateExpression[] predArr1,
097: XPathPredicateExpression[] predArr2) {
098: //
099: // Compare predicates count
100: int counter1 = predArr1 == null ? 0 : predArr1.length;
101: int counter2 = predArr2 == null ? 0 : predArr2.length;
102: if (counter1 != counter2) {
103: return false;
104: }
105: // Compare predicates one by one
106: for (int index = 0; index < counter1; index++) {
107: XPathPredicateExpression predicate1 = predArr1[index];
108: XPathPredicateExpression predicate2 = predArr2[index];
109: String predText1 = predicate1.getExpressionString();
110: String predText2 = predicate2.getExpressionString();
111: if (!(predText1.equals(predText2))) {
112: return false;
113: }
114: }
115: return true;
116: }
117:
118: /**
119: * Determines if a namespace prefix is required for the specified schema component.
120: * @param sComp
121: * @return
122: */
123: public static boolean isPrefixRequired(SchemaComponent sComp) {
124: if (sComp instanceof LocalElement) {
125: Form form = ((LocalElement) sComp).getFormEffective();
126: if (form == Form.QUALIFIED) {
127: return true;
128: } else {
129: return false;
130: }
131: } else if (sComp instanceof GlobalElement) {
132: return true;
133: } else if (sComp instanceof LocalAttribute) {
134: Form form = ((LocalAttribute) sComp).getFormEffective();
135: if (form == Form.QUALIFIED) {
136: return true;
137: } else {
138: return false;
139: }
140: } else if (sComp instanceof GlobalElement
141: || sComp instanceof ElementReference
142: || sComp instanceof GlobalAttribute) {
143: // all global objects have to be with a prefix
144: return true;
145: }
146: //
147: assert true : "Unsupported schema component in the BPEL mapper tree!"; // NOI18N
148: return false;
149: }
150: }
|