Search in sources :

Example 31 with IStructuredDocument

use of org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument in project webtools.sourceediting by eclipse.

the class AnchorHyperlinkDetector method detectHyperlinks.

public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region, boolean canShowMultipleHyperlinks) {
    if (textViewer != null && region != null) {
        IDocument document = textViewer.getDocument();
        if (document != null) {
            Node currentNode = getCurrentNode(document, region.getOffset());
            if (currentNode != null && currentNode.getNodeType() == Node.ELEMENT_NODE) {
                Element element = (Element) currentNode;
                IStructuredDocumentRegion documentRegion = ((IStructuredDocument) document).getRegionAtCharacterOffset(region.getOffset());
                ITextRegion textRegion = documentRegion.getRegionAtCharacterOffset(region.getOffset());
                ITextRegion nameRegion = null;
                ITextRegion valueRegion = null;
                String name = null;
                String value = null;
                /*
					 * http://bugs.eclipse.org/475680 - possible after last offset in the file or
					 * by a faulty implementation
					 */
                if (textRegion != null) {
                    if (DOMRegionContext.XML_TAG_ATTRIBUTE_VALUE.equals(textRegion.getType())) {
                        ITextRegionList regions = documentRegion.getRegions();
                        /*
							 * Could use 2, but there needs to be the tag open
							 * and name regions
							 */
                        int index = regions.indexOf(textRegion);
                        if (index >= 4) {
                            nameRegion = regions.get(index - 2);
                            valueRegion = textRegion;
                            name = documentRegion.getText(nameRegion);
                            value = StringUtils.strip(documentRegion.getText(valueRegion));
                        }
                    } else if (DOMRegionContext.XML_TAG_ATTRIBUTE_NAME.equals(textRegion.getType())) {
                        ITextRegionList regions = documentRegion.getRegions();
                        int index = regions.indexOf(textRegion);
                        // minus 3 to leave equal and value regions
                        if (index <= regions.size() - 3) {
                            nameRegion = textRegion;
                            valueRegion = regions.get(index + 2);
                            name = documentRegion.getText(nameRegion);
                            value = StringUtils.strip(documentRegion.getText(valueRegion));
                        }
                    }
                }
                if (name != null && value != null) {
                    int idx = -1;
                    if (HTML40Namespace.ATTR_NAME_HREF.equalsIgnoreCase(name) && (idx = value.indexOf("#")) >= 0) {
                        // $NON-NLS-1$
                        String filename = value.substring(0, idx);
                        final String anchorName = idx < value.length() - 1 ? value.substring(idx + 1) : null;
                        if (anchorName != null) {
                            final IPath basePath = new Path(((IDOMNode) element).getModel().getBaseLocation());
                            if (basePath.segmentCount() > 1) {
                                if (filename.length() == 0) {
                                    filename = basePath.lastSegment();
                                }
                                final IPath resolved = ModuleCoreSupport.resolve(basePath, filename);
                                final IFile targetFile = ResourcesPlugin.getWorkspace().getRoot().getFile(resolved);
                                if (targetFile.isAccessible())
                                    return createHyperlinksToAnchorNamed(textViewer, createHyperlinkRegion(documentRegion, valueRegion), element, value, canShowMultipleHyperlinks);
                            }
                        }
                    }
                    if (HTML40Namespace.ATTR_NAME_NAME.equalsIgnoreCase(name) || HTML40Namespace.ATTR_NAME_ID.equalsIgnoreCase(name)) {
                        return createReferrerHyperlinks(textViewer, createHyperlinkRegion(documentRegion, valueRegion), element, value, canShowMultipleHyperlinks);
                    }
                }
            }
        }
    }
    return null;
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) IFile(org.eclipse.core.resources.IFile) IPath(org.eclipse.core.runtime.IPath) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) IDocument(org.eclipse.jface.text.IDocument)

Example 32 with IStructuredDocument

use of org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument in project webtools.sourceediting by eclipse.

the class HTMLDocumentLoader method newEncodedDocument.

/*
	 * @see AbstractLoader#createNewStructuredDocument()
	 */
protected IEncodedDocument newEncodedDocument() {
    IStructuredDocument structuredDocument = StructuredDocumentFactory.getNewStructuredDocumentInstance(getParser());
    ((BasicStructuredDocument) structuredDocument).setReParser(new XMLStructuredDocumentReParser());
    return structuredDocument;
}
Also used : BasicStructuredDocument(org.eclipse.wst.sse.core.internal.text.BasicStructuredDocument) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) XMLStructuredDocumentReParser(org.eclipse.wst.xml.core.internal.parser.XMLStructuredDocumentReParser)

Example 33 with IStructuredDocument

use of org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument in project webtools.sourceediting by eclipse.

the class HTMLFormatter method replaceSource.

/**
 */
protected void replaceSource(IDOMModel model, int offset, int length, String source) {
    if (model == null)
        return;
    IStructuredDocument structuredDocument = model.getStructuredDocument();
    if (structuredDocument == null)
        return;
    if (source == null)
        source = new String();
    if (structuredDocument.containsReadOnly(offset, length))
        return;
    // We use 'structuredDocument' as the requester object just so this
    // and the other
    // format-related 'repalceText' (in replaceSource) can use the same
    // requester.
    // Otherwise, if requester is not identical,
    // the undo group gets "broken" into multiple pieces based
    // on the requesters being different. Technically, any unique, common
    // requester object would work.
    structuredDocument.replaceText(structuredDocument, offset, length, source);
}
Also used : IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)

Example 34 with IStructuredDocument

use of org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument in project webtools.sourceediting by eclipse.

the class HTMLFormatter method setWidth.

/**
 */
protected void setWidth(HTMLFormatContraints contraints, Node node) {
    if (contraints == null)
        return;
    if (node == null)
        return;
    IStructuredDocument structuredDocument = ((IDOMNode) node).getStructuredDocument();
    if (structuredDocument == null)
        // error
        return;
    if (!splitLines())
        return;
    int lineWidth = getLineWidth();
    if (lineWidth < 0)
        return;
    int offset = ((IDOMNode) node).getStartOffset();
    int line = structuredDocument.getLineOfOffset(offset);
    int lineOffset = 0;
    try {
        lineOffset = structuredDocument.getLineOffset(line);
    } catch (BadLocationException ex) {
        // error
        return;
    }
    if (lineOffset > offset)
        // error
        return;
    int availableWidth = lineWidth - (offset - lineOffset);
    if (availableWidth < 0)
        availableWidth = 0;
    contraints.setAvailableLineWidth(availableWidth);
}
Also used : IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 35 with IStructuredDocument

use of org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument in project webtools.sourceediting by eclipse.

the class StyleAttrAdapter method setValue.

/**
 */
private void setValue() {
    Element element = getElement();
    if (element == null)
        return;
    ICSSModel model = getExistingModel();
    if (model == null)
        return;
    IStructuredDocument structuredDocument = model.getStructuredDocument();
    if (structuredDocument == null)
        return;
    String value = null;
    IStructuredDocumentRegionList flatNodes = structuredDocument.getRegionList();
    if (flatNodes != null) {
        int count = flatNodes.getLength();
        if (count > 0) {
            StringBuffer buffer = new StringBuffer();
            for (int i = 0; i < count; i++) {
                IStructuredDocumentRegion flatNode = flatNodes.item(i);
                if (flatNode == null)
                    continue;
                buffer.append(flatNode.getText());
            }
            value = buffer.toString();
        }
    }
    this.ignoreNotification = true;
    if (value == null || value.length() == 0) {
        element.removeAttribute(STYLE);
    } else {
        Attr attr = element.getAttributeNode(STYLE);
        if (attr != null) {
            ((IDOMNode) attr).setValueSource(value);
        } else {
            Document document = element.getOwnerDocument();
            attr = document.createAttribute(STYLE);
            ((IDOMNode) attr).setValueSource(value);
            element.setAttributeNode(attr);
        }
    }
    this.ignoreNotification = false;
    notifyStyleChanged(element);
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) ICSSModel(org.eclipse.wst.css.core.internal.provisional.document.ICSSModel) IStructuredDocumentRegionList(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegionList) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) Element(org.w3c.dom.Element) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) Document(org.w3c.dom.Document) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) Attr(org.w3c.dom.Attr)

Aggregations

IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)432 IDOMModel (org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel)96 IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)79 IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)68 CSSCleanupStrategy (org.eclipse.wst.css.core.internal.cleanup.CSSCleanupStrategy)44 Node (org.w3c.dom.Node)42 ICSSModel (org.eclipse.wst.css.core.internal.provisional.document.ICSSModel)41 Document (org.w3c.dom.Document)36 BadLocationException (org.eclipse.jface.text.BadLocationException)35 IndexedRegion (org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)34 IDOMNode (org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode)34 ITextRegion (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)33 Element (org.w3c.dom.Element)30 IDocument (org.eclipse.jface.text.IDocument)29 IDOMDocument (org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument)28 IModelManager (org.eclipse.wst.sse.core.internal.provisional.IModelManager)26 StructuredDocumentEvent (org.eclipse.wst.sse.core.internal.provisional.events.StructuredDocumentEvent)23 Test (org.junit.Test)22 INodeAdapter (org.eclipse.wst.sse.core.internal.provisional.INodeAdapter)20 RegionChangedEvent (org.eclipse.wst.sse.core.internal.provisional.events.RegionChangedEvent)20