⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 elementuse.java

📁 normkit is a set of tools supporting ontology learning. from xml,xsd to rdfs.
💻 JAVA
字号:
package it.itc.ectrl.normkit.cnorm.XPathModel;

import oracle.xml.parser.schema.*;

import java.util.Vector;

import it.itc.ectrl.normkit.cnorm.impl.C_NormalisationImpl;
import it.itc.ectrl.normkit.common.*;

/**
 * Class representing a possible element occurence in a XML document compliant with currently processed XML Schema. This entity is used within the C-Normalisation process in order to define Normalisation Map based on XPath model.
 * @author Oliver Fodor (fodor@itc.it)
 */
public class ElementUse {

    C_NormalisationImpl m_c_norm;
    XSDElement m_element;
    ElementUse m_parentElementUse;
    String m_XPathAbsolute;
    String m_XPathRelative;
    XSDNode m_elmType;
    int m_hashCode;

    String m_nodeBridgeURI;

    XSDElement m_parentElement;

    public ElementUse(XSDElement elementDeclaration, C_NormalisationImpl c_norm, ElementUse parentElementUse) throws Exception {

        m_element = elementDeclaration;
        m_c_norm = c_norm;
        m_elmType = m_element.getType();
        m_hashCode = TransformationUtil.getHashCode(m_element);

//        if (m_element.getRefState() < 2 || parentElementUse == null)  // global element declaration
//            m_XPathRelative = m_element.getTargetNS() + ":" +  m_element.getName();    // taking into consideration that locals are not required to be qualified
//        else
            m_XPathRelative = m_element.getName();

        if (parentElementUse == null) {
            m_XPathAbsolute = "/" + m_XPathRelative;
        } else {
            m_XPathAbsolute = parentElementUse.getXPath() + "/" + m_XPathRelative;
            m_parentElement = parentElementUse.getElement();
            m_parentElementUse = parentElementUse;
        }

     }

    public void createNormMapEntries() throws Exception {

        //if  (!mapsToLiteral())
            m_nodeBridgeURI = createNormMapEntryForType();

        if (m_parentElement!=null)
            createNormMapEntriesForElement();

    }

    private String createNormMapEntryForType() throws Exception {

        int typeHashCode = TransformationUtil.getHashCode(m_elmType);
        String newNodeBridgeURI;

        Vector normalizedEntityURIset = m_c_norm.getNormalizedEntityURIs(typeHashCode);
        //String[] normalizedEntityURIs= (String[])normalizedEntityURIset.toArray();

        if  (mapsToLiteral())
          newNodeBridgeURI = m_c_norm.addNodeBridgeToNormMap(m_XPathAbsolute + "/text()", (String) normalizedEntityURIset.toArray()[0]);
        else
          newNodeBridgeURI = m_c_norm.addNodeBridgeToNormMap(m_XPathAbsolute, (String) normalizedEntityURIset.toArray()[0]);

        if (normalizedEntityURIset.size() > 1) {
            String subjectEntityURI = (String) normalizedEntityURIset.toArray()[0];
            String predicateEntityURI = (String) normalizedEntityURIset.toArray()[1];
            String objectEntityURI = (String) normalizedEntityURIset.toArray()[2];

            String litNodeBridgeURI = m_c_norm.addNodeBridgeToNormMap(m_XPathAbsolute + "/text()", (String) normalizedEntityURIset.toArray()[2]);

            String targetPathURI = m_c_norm.addPropertyNodeToNormMap(subjectEntityURI, predicateEntityURI, objectEntityURI );

            String newPathBridgeURI = m_c_norm.addPathBridgeToNormMap("text()", targetPathURI);

            m_c_norm.addHasBridgeToNodeBridge(newNodeBridgeURI, newPathBridgeURI);

            m_c_norm.addContextPropertyNodeToNodeBridge(litNodeBridgeURI, targetPathURI);
        }

        //if (m_c_norm.getNormalizedEntityURI(typeHashCode+99999999) != null) createNormMapEntriesForSimpleContent(newNodeBridgeURI);

        return newNodeBridgeURI;

    }

