/*******************************************************************************
* Copyright (c) 2007-2012 Red Hat, Inc.
* Distributed under license by Red Hat, Inc. All rights reserved.
* This program is made available under the terms of the
* Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
******************************************************************************/
package org.jboss.tools.cdi.seam.text.ext.hyperlink;
import org.eclipse.core.resources.IFile;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.Region;
import org.eclipse.wst.sse.core.internal.provisional.IndexedRegion;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMAttr;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement;
import org.jboss.tools.cdi.seam.config.core.util.Util;
import org.jboss.tools.common.text.ext.hyperlink.AbstractHyperlinkPartitioner;
import org.jboss.tools.common.text.ext.hyperlink.HyperlinkRegion;
import org.jboss.tools.common.text.ext.hyperlink.IHyperlinkPartitionRecognizer;
import org.jboss.tools.common.text.ext.hyperlink.IHyperlinkRegion;
import org.jboss.tools.common.text.ext.util.StructuredModelWrapper;
import org.jboss.tools.common.text.ext.util.Utils;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
/**
* @author Jeremy
*/
public class SeamConfigTagNameHyperlinkPartitioner extends AbstractHyperlinkPartitioner implements IHyperlinkPartitionRecognizer {
public static final String SEAM_CONFIG_TAG_NAME_PARTITION = "org.jboss.tools.common.text.ext.jsp.SEAM_CONFIG_TAG_NAME"; //$NON-NLS-1$
/**
* @see com.ibm.sse.editor.hyperlink.AbstractHyperlinkPartitioner#parse(org.eclipse.jface.text.IDocument, com.ibm.sse.editor.extensions.hyperlink.IHyperlinkRegion)
*/
protected IHyperlinkRegion parse(IDocument document, int offset, IHyperlinkRegion superRegion) {
StructuredModelWrapper smw = new StructuredModelWrapper();
smw.init(document);
try {
Document xmlDocument = smw.getDocument();
if (xmlDocument == null) return null;
IRegion r = getRegion(document, offset);
if (r == null) return null;
String axis = getAxis(document, offset);
String contentType = superRegion.getContentType();
String type = SEAM_CONFIG_TAG_NAME_PARTITION;
return new HyperlinkRegion(r.getOffset(), r.getLength(), axis, contentType, type);
} finally {
smw.dispose();
}
}
/**
* @see com.ibm.sse.editor.extensions.hyperlink.IHyperlinkPartitionRecognizer#recognize(org.eclipse.jface.text.IDocument, com.ibm.sse.editor.extensions.hyperlink.IHyperlinkRegion)
*/
public boolean recognize(IDocument document, int offset, IHyperlinkRegion region) {
StructuredModelWrapper smw = new StructuredModelWrapper();
smw.init(document);
try {
IFile documentFile = smw.getFile();
if(documentFile==null) return false;
return (documentFile.getProject() != null);
} finally {
smw.dispose();
}
}
protected IRegion getRegion(IDocument document, final int offset) {
StructuredModelWrapper smw = new StructuredModelWrapper();
smw.init(document);
try {
Document xmlDocument = smw.getDocument();
if (xmlDocument == null) return null;
Node n = Utils.findNodeForOffset(xmlDocument, offset);
if(n == null) return null;
IDOMAttr attr = null;
IDOMElement elem = null;
if(n instanceof IDOMElement) {
elem = (IDOMElement)n;
} else if(n instanceof IDOMAttr) {
attr = (IDOMAttr)n;
if(attr.getOwnerElement() instanceof IDOMElement) {
elem = (IDOMElement)attr.getOwnerElement();
}
}
if (elem == null) return null;
String tagName = elem.getTagName();
String uri = SeamConfigTagNameHyperlink.getURI(elem, document, offset);
String[] pks = Util.getPackages(uri);
if(pks.length == 0) return null;;
if(attr != null) {
int nameStart = ((IndexedRegion)attr).getStartOffset();
int nameEnd = nameStart + attr.getName().length();
return new Region(nameStart,nameEnd - nameStart);
}
int start = Utils.getValueStart(elem);
int nameStart = start + (elem.isEndTag() ? "</" : "<").length(); //$NON-NLS-1$ //$NON-NLS-2$
int nameEnd = nameStart + tagName.length();
if (nameStart > offset || nameEnd <= offset) return null;
return new Region(nameStart,nameEnd - nameStart);
} finally {
smw.dispose();
}
}
/*
protected String getAxis(IDocument document, IHyperlinkRegion superRegion) {
if (superRegion.getAxis() == null || superRegion.getAxis().length() == 0) {
return JSPRootHyperlinkPartitioner.computeAxis(document, superRegion.getOffset()) + "/"; //$NON-NLS-1$
}
return superRegion.getAxis();
}
*/
}