Search in sources :

Example 36 with ITextRegion

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

the class CSSSourceParser method parseNodes.

private IStructuredDocumentRegion parseNodes() {
    // regions are initially reported as complete offsets within the
    // scanned input
    // they are adjusted here to be indexes from the currentNode's start
    // offset
    IStructuredDocumentRegion headNode = null;
    IStructuredDocumentRegion lastNode = null;
    ITextRegion region = null;
    IStructuredDocumentRegion currentNode = null;
    String type = null;
    String currentRegionType = null;
    while ((region = getNextRegion()) != null) {
        type = region.getType();
        if (mustBeStart(type, currentRegionType) && currentNode != null) {
            currentNode.setEnded(true);
        }
        if ((currentNode != null && currentNode.isEnded()) || currentNode == null) {
            if (currentNode != null && !currentNode.isEnded()) {
                currentNode.setEnded(true);
            }
            lastNode = currentNode;
            currentNode = createStructuredDocumentRegion(type);
            currentRegionType = type;
            if (lastNode != null) {
                lastNode.setNext(currentNode);
            }
            currentNode.setPrevious(lastNode);
            currentNode.setStart(region.getStart());
        }
        currentNode.addRegion(region);
        currentNode.setLength(region.getStart() + region.getLength() - currentNode.getStart());
        region.adjustStart(-currentNode.getStart());
        if (mustBeEnd(type)) {
            currentNode.setEnded(true);
        }
        if (headNode == null && currentNode != null) {
            headNode = currentNode;
        }
    }
    if (currentNode != null && !currentNode.isEnded()) {
        currentNode.setEnded(true);
    }
    primReset();
    return headNode;
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)

Example 37 with ITextRegion

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

the class CSSTextRegionFactory method createRegion.

public ITextRegion createRegion(String context, int start, int textLength, int length) {
    ITextRegion region = null;
    region = new ContextRegion(context, start, textLength, length);
    return region;
}
Also used : ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) ContextRegion(org.eclipse.wst.sse.core.internal.parser.ContextRegion)

Example 38 with ITextRegion

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

the class JsTranslator method translateInlineJSNode.

/* (non-Javadoc)
	 * @see org.eclipse.wst.jsdt.web.core.javascript.IJsTranslator#translateInlineJSNode(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)
	 */
public void translateInlineJSNode(IStructuredDocumentRegion container) {
    // System.out
    // .println("JSPTranslator.translateInlineJSNode Entered
    // w/ScriptOffset:"
    // + scriptOffset);
    // NodeHelper nh = new NodeHelper(container);
    // System.out.println("inline js node looking at:\n" + nh);
    /* start a function header.. will amend later */
    ITextRegionList t = container.getRegions();
    ITextRegion r;
    Iterator regionIterator = t.iterator();
    while (regionIterator.hasNext() && !isCanceled()) {
        r = (ITextRegion) regionIterator.next();
        if (r.getType() == DOMRegionContext.XML_TAG_ATTRIBUTE_NAME) {
            int start = r.getStart();
            int offset = r.getTextEnd();
            String tagAttrname = container.getText(r);
            /*
					 * Attribute values aren't case sensative, also make sure next
					 * region is attrib value
					 */
            if (NodeHelper.isInArray(JsDataTypes.HTMLATREVENTS, tagAttrname)) {
                if (regionIterator.hasNext()) {
                    regionIterator.next();
                }
                if (regionIterator.hasNext()) {
                    r = ((ITextRegion) regionIterator.next());
                }
                if (r.getType() == DOMRegionContext.XML_TAG_ATTRIBUTE_VALUE) {
                    int valStartOffset = container.getStartOffset(r);
                    // int valEndOffset = r.getTextEnd();
                    String rawText = container.getText(r);
                    if (rawText == null || rawText.length() == 0) {
                        continue;
                    }
                    /* Strip quotes */
                    switch(rawText.charAt(0)) {
                        case '\'':
                        case '"':
                            rawText = rawText.substring(1);
                            valStartOffset++;
                    }
                    if (rawText == null || rawText.length() == 0) {
                        continue;
                    }
                    switch(rawText.charAt(rawText.length() - 1)) {
                        case '\'':
                        case '"':
                            rawText = rawText.substring(0, rawText.length() - 1);
                    }
                    // Position inScript = new Position(scriptOffset,
                    // rawText.length());
                    /* Quoted text starts +1 and ends -1 char */
                    Position inHtml = new Position(valStartOffset, rawText.length());
                    /* need to pad the script text with spaces */
                    char[] spaces = Util.getPad(Math.max(0, valStartOffset - scriptOffset - EVENT_HANDLER_PRE_LENGTH));
                    for (int i = 0; i < spaces.length; i++) {
                        try {
                            char c = fStructuredDocument.getChar(scriptOffset + i);
                            if (c == '\n' || c == '\r' || c == '\t')
                                spaces[i] = c;
                        } catch (BadLocationException e) {
                            Logger.logException(e);
                        }
                    }
                    fScriptText.append(spaces);
                    fScriptText.append(EVENT_HANDLER_PRE);
                    appendAndTrack(rawText, valStartOffset);
                    // $NON-NLS-1$
                    if (ADD_SEMICOLON_AT_INLINE)
                        fScriptText.append(";");
                    if (r.getLength() > rawText.length()) {
                        fScriptText.append(EVENT_HANDLER_POST);
                        spaces = Util.getPad(Math.max(0, r.getLength() - rawText.length() - EVENT_HANDLER_POST_LENGTH));
                        fScriptText.append(spaces);
                    }
                    scriptOffset = container.getEndOffset(r);
                }
            }
        }
    }
}
Also used : ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) Position(org.eclipse.jface.text.Position) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) Iterator(java.util.Iterator) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 39 with ITextRegion

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

the class JsTranslator method translateJSNode.

/* (non-Javadoc)
	 * @see org.eclipse.wst.jsdt.web.core.javascript.IJsTranslator#translateJSNode(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)
	 */
public void translateJSNode(IStructuredDocumentRegion container) {
    if (container == null)
        return;
    ITextRegionCollection containerRegion = container;
    Iterator regions = containerRegion.getRegions().iterator();
    ITextRegion region = null;
    char[] spaces = Util.getPad(container.getStartOffset() - scriptOffset);
    for (int i = 0; i < spaces.length; i++) {
        try {
            char c = fStructuredDocument.getChar(scriptOffset + i);
            if (c == '\n' || c == '\r' || c == '\t')
                spaces[i] = c;
        } catch (BadLocationException e) {
            Logger.logException(e);
        }
    }
    fScriptText.append(spaces);
    scriptOffset = container.getStartOffset();
    if (container.getType() != DOMRegionContext.BLOCK_TEXT && container.getType() != DOMRegionContext.XML_CDATA_TEXT) {
        // add place holder in position map (for formatting)
        appendAndTrack("", scriptOffset);
        return;
    }
    while (regions.hasNext() && !isCanceled()) {
        region = (ITextRegion) regions.next();
        String type = region.getType();
        // content assist was not showing up in JSP inside a javascript
        // region
        // System.out.println("Region text: " + container.getText().substring(region.getStart(), region.getEnd()));
        boolean isContainerRegion = region instanceof ITextRegionContainer;
        /* make sure its not a sub container region, probably JSP */
        if (type == DOMRegionContext.BLOCK_TEXT) {
            int scriptStartOffset = container.getStartOffset(region);
            int scriptTextLength = container.getLength();
            String regionText = container.getFullText(region);
            // regionText = StringUtils.replace(regionText, CDATA_START, CDATA_START_PAD);
            // regionText = StringUtils.replace(regionText, CDATA_END, CDATA_END_PAD);
            int regionLength = region.getLength();
            spaces = Util.getPad(scriptStartOffset - scriptOffset);
            for (int i = 0; i < spaces.length; i++) {
                try {
                    char c = fStructuredDocument.getChar(scriptOffset + i);
                    if (c == '\n' || c == '\r' || c == '\t')
                        spaces[i] = c;
                } catch (BadLocationException e) {
                    Logger.logException(e);
                }
            }
            fScriptText.append(spaces);
            // skip over XML/HTML comment starts
            if (regionText.indexOf(XML_COMMENT_START) >= 0) {
                int index = regionText.indexOf(XML_COMMENT_START);
                boolean replaceCommentStart = true;
                for (int i = 0; i < index; i++) {
                    /*
						 * replace the comment start in the translation when
						 * it's preceded only by white space or '/'
						 */
                    replaceCommentStart = replaceCommentStart && (Character.isWhitespace(regionText.charAt(i)) || '/' == regionText.charAt(i));
                }
                if (replaceCommentStart) {
                    IRegion line;
                    int end;
                    int length;
                    try {
                        /*
							 * try to find where the line with the comment
							 * ends (it is the end of what we'll replace)
							 */
                        line = container.getParentDocument().getLineInformationOfOffset(index + scriptStartOffset);
                        end = line.getOffset() + line.getLength() - scriptStartOffset;
                        if (end > regionText.length()) {
                            end = regionText.length();
                        }
                        length = end - index;
                    } catch (BadLocationException e) {
                        // $NON-NLS-1$
                        Logger.logException("Could not get web page's comment line information", e);
                        end = index + XML_COMMENT_START.length();
                        length = XML_COMMENT_START.length();
                    }
                    scriptStartOffset += end;
                    regionText = regionText.substring(end);
                    appendAndTrack(new String(Util.getPad(end)), fScriptText.length());
                }
            }
            /*
				 * Fix for
				 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=284774
				 * end of last valid JS source, start of next content to
				 * skip
				 */
            // last offset of valid JS source, after which there's server-side stuff
            int validJSend = 0;
            // start of next valid JS source, last offset of content that was skipped
            int validJSstart = 0;
            Matcher matcher = fClientSideTagPattern.matcher(regionText);
            // note the start of a HTML tag if one's present
            int clientMatchStart = matcher.find() ? matcher.start() : -1;
            StringBuffer generatedContent = new StringBuffer();
            int serverSideStart = -1;
            int serverSideDelimiter = 0;
            // find any instance of server code blocks in the region text
            for (int i = 0; i < fServerSideDelimiters.length; i++) {
                int index = regionText.indexOf(fServerSideDelimiters[i][0]);
                if (serverSideStart < 0) {
                    serverSideStart = index;
                    serverSideDelimiter = i;
                } else if (index >= 0) {
                    serverSideStart = Math.min(serverSideStart, index);
                    if (serverSideStart == index) {
                        serverSideDelimiter = i;
                    }
                }
            }
            // contains something other than pure JavaScript
            while (serverSideStart > -1 || clientMatchStart > -1) {
                // $NON-NLS-1$
                validJSend = validJSstart;
                boolean biasClient = false;
                boolean biasServer = false;
                // update the start of content to skip
                if (clientMatchStart > -1 && serverSideStart > -1) {
                    validJSend = Math.min(clientMatchStart, serverSideStart);
                    biasClient = validJSend == clientMatchStart;
                    biasServer = validJSend == serverSideStart;
                } else if (clientMatchStart > -1 && serverSideStart < 0) {
                    validJSend = clientMatchStart;
                    biasClient = true;
                } else if (clientMatchStart < 0 && serverSideStart > -1) {
                    validJSend = serverSideStart;
                    biasServer = true;
                }
                // append if there's something we want to include
                if (-1 < validJSstart && -1 < validJSend) {
                    // append what we want to include
                    appendAndTrack(regionText.substring(validJSstart, validJSend), scriptStartOffset + validJSstart);
                    // change the skipped content to a valid variable name and append it as a placeholder
                    int startOffset = scriptStartOffset + validJSend;
                    String serverEnd = fServerSideDelimiters[serverSideDelimiter][1];
                    int serverSideEnd = (regionLength > validJSend + serverEnd.length()) ? regionText.indexOf(serverEnd, validJSend + fServerSideDelimiters[serverSideDelimiter][1].length()) : -1;
                    if (serverSideEnd > -1)
                        serverSideEnd += serverEnd.length();
                    int clientMatchEnd = matcher.find(validJSend) ? matcher.end() : -1;
                    // update end of what we skipped
                    validJSstart = -1;
                    if (clientMatchEnd > validJSend && serverSideEnd > validJSend) {
                        if (biasClient)
                            validJSstart = clientMatchEnd;
                        else if (biasServer)
                            validJSstart = serverSideEnd;
                        else
                            validJSstart = Math.min(clientMatchEnd, serverSideEnd);
                    }
                    if (clientMatchEnd >= validJSend && serverSideEnd < 0)
                        validJSstart = matcher.end();
                    if (clientMatchEnd < 0 && serverSideEnd >= validJSend)
                        validJSstart = serverSideEnd;
                    /*
						 * the substitution text length much match original
						 * length exactly, generate text of the right length
						 */
                    int start = scriptStartOffset + validJSend;
                    int end = scriptStartOffset + validJSstart;
                    generatedContent.append('_');
                    for (int i = validJSend + 1; i < validJSstart; i++) {
                        switch(i - validJSend) {
                            case 1:
                                generatedContent.append('$');
                                break;
                            case 2:
                                generatedContent.append('t');
                                break;
                            case 3:
                                generatedContent.append('a');
                                break;
                            case 4:
                                generatedContent.append('g');
                                break;
                            default:
                                generatedContent.append('_');
                        }
                    }
                    /*
						 * Remember this source range, it may be needed to
						 * find the original contents for which we're
						 * placeholding
						 */
                    fGeneratedRanges.add(new Region(start, end - start));
                    appendAndTrack(generatedContent.toString(), start);
                    // reset now that it's been appended
                    generatedContent.setLength(0);
                }
                // set up to end while if no end for valid
                if (validJSstart > 0) {
                    int serverSideStartGuess = -1;
                    for (int i = 0; i < fServerSideDelimiters.length; i++) {
                        int index = regionText.indexOf(fServerSideDelimiters[i][0], validJSstart);
                        if (serverSideStartGuess < 0) {
                            serverSideStartGuess = index;
                            serverSideDelimiter = i;
                        } else if (index >= 0) {
                            serverSideStartGuess = Math.min(serverSideStartGuess, index);
                            if (serverSideStartGuess == index) {
                                serverSideDelimiter = i;
                            }
                        }
                    }
                    serverSideStart = validJSstart < regionLength - fShortestServerSideDelimiterPairLength ? serverSideStartGuess : -1;
                    clientMatchStart = validJSstart < regionLength ? (matcher.find(validJSstart) ? matcher.start() : -1) : -1;
                } else {
                    serverSideStart = clientMatchStart = -1;
                }
            }
            if (validJSstart >= 0) {
                appendAndTrack(regionText.substring(validJSstart), scriptStartOffset + validJSstart);
                Position inHtml = new Position(scriptStartOffset + validJSstart, regionText.length() - validJSstart);
                scriptLocationInHtml.add(inHtml);
            } else {
                appendAndTrack(regionText, scriptStartOffset);
                Position inHtml = new Position(scriptStartOffset, regionText.length());
                scriptLocationInHtml.add(inHtml);
            }
            scriptOffset = fScriptText.length();
        }
    }
    IStructuredDocumentRegion endTag = container.getNext();
    if (endTag == null) {
        missingEndTagRegionStart = container.getStartOffset();
    } else if (endTag != null) {
        NodeHelper nh = new NodeHelper(endTag);
        String name = nh.getTagName();
        if (name == null || !name.trim().equalsIgnoreCase("script") || !nh.isEndTag()) {
            // $NON-NLS-1$
            missingEndTagRegionStart = container.getStartOffset();
        }
    }
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) Matcher(java.util.regex.Matcher) Position(org.eclipse.jface.text.Position) ITextRegionCollection(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionCollection) ITextRegionContainer(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionContainer) IRegion(org.eclipse.jface.text.IRegion) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) Iterator(java.util.Iterator) Region(org.eclipse.jface.text.Region) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) IRegion(org.eclipse.jface.text.IRegion) IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 40 with ITextRegion

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

the class NodeHelper method AttrToString.

public String AttrToString() {
    if (region == null) {
        return null;
    }
    // For debugging
    ITextRegionList t = region.getRegions();
    ITextRegion r;
    Iterator regionIterator = t.iterator();
    // $NON-NLS-1$ //$NON-NLS-2$
    String structuredValue = Messages.NodeHelper00 + getTagName() + Messages.NodeHelper01;
    while (regionIterator.hasNext()) {
        r = (ITextRegion) regionIterator.next();
        if (r.getType() == DOMRegionContext.XML_TAG_ATTRIBUTE_NAME) {
            // $NON-NLS-1$
            structuredValue += "\t\t" + region.getText(r);
            /*
				 * Theres a XML_TAG_ATTRIBUTE_EQUALS after the
				 * XML_TAG_ATTRIBUTE_NAME we have to get rid of
				 */
            if (regionIterator.hasNext()) {
                regionIterator.next();
            }
            if (r.getType() == DOMRegionContext.XML_TAG_ATTRIBUTE_EQUALS) {
                if (regionIterator.hasNext()) {
                    r = ((ITextRegion) regionIterator.next());
                }
                if (r.getType() == DOMRegionContext.XML_TAG_ATTRIBUTE_VALUE) {
                    // $NON-NLS-1$ //$NON-NLS-2$
                    structuredValue += "\t\t" + stripEndQuotes(region.getText(r)) + "\n";
                }
            }
        }
    }
    return structuredValue;
}
Also used : ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) Iterator(java.util.Iterator)

Aggregations

ITextRegion (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)447 ITextRegionList (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList)179 IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)174 Iterator (java.util.Iterator)75 IDOMNode (org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode)58 ArrayList (java.util.ArrayList)40 ITextRegionContainer (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionContainer)39 IndexedRegion (org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)35 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)35 List (java.util.List)33 RegionIterator (org.eclipse.wst.css.core.internal.util.RegionIterator)31 BadLocationException (org.eclipse.jface.text.BadLocationException)30 RegionIterator (org.eclipse.wst.dtd.core.internal.text.RegionIterator)22 TextRegionListImpl (org.eclipse.wst.sse.core.internal.text.TextRegionListImpl)19 ITextRegionCollection (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionCollection)16 Node (org.w3c.dom.Node)16 ContextRegion (org.eclipse.wst.sse.core.internal.parser.ContextRegion)15 IOException (java.io.IOException)13 IRegion (org.eclipse.jface.text.IRegion)13 StyleRange (org.eclipse.swt.custom.StyleRange)13