    private void createNormMapEntriesForElement() throws Exception {

        int typeHashCode = TransformationUtil.getHashCode(m_elmType);
        int parentTypeHashCode = TransformationUtil.getHashCode(m_parentElement.getType());
        int elmHashCode = TransformationUtil.getHashCode(m_element);
/*
        String byElmTypeNormalizedEntityURI;

        //if  (mapsToLiteral())
        //        byElmTypeNormalizedEntityURI = CNOVocabulary.URI_RDFS_LITERAL;
        //else
                byElmTypeNormalizedEntityURI = m_c_norm.getNormalizedEntityURI(typeHashCode);

        String byParentTypeNormalizedEntityURI = m_c_norm.getNormalizedEntityURI(parentTypeHashCode);
        String byElmNormalizedEntityURI = m_c_norm.getNormalizedEntityURI(elmHashCode);
*/
        String byElmTypeNormalizedEntityURI = m_c_norm.getNormalizedEntityFirstURI(typeHashCode);
        String byParentTypeNormalizedEntityURI = m_c_norm.getNormalizedEntityFirstURI(parentTypeHashCode);
        String byElmNormalizedEntityURI = m_c_norm.getNormalizedEntityFirstURI(elmHashCode);

        // add property node to map
        String targetPathURI = m_c_norm.addPropertyNodeToNormMap(byParentTypeNormalizedEntityURI, byElmNormalizedEntityURI, byElmTypeNormalizedEntityURI);
        // add pathbridge to map
        String newPathBridgeURI;
        if  (mapsToLiteral())
          newPathBridgeURI = m_c_norm.addPathBridgeToNormMap(m_XPathRelative + "/text()", targetPathURI);
        else
          newPathBridgeURI = m_c_norm.addPathBridgeToNormMap(m_XPathRelative, targetPathURI);
        // add hasbridge to the bridge defined by parents element
        m_c_norm.addHasBridgeToNodeBridge(m_parentElementUse.getNodeBridgeURI(), newPathBridgeURI);

        // add inContextOf to the nodebridge defined by this elements type
        if (m_nodeBridgeURI!=null) m_c_norm.addContextPropertyNodeToNodeBridge(m_nodeBridgeURI, targetPathURI);

    }
/*
    private void createNormMapEntriesForSimpleContent(String parentNodeBridgeURI) throws Exception { // this is an ugly workaround ... better solution: parse simple content as object and associate new heuristic

        int parentTypeHashCode = TransformationUtil.getHashCode(m_elmType);
        int scHashCode = parentTypeHashCode + 99999999;
//        int propHashCode = scHashCode;

        String newNodeBridgeURI;

        // String normalizedEntityURI = m_c_norm.getNormalizedEntityURI(scHashCode);

        String byScNormalizedEntityURI = CNOVocabulary.URI_RDFS_LITERAL;

        newNodeBridgeURI = m_c_norm.addNodeBridgeToNormMap(m_XPathAbsolute, byScNormalizedEntityURI);

        String byParentTypeNormalizedEntityURI = m_c_norm.getNormalizedEntityFirstURI(parentTypeHashCode);
        String byScNormalizedPropertyURI = m_c_norm.getNormalizedEntityFirstURI(scHashCode);

        String newPathBridgeURI;

        // add property node to map
        String targetPathURI = m_c_norm.addPropertyNodeToNormMap(byParentTypeNormalizedEntityURI, byScNormalizedPropertyURI, byScNormalizedEntityURI);
        // add pathbridge to map
        newPathBridgeURI = m_c_norm.addPathBridgeToNormMap("text()", targetPathURI);
        // add hasbridge to the bridge defined by parents element

        m_c_norm.addHasBridgeToNodeBridge(parentNodeBridgeURI, newPathBridgeURI);

        // add inContextOf to the nodebridge defined by this elements type
        if (m_nodeBridgeURI!=null) m_c_norm.addContextPropertyNodeToNodeBridge(newNodeBridgeURI, targetPathURI);

    }
*/
    public void traverseAndProcessEventualNestedElementUses() throws Exception { // and attribute

        if (m_elmType.getNodeType()==1) {   //complex type
          if (((XSDComplexType)m_elmType).getContent()!=13) { // don't recurse mixed content (maybe this should be removed)

            XSDNode[] nestedElms = ((XSDComplexType) m_elmType).getElementSet();
            if (nestedElms!=null)
                traverseAndProcessNestedElementUses(nestedElms);

          }
            XSDAttribute[] nestedAtts = ((XSDComplexType) m_elmType).getAttributeDeclarations();
            traverseAndProcessNestedAttributeUses(nestedAtts);

        }

    }

    private void traverseAndProcessNestedElementUses(XSDNode[] nestedElms) throws Exception {

        int length=nestedElms.length;

        for (int i = 0; i < length; i++) {


            if (!(nestedElms[i] instanceof XSDElement)) {
                if (nestedElms[i] instanceof XSDGroup) {

                    Vector nodes = ((XSDGroup)nestedElms[i]).getNodeVector();
                    XSDNode[] groupMembers = new XSDNode[nodes.size()];
                    groupMembers = (XSDNode[])nodes.toArray(groupMembers);
                    traverseAndProcessNestedElementUses(groupMembers);
                } else { // xs:any, only??
                    // do nothing???
                }

            } else {

                XSDElement elm = (XSDElement)nestedElms[i];


                //System.out.println("ELM "+elm.getName()+ " " +elm.getRefState());

                ElementUse elmUse = new ElementUse(elm, m_c_norm, this);

                elmUse.createNormMapEntries();

                elmUse.traverseAndProcessEventualNestedElementUses();
            }
        }


    }

    private void traverseAndProcessNestedAttributeUses(XSDAttribute[] nestedAtts) throws Exception {

        int length=nestedAtts.length;

        for (int i = 0; i < length; i++) {

                XSDAttribute att = (XSDAttribute)nestedAtts[i];

                //System.out.println("ATT USE: " + att.getName());

                AttributeUse attUse = new AttributeUse(att, m_c_norm, this);

                attUse.createNormMapEntries();

        }
    }

    private boolean mapsToLiteral() throws Exception {   // heavy optimalisation necessary

        String normalizedEntityURI = m_c_norm.getNormalizedEntityFirstURI(TransformationUtil.getHashCode(m_elmType));

        if (normalizedEntityURI.equals(CNOVocabulary.URI_RDFS_LITERAL)) return true;

        else return false;
    }

    public XSDElement getElement() throws Exception {

        return m_element;
    }

    public int getHashCode() throws Exception {

        return TransformationUtil.getHashCode(m_element);
    }

    public String getXPath() throws Exception {

        return m_XPathAbsolute;
    }


    public String getNodeBridgeURI() throws Exception {

        return m_nodeBridgeURI;
    }

    public XSDNode getElementsType() throws Exception {

        return m_elmType;
    }

    public int getParentsTypeHashCode() throws Exception {

        return TransformationUtil.getHashCode(m_parentElement.getType());
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -